blob: 51000a92be5ffb0299e26226610e4168fa9a59a6 [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;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070021import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070022import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070023
Ray Milkeya4122362015-08-18 15:19:08 -070024import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070025import org.onosproject.net.config.basics.OpticalPortConfig;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070026import org.onosproject.net.AnnotationKeys;
27import org.onosproject.net.DefaultAnnotations;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020028import org.onosproject.net.OtuPort;
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;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070042import org.slf4j.Logger;
43
44/**
45 * Implementations of merge policies for various sources of optical port
46 * configuration information. This includes applications, provides, and network
47 * configurations.
48 */
49public final class OpticalPortOperator implements ConfigOperator {
50
51 private static final Logger log = getLogger(OpticalPortOperator.class);
52
53 private OpticalPortOperator() {
54 }
55
56 /**
57 * Generates a PortDescription containing fields from a PortDescription and
58 * an OpticalPortConfig.
59 *
60 * @param opc the port config entity from network config
61 * @param descr a PortDescription
62 * @return PortDescription based on both sources
63 */
64 public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) {
65 if (opc == null) {
66 return descr;
67 }
68
69 PortNumber port = descr.portNumber();
70 final String name = opc.name();
71 final String numName = opc.numberName();
72 // if the description is null, or the current description port name != config name,
73 // create a new PortNumber.
74 PortNumber newPort = null;
75 if (port == null) {
76 // try to get the portNumber from the numName.
77 if (!numName.isEmpty()) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070078 final long pn = Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070079 newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn);
80 } else {
81 // we don't have defining info (a port number value)
82 throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr);
83 }
84 } else if ((!name.isEmpty()) && !name.equals(port.name())) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070085 final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070086 newPort = PortNumber.portNumber(pn, name);
87 }
88
89 // Port type won't change unless we're overwriting a port completely.
90 // Watch out for overwrites to avoid class cast craziness.
Sho SHIMIZUb89d4912015-09-09 14:37:59 -070091 boolean noOwrite = opc.type() == descr.type();
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070092
93 SparseAnnotations sa = combine(opc, descr.annotations());
94 if (noOwrite) {
95 return updateDescription((newPort == null) ? port : newPort, sa, descr);
96 } else {
97 // TODO: must reconstruct a different type of PortDescription.
98 log.info("Type rewrite from {} to {} required", descr.type(), opc.type());
99 }
100 return descr;
101 }
102
103 // updates a port description whose port type has not changed.
104 private static PortDescription updateDescription(
105 PortNumber port, SparseAnnotations sa, PortDescription descr) {
106 switch (descr.type()) {
107 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700108 if (descr instanceof OmsPortDescription) {
109 // TODO This block can go away once deprecation is complete.
110 OmsPortDescription oms = (OmsPortDescription) descr;
111 return omsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
112 oms.maxFrequency(), oms.grid(), sa);
113 }
114 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700115 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800116 // We might need to update lambda below with STATIC_LAMBDA.
117 if (descr instanceof OchPortDescription) {
118 // TODO This block can go away once deprecation is complete.
119 OchPortDescription och = (OchPortDescription) descr;
120 return ochPortDescription(port, och.isEnabled(), och.signalType(),
121 och.isTunable(), och.lambda(), sa);
122 }
123 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700124 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700125 if (descr instanceof OduCltPortDescription) {
126 // TODO This block can go away once deprecation is complete.
127 OduCltPortDescription odu = (OduCltPortDescription) descr;
128 return oduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
129 }
130 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700131 case PACKET:
132 case FIBER:
Ayaka Koshibe144bab02015-10-22 12:56:59 -0700133 case COPPER:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800134 // TODO: it should be safe to just return descr. confirm and fix
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700135 return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
136 descr.portSpeed(), sa);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200137 case OTU:
138 OtuPortDescription otu = (OtuPortDescription) descr;
139 return new OtuPortDescription(port, otu.isEnabled(), otu.signalType(), sa);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700140 default:
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700141 log.warn("Unsupported optical port type {} - can't update", descr.type());
142 return descr;
143 }
144 }
145
146 /**
147 * Generates an annotation from an existing annotation and OptcalPortConfig.
148 *
149 * @param opc the port config entity from network config
150 * @param an the annotation
151 * @return annotation combining both sources
152 */
153 public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
154 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
155 if (!opc.staticPort().isEmpty()) {
156 b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
157 }
158 if (opc.staticLambda().isPresent()) {
159 b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
160 }
161 // The following may not need to be carried.
162 if (!opc.name().isEmpty()) {
163 b.set(AnnotationKeys.PORT_NAME, opc.name());
164 }
165 return DefaultAnnotations.union(an, b.build());
166 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700167
168 /**
169 * Returns a description built from an existing port.
170 *
171 * @param port the device port
172 * @return a PortDescription based on the port
173 */
174 public static PortDescription descriptionOf(Port port) {
175 checkNotNull(port, "Must supply non-null Port");
Yafit Hadara9a73de2015-09-06 13:52:52 +0300176 final boolean isUp = port.isEnabled();
177 return descriptionOfPort(port, isUp);
178 }
179
180 /**
181 * Returns a description built from an existing port and reported status.
182 *
Brian O'Connor52515622015-10-09 17:03:44 -0700183 * @param port port
184 * @param isEnabled true if enabled
Yafit Hadara9a73de2015-09-06 13:52:52 +0300185 * @return a PortDescription based on the port
186 */
187 static PortDescription descriptionOf(Port port, boolean isEnabled) {
188 checkNotNull(port, "Must supply non-null Port");
189 final boolean isup = isEnabled;
190 return descriptionOfPort(port, isup);
191 }
192
193 private static PortDescription descriptionOfPort(Port port, final boolean isup) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700194 final PortNumber ptn = port.number();
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700195 final SparseAnnotations an = (SparseAnnotations) port.annotations();
196 switch (port.type()) {
197 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700198 if (port instanceof org.onosproject.net.OmsPort) {
199 // remove if-block once deprecation is complete
200 org.onosproject.net.OmsPort oms = (org.onosproject.net.OmsPort) port;
201 return omsPortDescription(ptn, isup, oms.minFrequency(),
202 oms.maxFrequency(), oms.grid(), an);
203 }
204 if (port.element().is(OpticalDevice.class)) {
205 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
206 if (optDevice.portIs(port, OmsPort.class)) {
207 OmsPort oms = optDevice.portAs(port, OmsPort.class).get();
208 return omsPortDescription(ptn, isup, oms.minFrequency(),
209 oms.maxFrequency(), oms.grid(), an);
210 }
211 }
212 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700213 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800214 if (port instanceof org.onosproject.net.OchPort) {
215 // remove if-block once old OchPort deprecation is complete
216 org.onosproject.net.OchPort och = (org.onosproject.net.OchPort) port;
217 return ochPortDescription(ptn, isup, och.signalType(),
218 och.isTunable(), och.lambda(), an);
219 }
220 if (port.element().is(OpticalDevice.class)) {
221 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
222 if (optDevice.portIs(port, OchPort.class)) {
223 OchPort och = optDevice.portAs(port, OchPort.class).get();
224 return ochPortDescription(ptn, isup, och.signalType(),
225 och.isTunable(), och.lambda(), an);
226 }
227 }
228 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
229
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700230 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700231 if (port instanceof org.onosproject.net.OduCltPort) {
232 // remove if-block once deprecation is complete
233 org.onosproject.net.OduCltPort odu = (org.onosproject.net.OduCltPort) port;
234 return oduCltPortDescription(ptn, isup, odu.signalType(), an);
235 }
236 if (port.element().is(OpticalDevice.class)) {
237 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
238 if (optDevice.portIs(port, OduCltPort.class)) {
239 OduCltPort odu = (OduCltPort) port;
240 return oduCltPortDescription(ptn, isup, odu.signalType(), an);
241 }
242 }
243 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200244 case OTU:
245 OtuPort otu = (OtuPort) port;
246 return new OtuPortDescription(ptn, isup, otu.signalType(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700247 default:
248 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
249 }
250 }
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700251}