blob: 43008d243786d50b3989748e01f18bcba826e231 [file] [log] [blame]
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -07001package org.onlab.onos.store.device.impl;
2
3import static com.google.common.base.Preconditions.checkNotNull;
4import static org.onlab.onos.net.DefaultAnnotations.union;
5
6import java.util.Collections;
7import java.util.Map;
8import java.util.concurrent.ConcurrentHashMap;
9import java.util.concurrent.ConcurrentMap;
10
11import org.onlab.onos.net.PortNumber;
12import org.onlab.onos.net.SparseAnnotations;
13import org.onlab.onos.net.device.DefaultDeviceDescription;
14import org.onlab.onos.net.device.DefaultPortDescription;
15import org.onlab.onos.net.device.DeviceDescription;
16import org.onlab.onos.net.device.PortDescription;
17import org.onlab.onos.store.Timestamp;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070018import org.onlab.onos.store.impl.Timestamped;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070019
20/*
21 * Collection of Description of a Device and Ports, given from a Provider.
22 */
23class 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 HIGUCHI47c40882014-10-10 18:44:37 -070061 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070062 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 HIGUCHI47c40882014-10-10 18:44:37 -070079 public void putPortDesc(Timestamped<PortDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070080 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}