blob: c0bd97bf65cf53eabfffdc4a61124e8842f76447 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080020import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070021import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070022import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070023import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070024
25import java.util.Collections;
26import java.util.Map;
27import java.util.concurrent.ConcurrentHashMap;
28import java.util.concurrent.ConcurrentMap;
29
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.PortNumber;
31import org.onosproject.net.SparseAnnotations;
32import org.onosproject.net.device.DefaultDeviceDescription;
33import org.onosproject.net.device.DefaultPortDescription;
34import org.onosproject.net.device.DeviceDescription;
Ayaka Koshibeae541732015-05-19 13:37:27 -070035import org.onosproject.net.device.OchPortDescription;
36import org.onosproject.net.device.OduCltPortDescription;
37import org.onosproject.net.device.OmsPortDescription;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020038import org.onosproject.net.device.OtuPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.device.PortDescription;
40import org.onosproject.store.Timestamp;
41import org.onosproject.store.impl.Timestamped;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070042
43/*
44 * Collection of Description of a Device and Ports, given from a Provider.
45 */
46class DeviceDescriptions {
47
48 private volatile Timestamped<DeviceDescription> deviceDesc;
49
50 private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs;
51
52 public DeviceDescriptions(Timestamped<DeviceDescription> desc) {
53 this.deviceDesc = checkNotNull(desc);
54 this.portDescs = new ConcurrentHashMap<>();
55 }
56
57 public Timestamp getLatestTimestamp() {
58 Timestamp latest = deviceDesc.timestamp();
59 for (Timestamped<PortDescription> desc : portDescs.values()) {
60 if (desc.timestamp().compareTo(latest) > 0) {
61 latest = desc.timestamp();
62 }
63 }
64 return latest;
65 }
66
67 public Timestamped<DeviceDescription> getDeviceDesc() {
68 return deviceDesc;
69 }
70
71 public Timestamped<PortDescription> getPortDesc(PortNumber number) {
72 return portDescs.get(number);
73 }
74
75 public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() {
76 return Collections.unmodifiableMap(portDescs);
77 }
78
79 /**
80 * Puts DeviceDescription, merging annotations as necessary.
81 *
82 * @param newDesc new DeviceDescription
83 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070084 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070085 Timestamped<DeviceDescription> oldOne = deviceDesc;
86 Timestamped<DeviceDescription> newOne = newDesc;
87 if (oldOne != null) {
88 SparseAnnotations merged = union(oldOne.value().annotations(),
89 newDesc.value().annotations());
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -070090 newOne = new Timestamped<>(
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070091 new DefaultDeviceDescription(newDesc.value(), merged),
92 newDesc.timestamp());
93 }
94 deviceDesc = newOne;
95 }
96
97 /**
98 * Puts PortDescription, merging annotations as necessary.
99 *
100 * @param newDesc new PortDescription
101 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -0700102 public void putPortDesc(Timestamped<PortDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700103 Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber());
104 Timestamped<PortDescription> newOne = newDesc;
105 if (oldOne != null) {
106 SparseAnnotations merged = union(oldOne.value().annotations(),
107 newDesc.value().annotations());
Ayaka Koshibeae541732015-05-19 13:37:27 -0700108 newOne = null;
109 switch (newDesc.value().type()) {
110 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700111 if (newDesc.value() instanceof OmsPortDescription) {
112 // remove if-block after deprecation is complete
113 OmsPortDescription omsDesc = (OmsPortDescription) (newDesc.value());
114 newOne = new Timestamped<>(
115 omsPortDescription(omsDesc,
116 omsDesc.minFrequency(),
117 omsDesc.maxFrequency(),
118 omsDesc.grid(), merged),
119 newDesc.timestamp());
120 } else {
121 // same as default case
122 newOne = new Timestamped<>(
123 new DefaultPortDescription(newDesc.value(), merged),
124 newDesc.timestamp());
125 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700126 break;
127 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800128 if (newDesc.value() instanceof OchPortDescription) {
129 // remove if-block after Och related deprecation is complete
130 OchPortDescription ochDesc = (OchPortDescription) (newDesc.value());
131 newOne = new Timestamped<>(
132 ochPortDescription(ochDesc,
133 ochDesc.signalType(),
134 ochDesc.isTunable(),
135 ochDesc.lambda(), merged),
136 newDesc.timestamp());
137 } else {
138 // same as default case
139 newOne = new Timestamped<>(
140 new DefaultPortDescription(newDesc.value(), merged),
141 newDesc.timestamp());
142 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700143 break;
144 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700145 if (newDesc.value() instanceof OduCltPortDescription) {
146 // remove if-block after deprecation is complete
147 OduCltPortDescription ocDesc = (OduCltPortDescription) (newDesc.value());
148 newOne = new Timestamped<>(
149 oduCltPortDescription(ocDesc,
150 ocDesc.signalType(),
151 merged),
152 newDesc.timestamp());
153 } else {
154 // same as default case
155 newOne = new Timestamped<>(
156 new DefaultPortDescription(newDesc.value(), merged),
157 newDesc.timestamp());
158 }
Ayaka Koshibeae541732015-05-19 13:37:27 -0700159 break;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200160 case OTU:
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700161 if (newDesc.value() instanceof OtuPortDescription) {
162 // remove if-block after deprecation is complete
163 OtuPortDescription otuDesc = (OtuPortDescription) (newDesc.value());
164 newOne = new Timestamped<>(
165 otuPortDescription(
166 otuDesc, otuDesc.signalType(), merged),
167 newDesc.timestamp());
168 } else {
169 // same as default case
170 newOne = new Timestamped<>(
171 new DefaultPortDescription(newDesc.value(), merged),
172 newDesc.timestamp());
173 }
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200174 break;
Ayaka Koshibeae541732015-05-19 13:37:27 -0700175 default:
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700176 newOne = new Timestamped<>(
Ayaka Koshibeae541732015-05-19 13:37:27 -0700177 new DefaultPortDescription(newDesc.value(), merged),
178 newDesc.timestamp());
179 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700180 }
181 portDescs.put(newOne.value().portNumber(), newOne);
182 }
183}