Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.device.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.onlab.onos.net.DefaultAnnotations.union; |
| 20 | |
| 21 | import java.util.Collections; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.ConcurrentMap; |
| 25 | |
| 26 | import org.onlab.onos.net.PortNumber; |
| 27 | import org.onlab.onos.net.SparseAnnotations; |
| 28 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
| 29 | import org.onlab.onos.net.device.DefaultPortDescription; |
| 30 | import org.onlab.onos.net.device.DeviceDescription; |
| 31 | import org.onlab.onos.net.device.PortDescription; |
| 32 | import org.onlab.onos.store.Timestamp; |
Yuta HIGUCHI | eecee55 | 2014-10-16 14:09:01 -0700 | [diff] [blame] | 33 | import org.onlab.onos.store.impl.Timestamped; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Collection of Description of a Device and Ports, given from a Provider. |
| 37 | */ |
| 38 | class DeviceDescriptions { |
| 39 | |
| 40 | private volatile Timestamped<DeviceDescription> deviceDesc; |
| 41 | |
| 42 | private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs; |
| 43 | |
| 44 | public DeviceDescriptions(Timestamped<DeviceDescription> desc) { |
| 45 | this.deviceDesc = checkNotNull(desc); |
| 46 | this.portDescs = new ConcurrentHashMap<>(); |
| 47 | } |
| 48 | |
| 49 | public Timestamp getLatestTimestamp() { |
| 50 | Timestamp latest = deviceDesc.timestamp(); |
| 51 | for (Timestamped<PortDescription> desc : portDescs.values()) { |
| 52 | if (desc.timestamp().compareTo(latest) > 0) { |
| 53 | latest = desc.timestamp(); |
| 54 | } |
| 55 | } |
| 56 | return latest; |
| 57 | } |
| 58 | |
| 59 | public Timestamped<DeviceDescription> getDeviceDesc() { |
| 60 | return deviceDesc; |
| 61 | } |
| 62 | |
| 63 | public Timestamped<PortDescription> getPortDesc(PortNumber number) { |
| 64 | return portDescs.get(number); |
| 65 | } |
| 66 | |
| 67 | public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() { |
| 68 | return Collections.unmodifiableMap(portDescs); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Puts DeviceDescription, merging annotations as necessary. |
| 73 | * |
| 74 | * @param newDesc new DeviceDescription |
| 75 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 76 | public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 77 | Timestamped<DeviceDescription> oldOne = deviceDesc; |
| 78 | Timestamped<DeviceDescription> newOne = newDesc; |
| 79 | if (oldOne != null) { |
| 80 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 81 | newDesc.value().annotations()); |
| 82 | newOne = new Timestamped<DeviceDescription>( |
| 83 | new DefaultDeviceDescription(newDesc.value(), merged), |
| 84 | newDesc.timestamp()); |
| 85 | } |
| 86 | deviceDesc = newOne; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Puts PortDescription, merging annotations as necessary. |
| 91 | * |
| 92 | * @param newDesc new PortDescription |
| 93 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 94 | public void putPortDesc(Timestamped<PortDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 95 | Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber()); |
| 96 | Timestamped<PortDescription> newOne = newDesc; |
| 97 | if (oldOne != null) { |
| 98 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 99 | newDesc.value().annotations()); |
| 100 | newOne = new Timestamped<PortDescription>( |
| 101 | new DefaultPortDescription(newDesc.value(), merged), |
| 102 | newDesc.timestamp()); |
| 103 | } |
| 104 | portDescs.put(newOne.value().portNumber(), newOne); |
| 105 | } |
| 106 | } |