blob: b51377fbbeebe4abb030e5175c3398686091d14c [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 HIGUCHI9ee60f62014-10-09 10:00:01 -070016package org.onlab.onos.store.device.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.onlab.onos.net.DefaultAnnotations.union;
20
21import java.util.Collections;
22import java.util.Map;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.ConcurrentMap;
25
26import org.onlab.onos.net.PortNumber;
27import org.onlab.onos.net.SparseAnnotations;
28import org.onlab.onos.net.device.DefaultDeviceDescription;
29import org.onlab.onos.net.device.DefaultPortDescription;
30import org.onlab.onos.net.device.DeviceDescription;
31import org.onlab.onos.net.device.PortDescription;
32import org.onlab.onos.store.Timestamp;
Yuta HIGUCHIeecee552014-10-16 14:09:01 -070033import org.onlab.onos.store.impl.Timestamped;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070034
35/*
36 * Collection of Description of a Device and Ports, given from a Provider.
37 */
38class 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 HIGUCHI47c40882014-10-10 18:44:37 -070076 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070077 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 HIGUCHI47c40882014-10-10 18:44:37 -070094 public void putPortDesc(Timestamped<PortDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070095 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}