fd7fcd8016bdde1d4f15b14b7a24670f77ad0ddb
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.store.device.impl;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.onosproject.net.DefaultAnnotations.union;
20
21 import java.util.Collections;
22 import java.util.Map;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25
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;
37
38 /*
39  * Collection of Description of a Device and Ports, given from a Provider.
40  */
41 class 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      */
79     public void putDeviceDesc(Timestamped<DeviceDescription> newDesc) {
80         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      */
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());
103             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             }
131         }
132         portDescs.put(newOne.value().portNumber(), newOne);
133     }
134 }