Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | import static org.onlab.onos.net.DefaultAnnotations.union; |
| 5 | |
| 6 | import java.util.Collections; |
| 7 | import java.util.Map; |
| 8 | import java.util.concurrent.ConcurrentHashMap; |
| 9 | import java.util.concurrent.ConcurrentMap; |
| 10 | |
| 11 | import org.onlab.onos.net.PortNumber; |
| 12 | import org.onlab.onos.net.SparseAnnotations; |
| 13 | import org.onlab.onos.net.device.DefaultDeviceDescription; |
| 14 | import org.onlab.onos.net.device.DefaultPortDescription; |
| 15 | import org.onlab.onos.net.device.DeviceDescription; |
| 16 | import org.onlab.onos.net.device.PortDescription; |
| 17 | import org.onlab.onos.store.Timestamp; |
Yuta HIGUCHI | eecee55 | 2014-10-16 14:09:01 -0700 | [diff] [blame] | 18 | import org.onlab.onos.store.impl.Timestamped; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Collection of Description of a Device and Ports, given from a Provider. |
| 22 | */ |
| 23 | class DeviceDescriptions { |
| 24 | |
| 25 | private volatile Timestamped<DeviceDescription> deviceDesc; |
| 26 | |
| 27 | private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs; |
| 28 | |
| 29 | public DeviceDescriptions(Timestamped<DeviceDescription> desc) { |
| 30 | this.deviceDesc = checkNotNull(desc); |
| 31 | this.portDescs = new ConcurrentHashMap<>(); |
| 32 | } |
| 33 | |
| 34 | public Timestamp getLatestTimestamp() { |
| 35 | Timestamp latest = deviceDesc.timestamp(); |
| 36 | for (Timestamped<PortDescription> desc : portDescs.values()) { |
| 37 | if (desc.timestamp().compareTo(latest) > 0) { |
| 38 | latest = desc.timestamp(); |
| 39 | } |
| 40 | } |
| 41 | return latest; |
| 42 | } |
| 43 | |
| 44 | public Timestamped<DeviceDescription> getDeviceDesc() { |
| 45 | return deviceDesc; |
| 46 | } |
| 47 | |
| 48 | public Timestamped<PortDescription> getPortDesc(PortNumber number) { |
| 49 | return portDescs.get(number); |
| 50 | } |
| 51 | |
| 52 | public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() { |
| 53 | return Collections.unmodifiableMap(portDescs); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Puts DeviceDescription, merging annotations as necessary. |
| 58 | * |
| 59 | * @param newDesc new DeviceDescription |
| 60 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 61 | public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 62 | Timestamped<DeviceDescription> oldOne = deviceDesc; |
| 63 | Timestamped<DeviceDescription> newOne = newDesc; |
| 64 | if (oldOne != null) { |
| 65 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 66 | newDesc.value().annotations()); |
| 67 | newOne = new Timestamped<DeviceDescription>( |
| 68 | new DefaultDeviceDescription(newDesc.value(), merged), |
| 69 | newDesc.timestamp()); |
| 70 | } |
| 71 | deviceDesc = newOne; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Puts PortDescription, merging annotations as necessary. |
| 76 | * |
| 77 | * @param newDesc new PortDescription |
| 78 | */ |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 79 | public void putPortDesc(Timestamped<PortDescription> newDesc) { |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 80 | Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber()); |
| 81 | Timestamped<PortDescription> newOne = newDesc; |
| 82 | if (oldOne != null) { |
| 83 | SparseAnnotations merged = union(oldOne.value().annotations(), |
| 84 | newDesc.value().annotations()); |
| 85 | newOne = new Timestamped<PortDescription>( |
| 86 | new DefaultPortDescription(newDesc.value(), merged), |
| 87 | newDesc.timestamp()); |
| 88 | } |
| 89 | portDescs.put(newOne.value().portNumber(), newOne); |
| 90 | } |
| 91 | } |