2 * Copyright 2014 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.device.impl;
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.onosproject.net.DefaultAnnotations.union;
21 import java.util.Collections;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
26 import org.onosproject.net.PortNumber;
27 import org.onosproject.net.SparseAnnotations;
28 import org.onosproject.net.device.DefaultDeviceDescription;
29 import org.onosproject.net.device.DefaultPortDescription;
30 import org.onosproject.net.device.DeviceDescription;
31 import org.onosproject.net.device.OchPortDescription;
32 import org.onosproject.net.device.OduCltPortDescription;
33 import org.onosproject.net.device.OmsPortDescription;
34 import org.onosproject.net.device.PortDescription;
35 import org.onosproject.store.Timestamp;
36 import org.onosproject.store.impl.Timestamped;
39 * Collection of Description of a Device and Ports, given from a Provider.
41 class DeviceDescriptions {
43 private volatile Timestamped<DeviceDescription> deviceDesc;
45 private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs;
47 public DeviceDescriptions(Timestamped<DeviceDescription> desc) {
48 this.deviceDesc = checkNotNull(desc);
49 this.portDescs = new ConcurrentHashMap<>();
52 public Timestamp getLatestTimestamp() {
53 Timestamp latest = deviceDesc.timestamp();
54 for (Timestamped<PortDescription> desc : portDescs.values()) {
55 if (desc.timestamp().compareTo(latest) > 0) {
56 latest = desc.timestamp();
62 public Timestamped<DeviceDescription> getDeviceDesc() {
66 public Timestamped<PortDescription> getPortDesc(PortNumber number) {
67 return portDescs.get(number);
70 public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() {
71 return Collections.unmodifiableMap(portDescs);
75 * Puts DeviceDescription, merging annotations as necessary.
77 * @param newDesc new DeviceDescription
79 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
80 Timestamped<DeviceDescription> oldOne = deviceDesc;
81 Timestamped<DeviceDescription> newOne = newDesc;
83 SparseAnnotations merged = union(oldOne.value().annotations(),
84 newDesc.value().annotations());
85 newOne = new Timestamped<DeviceDescription>(
86 new DefaultDeviceDescription(newDesc.value(), merged),
93 * Puts PortDescription, merging annotations as necessary.
95 * @param newDesc new PortDescription
97 public void putPortDesc(Timestamped<PortDescription> newDesc) {
98 Timestamped<PortDescription> oldOne = portDescs.get(newDesc.value().portNumber());
99 Timestamped<PortDescription> newOne = newDesc;
100 if (oldOne != null) {
101 SparseAnnotations merged = union(oldOne.value().annotations(),
102 newDesc.value().annotations());
104 switch (newDesc.value().type()) {
106 OmsPortDescription omsDesc = (OmsPortDescription) (newDesc.value());
107 newOne = new Timestamped<PortDescription>(
108 new OmsPortDescription(
109 omsDesc, omsDesc.minFrequency(), omsDesc.maxFrequency(), omsDesc.grid(), merged),
110 newDesc.timestamp());
113 OchPortDescription ochDesc = (OchPortDescription) (newDesc.value());
114 newOne = new Timestamped<PortDescription>(
115 new OchPortDescription(
116 ochDesc, ochDesc.signalType(), ochDesc.isTunable(), ochDesc.lambda(), merged),
117 newDesc.timestamp());
120 OduCltPortDescription ocDesc = (OduCltPortDescription) (newDesc.value());
121 newOne = new Timestamped<PortDescription>(
122 new OduCltPortDescription(
123 ocDesc, ocDesc.signalType(), merged),
124 newDesc.timestamp());
127 newOne = new Timestamped<PortDescription>(
128 new DefaultPortDescription(newDesc.value(), merged),
129 newDesc.timestamp());
132 portDescs.put(newOne.value().portNumber(), newOne);