blob: 4c391ce1308f6023c2666cf59b8344bfee421a0a [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;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070020
21import java.util.Collections;
22import java.util.Map;
23import java.util.concurrent.ConcurrentHashMap;
24import java.util.concurrent.ConcurrentMap;
25
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.PortNumber;
27import org.onosproject.net.SparseAnnotations;
28import org.onosproject.net.device.DefaultDeviceDescription;
29import org.onosproject.net.device.DefaultPortDescription;
30import org.onosproject.net.device.DeviceDescription;
Ayaka Koshibeae541732015-05-19 13:37:27 -070031import org.onosproject.net.device.OchPortDescription;
32import org.onosproject.net.device.OduCltPortDescription;
33import org.onosproject.net.device.OmsPortDescription;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020034import org.onosproject.net.device.OtuPortDescription;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.device.PortDescription;
36import org.onosproject.store.Timestamp;
37import org.onosproject.store.impl.Timestamped;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070038
39/*
40 * Collection of Description of a Device and Ports, given from a Provider.
41 */
42class DeviceDescriptions {
43
44 private volatile Timestamped<DeviceDescription> deviceDesc;
45
46 private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs;
47
48 public DeviceDescriptions(Timestamped<DeviceDescription> desc) {
49 this.deviceDesc = checkNotNull(desc);
50 this.portDescs = new ConcurrentHashMap<>();
51 }
52
53 public Timestamp getLatestTimestamp() {
54 Timestamp latest = deviceDesc.timestamp();
55 for (Timestamped<PortDescription> desc : portDescs.values()) {
56 if (desc.timestamp().compareTo(latest) > 0) {
57 latest = desc.timestamp();
58 }
59 }
60 return latest;
61 }
62
63 public Timestamped<DeviceDescription> getDeviceDesc() {
64 return deviceDesc;
65 }
66
67 public Timestamped<PortDescription> getPortDesc(PortNumber number) {
68 return portDescs.get(number);
69 }
70
71 public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() {
72 return Collections.unmodifiableMap(portDescs);
73 }
74
75 /**
76 * Puts DeviceDescription, merging annotations as necessary.
77 *
78 * @param newDesc new DeviceDescription
79 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070080 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070081 Timestamped<DeviceDescription> oldOne = deviceDesc;
82 Timestamped<DeviceDescription> newOne = newDesc;
83 if (oldOne != null) {
84 SparseAnnotations merged = union(oldOne.value().annotations(),
85 newDesc.value().annotations());
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -070086 newOne = new Timestamped<>(
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070087 new DefaultDeviceDescription(newDesc.value(), merged),
88 newDesc.timestamp());
89 }
90 deviceDesc = newOne;
91 }
92
93 /**
94 * Puts PortDescription, merging annotations as necessary.
95 *
96 * @param newDesc new PortDescription
97 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070098 public void putPortDesc(Timestamped<PortDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070099 Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber());
100 Timestamped<PortDescription> newOne = newDesc;
101 if (oldOne != null) {
102 SparseAnnotations merged = union(oldOne.value().annotations(),
103 newDesc.value().annotations());
Ayaka Koshibeae541732015-05-19 13:37:27 -0700104 newOne = null;
105 switch (newDesc.value().type()) {
106 case OMS:
107 OmsPortDescription omsDesc = (OmsPortDescription) (newDesc.value());
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700108 newOne = new Timestamped<>(
Ayaka Koshibeae541732015-05-19 13:37:27 -0700109 new OmsPortDescription(
110 omsDesc, omsDesc.minFrequency(), omsDesc.maxFrequency(), omsDesc.grid(), merged),
111 newDesc.timestamp());
112 break;
113 case OCH:
114 OchPortDescription ochDesc = (OchPortDescription) (newDesc.value());
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700115 newOne = new Timestamped<>(
Ayaka Koshibeae541732015-05-19 13:37:27 -0700116 new OchPortDescription(
117 ochDesc, ochDesc.signalType(), ochDesc.isTunable(), ochDesc.lambda(), merged),
118 newDesc.timestamp());
119 break;
120 case ODUCLT:
121 OduCltPortDescription ocDesc = (OduCltPortDescription) (newDesc.value());
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700122 newOne = new Timestamped<>(
Ayaka Koshibeae541732015-05-19 13:37:27 -0700123 new OduCltPortDescription(
124 ocDesc, ocDesc.signalType(), merged),
125 newDesc.timestamp());
126 break;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200127 case OTU:
128 OtuPortDescription otuDesc = (OtuPortDescription) (newDesc.value());
129 newOne = new Timestamped<>(
130 new OtuPortDescription(
131 otuDesc, otuDesc.signalType(), merged),
132 newDesc.timestamp());
133 break;
Ayaka Koshibeae541732015-05-19 13:37:27 -0700134 default:
Sho SHIMIZU7a4087b2015-09-10 09:23:16 -0700135 newOne = new Timestamped<>(
Ayaka Koshibeae541732015-05-19 13:37:27 -0700136 new DefaultPortDescription(newDesc.value(), merged),
137 newDesc.timestamp());
138 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700139 }
140 portDescs.put(newOne.value().portNumber(), newOne);
141 }
142}