blob: 0bf45b3c93644063e394a2d632e5b784eef18ba0 [file] [log] [blame]
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ayaka Koshibee39c23a2015-08-03 18:17:43 -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 */
16package org.onosproject.net.device.impl;
17
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080018import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070019import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070020import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070021import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070022import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070023import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070024
Ray Milkeya4122362015-08-18 15:19:08 -070025import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070026import org.onosproject.net.config.basics.OpticalPortConfig;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070027import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.DefaultAnnotations;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070029import org.onosproject.net.Port;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070030import org.onosproject.net.PortNumber;
31import org.onosproject.net.SparseAnnotations;
32import org.onosproject.net.device.DefaultPortDescription;
33import org.onosproject.net.device.OchPortDescription;
34import org.onosproject.net.device.OduCltPortDescription;
35import org.onosproject.net.device.OmsPortDescription;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020036import org.onosproject.net.device.OtuPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070037import org.onosproject.net.device.PortDescription;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080038import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070039import org.onosproject.net.optical.OduCltPort;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070040import org.onosproject.net.optical.OmsPort;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080041import org.onosproject.net.optical.OpticalDevice;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070042import org.onosproject.net.optical.OtuPort;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070043import org.slf4j.Logger;
44
45/**
46 * Implementations of merge policies for various sources of optical port
47 * configuration information. This includes applications, provides, and network
48 * configurations.
49 */
50public final class OpticalPortOperator implements ConfigOperator {
51
52 private static final Logger log = getLogger(OpticalPortOperator.class);
53
54 private OpticalPortOperator() {
55 }
56
57 /**
58 * Generates a PortDescription containing fields from a PortDescription and
59 * an OpticalPortConfig.
60 *
61 * @param opc the port config entity from network config
62 * @param descr a PortDescription
63 * @return PortDescription based on both sources
64 */
65 public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) {
66 if (opc == null) {
67 return descr;
68 }
69
70 PortNumber port = descr.portNumber();
71 final String name = opc.name();
72 final String numName = opc.numberName();
73 // if the description is null, or the current description port name != config name,
74 // create a new PortNumber.
75 PortNumber newPort = null;
76 if (port == null) {
77 // try to get the portNumber from the numName.
78 if (!numName.isEmpty()) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070079 final long pn = Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070080 newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn);
81 } else {
82 // we don't have defining info (a port number value)
83 throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr);
84 }
85 } else if ((!name.isEmpty()) && !name.equals(port.name())) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070086 final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070087 newPort = PortNumber.portNumber(pn, name);
88 }
89
90 // Port type won't change unless we're overwriting a port completely.
91 // Watch out for overwrites to avoid class cast craziness.
Sho SHIMIZUb89d4912015-09-09 14:37:59 -070092 boolean noOwrite = opc.type() == descr.type();
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070093
94 SparseAnnotations sa = combine(opc, descr.annotations());
95 if (noOwrite) {
96 return updateDescription((newPort == null) ? port : newPort, sa, descr);
97 } else {
98 // TODO: must reconstruct a different type of PortDescription.
99 log.info("Type rewrite from {} to {} required", descr.type(), opc.type());
100 }
101 return descr;
102 }
103
104 // updates a port description whose port type has not changed.
105 private static PortDescription updateDescription(
106 PortNumber port, SparseAnnotations sa, PortDescription descr) {
107 switch (descr.type()) {
108 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700109 if (descr instanceof OmsPortDescription) {
110 // TODO This block can go away once deprecation is complete.
111 OmsPortDescription oms = (OmsPortDescription) descr;
112 return omsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
113 oms.maxFrequency(), oms.grid(), sa);
114 }
115 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700116 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800117 // We might need to update lambda below with STATIC_LAMBDA.
118 if (descr instanceof OchPortDescription) {
119 // TODO This block can go away once deprecation is complete.
120 OchPortDescription och = (OchPortDescription) descr;
121 return ochPortDescription(port, och.isEnabled(), och.signalType(),
122 och.isTunable(), och.lambda(), sa);
123 }
124 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700125 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700126 if (descr instanceof OduCltPortDescription) {
127 // TODO This block can go away once deprecation is complete.
128 OduCltPortDescription odu = (OduCltPortDescription) descr;
129 return oduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
130 }
131 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700132 case PACKET:
133 case FIBER:
Ayaka Koshibe144bab02015-10-22 12:56:59 -0700134 case COPPER:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800135 // TODO: it should be safe to just return descr. confirm and fix
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700136 return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
137 descr.portSpeed(), sa);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200138 case OTU:
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700139 if (descr instanceof OtuPortDescription) {
140 // TODO This block can go away once deprecation is complete.
141 OtuPortDescription otu = (OtuPortDescription) descr;
142 return otuPortDescription(port, otu.isEnabled(), otu.signalType(), sa);
143 }
144 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700145 default:
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700146 log.warn("Unsupported optical port type {} - can't update", descr.type());
147 return descr;
148 }
149 }
150
151 /**
152 * Generates an annotation from an existing annotation and OptcalPortConfig.
153 *
154 * @param opc the port config entity from network config
155 * @param an the annotation
156 * @return annotation combining both sources
157 */
158 public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
159 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
160 if (!opc.staticPort().isEmpty()) {
161 b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
162 }
163 if (opc.staticLambda().isPresent()) {
164 b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
165 }
166 // The following may not need to be carried.
167 if (!opc.name().isEmpty()) {
168 b.set(AnnotationKeys.PORT_NAME, opc.name());
169 }
170 return DefaultAnnotations.union(an, b.build());
171 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700172
173 /**
174 * Returns a description built from an existing port.
175 *
176 * @param port the device port
177 * @return a PortDescription based on the port
178 */
179 public static PortDescription descriptionOf(Port port) {
180 checkNotNull(port, "Must supply non-null Port");
Yafit Hadara9a73de2015-09-06 13:52:52 +0300181 final boolean isUp = port.isEnabled();
182 return descriptionOfPort(port, isUp);
183 }
184
185 /**
186 * Returns a description built from an existing port and reported status.
187 *
Brian O'Connor52515622015-10-09 17:03:44 -0700188 * @param port port
189 * @param isEnabled true if enabled
Yafit Hadara9a73de2015-09-06 13:52:52 +0300190 * @return a PortDescription based on the port
191 */
192 static PortDescription descriptionOf(Port port, boolean isEnabled) {
193 checkNotNull(port, "Must supply non-null Port");
194 final boolean isup = isEnabled;
195 return descriptionOfPort(port, isup);
196 }
197
198 private static PortDescription descriptionOfPort(Port port, final boolean isup) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700199 final PortNumber ptn = port.number();
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700200 final SparseAnnotations an = (SparseAnnotations) port.annotations();
201 switch (port.type()) {
202 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700203 if (port instanceof org.onosproject.net.OmsPort) {
204 // remove if-block once deprecation is complete
205 org.onosproject.net.OmsPort oms = (org.onosproject.net.OmsPort) port;
206 return omsPortDescription(ptn, isup, oms.minFrequency(),
207 oms.maxFrequency(), oms.grid(), an);
208 }
209 if (port.element().is(OpticalDevice.class)) {
210 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
211 if (optDevice.portIs(port, OmsPort.class)) {
212 OmsPort oms = optDevice.portAs(port, OmsPort.class).get();
213 return omsPortDescription(ptn, isup, oms.minFrequency(),
214 oms.maxFrequency(), oms.grid(), an);
215 }
216 }
217 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700218 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800219 if (port instanceof org.onosproject.net.OchPort) {
220 // remove if-block once old OchPort deprecation is complete
221 org.onosproject.net.OchPort och = (org.onosproject.net.OchPort) port;
222 return ochPortDescription(ptn, isup, och.signalType(),
223 och.isTunable(), och.lambda(), an);
224 }
225 if (port.element().is(OpticalDevice.class)) {
226 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
227 if (optDevice.portIs(port, OchPort.class)) {
228 OchPort och = optDevice.portAs(port, OchPort.class).get();
229 return ochPortDescription(ptn, isup, och.signalType(),
230 och.isTunable(), och.lambda(), an);
231 }
232 }
233 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
234
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700235 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700236 if (port instanceof org.onosproject.net.OduCltPort) {
237 // remove if-block once deprecation is complete
238 org.onosproject.net.OduCltPort odu = (org.onosproject.net.OduCltPort) port;
239 return oduCltPortDescription(ptn, isup, odu.signalType(), an);
240 }
241 if (port.element().is(OpticalDevice.class)) {
242 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
243 if (optDevice.portIs(port, OduCltPort.class)) {
244 OduCltPort odu = (OduCltPort) port;
245 return oduCltPortDescription(ptn, isup, odu.signalType(), an);
246 }
247 }
248 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200249 case OTU:
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700250 if (port instanceof org.onosproject.net.OtuPort) {
251 // remove if-block once deprecation is complete
252 org.onosproject.net.OtuPort otu = (org.onosproject.net.OtuPort) port;
253 return otuPortDescription(ptn, isup, otu.signalType(), an);
254 }
255 if (port.element().is(OpticalDevice.class)) {
256 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
257 if (optDevice.portIs(port, OtuPort.class)) {
258 OtuPort otu = (OtuPort) port;
259 return otuPortDescription(ptn, isup, otu.signalType(), an);
260 }
261 }
262 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700263 default:
264 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
265 }
266 }
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700267}