blob: bcebc613477d6b19b01553ddf23d3b9282d301d8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.device.impl;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070017
18import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import static org.onosproject.net.DefaultAnnotations.union;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070020import java.util.Collections;
21import java.util.Map;
22import java.util.concurrent.ConcurrentHashMap;
23import java.util.concurrent.ConcurrentMap;
24
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.PortNumber;
26import org.onosproject.net.SparseAnnotations;
27import org.onosproject.net.device.DefaultDeviceDescription;
28import org.onosproject.net.device.DefaultPortDescription;
29import org.onosproject.net.device.DeviceDescription;
30import org.onosproject.net.device.PortDescription;
31import org.onosproject.store.Timestamp;
32import org.onosproject.store.impl.Timestamped;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070033
34/*
35 * Collection of Description of a Device and Ports, given from a Provider.
36 */
37class DeviceDescriptions {
38
39 private volatile Timestamped<DeviceDescription> deviceDesc;
40
41 private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs;
42
43 public DeviceDescriptions(Timestamped<DeviceDescription> desc) {
44 this.deviceDesc = checkNotNull(desc);
45 this.portDescs = new ConcurrentHashMap<>();
46 }
47
48 public Timestamp getLatestTimestamp() {
49 Timestamp latest = deviceDesc.timestamp();
50 for (Timestamped<PortDescription> desc : portDescs.values()) {
51 if (desc.timestamp().compareTo(latest) > 0) {
52 latest = desc.timestamp();
53 }
54 }
55 return latest;
56 }
57
58 public Timestamped<DeviceDescription> getDeviceDesc() {
59 return deviceDesc;
60 }
61
62 public Timestamped<PortDescription> getPortDesc(PortNumber number) {
63 return portDescs.get(number);
64 }
65
66 public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() {
67 return Collections.unmodifiableMap(portDescs);
68 }
69
70 /**
71 * Puts DeviceDescription, merging annotations as necessary.
72 *
73 * @param newDesc new DeviceDescription
74 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070075 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070076 Timestamped<DeviceDescription> oldOne = deviceDesc;
77 Timestamped<DeviceDescription> newOne = newDesc;
78 if (oldOne != null) {
79 SparseAnnotations merged = union(oldOne.value().annotations(),
80 newDesc.value().annotations());
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -070081 newOne = new Timestamped<>(
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070082 new DefaultDeviceDescription(newDesc.value(), merged),
83 newDesc.timestamp());
84 }
85 deviceDesc = newOne;
86 }
87
88 /**
89 * Puts PortDescription, merging annotations as necessary.
90 *
91 * @param newDesc new PortDescription
92 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070093 public void putPortDesc(Timestamped<PortDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070094 Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber());
95 Timestamped<PortDescription> newOne = newDesc;
96 if (oldOne != null) {
97 SparseAnnotations merged = union(oldOne.value().annotations(),
98 newDesc.value().annotations());
HIGUCHI Yuta6972ae62016-05-12 19:57:46 -070099 newOne = new Timestamped<>(
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800100 DefaultPortDescription.builder(newDesc.value())
101 .annotations(merged)
102 .build(),
Ayaka Koshibeae541732015-05-19 13:37:27 -0700103 newDesc.timestamp());
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700104 }
105 portDescs.put(newOne.value().portNumber(), newOne);
106 }
Andrea Campanellab0dfbc82020-08-10 18:06:42 +0200107
108 /**
109 * Removes PortDescription.
110 *
111 * @param portNumber the port to remove.
112 */
113 public void removePortDesc(PortNumber portNumber) {
114 portDescs.remove(portNumber);
115 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700116}