blob: fd7fcd8016bdde1d4f15b14b7a24670f77ad0ddb [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 */
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.device.PortDescription;
35import org.onosproject.store.Timestamp;
36import org.onosproject.store.impl.Timestamped;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070037
38/*
39 * Collection of Description of a Device and Ports, given from a Provider.
40 */
41class DeviceDescriptions {
42
43 private volatile Timestamped<DeviceDescription> deviceDesc;
44
45 private final ConcurrentMap<PortNumber, Timestamped<PortDescription>> portDescs;
46
47 public DeviceDescriptions(Timestamped<DeviceDescription> desc) {
48 this.deviceDesc = checkNotNull(desc);
49 this.portDescs = new ConcurrentHashMap<>();
50 }
51
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();
57 }
58 }
59 return latest;
60 }
61
62 public Timestamped<DeviceDescription> getDeviceDesc() {
63 return deviceDesc;
64 }
65
66 public Timestamped<PortDescription> getPortDesc(PortNumber number) {
67 return portDescs.get(number);
68 }
69
70 public Map<PortNumber, Timestamped<PortDescription>> getPortDescs() {
71 return Collections.unmodifiableMap(portDescs);
72 }
73
74 /**
75 * Puts DeviceDescription, merging annotations as necessary.
76 *
77 * @param newDesc new DeviceDescription
78 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070079 public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070080 Timestamped<DeviceDescription> oldOne = deviceDesc;
81 Timestamped<DeviceDescription> newOne = newDesc;
82 if (oldOne != null) {
83 SparseAnnotations merged = union(oldOne.value().annotations(),
84 newDesc.value().annotations());
85 newOne = new Timestamped<DeviceDescription>(
86 new DefaultDeviceDescription(newDesc.value(), merged),
87 newDesc.timestamp());
88 }
89 deviceDesc = newOne;
90 }
91
92 /**
93 * Puts PortDescription, merging annotations as necessary.
94 *
95 * @param newDesc new PortDescription
96 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070097 public void putPortDesc(Timestamped<PortDescription> newDesc) {
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070098 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());
Ayaka Koshibeae541732015-05-19 13:37:27 -0700103 newOne = null;
104 switch (newDesc.value().type()) {
105 case OMS:
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());
111 break;
112 case OCH:
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());
118 break;
119 case ODUCLT:
120 OduCltPortDescription ocDesc = (OduCltPortDescription) (newDesc.value());
121 newOne = new Timestamped<PortDescription>(
122 new OduCltPortDescription(
123 ocDesc, ocDesc.signalType(), merged),
124 newDesc.timestamp());
125 break;
126 default:
127 newOne = new Timestamped<PortDescription>(
128 new DefaultPortDescription(newDesc.value(), merged),
129 newDesc.timestamp());
130 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700131 }
132 portDescs.put(newOne.value().portNumber(), newOne);
133 }
134}