blob: 14f6aeb12447a90008eb793ef37a7fe3f01c3402 [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 Yuta95d83e82016-04-26 12:13:48 -070019import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070020import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070021import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070022
Ray Milkeya4122362015-08-18 15:19:08 -070023import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070024import org.onosproject.net.config.basics.OpticalPortConfig;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070025import org.onosproject.net.AnnotationKeys;
26import org.onosproject.net.DefaultAnnotations;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020027import org.onosproject.net.OtuPort;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070028import org.onosproject.net.OduCltPort;
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 Yuta95d83e82016-04-26 12:13:48 -070039import org.onosproject.net.optical.OmsPort;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080040import org.onosproject.net.optical.OpticalDevice;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070041import org.slf4j.Logger;
42
43/**
44 * Implementations of merge policies for various sources of optical port
45 * configuration information. This includes applications, provides, and network
46 * configurations.
47 */
48public final class OpticalPortOperator implements ConfigOperator {
49
50 private static final Logger log = getLogger(OpticalPortOperator.class);
51
52 private OpticalPortOperator() {
53 }
54
55 /**
56 * Generates a PortDescription containing fields from a PortDescription and
57 * an OpticalPortConfig.
58 *
59 * @param opc the port config entity from network config
60 * @param descr a PortDescription
61 * @return PortDescription based on both sources
62 */
63 public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) {
64 if (opc == null) {
65 return descr;
66 }
67
68 PortNumber port = descr.portNumber();
69 final String name = opc.name();
70 final String numName = opc.numberName();
71 // if the description is null, or the current description port name != config name,
72 // create a new PortNumber.
73 PortNumber newPort = null;
74 if (port == null) {
75 // try to get the portNumber from the numName.
76 if (!numName.isEmpty()) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070077 final long pn = Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070078 newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn);
79 } else {
80 // we don't have defining info (a port number value)
81 throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr);
82 }
83 } else if ((!name.isEmpty()) && !name.equals(port.name())) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070084 final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070085 newPort = PortNumber.portNumber(pn, name);
86 }
87
88 // Port type won't change unless we're overwriting a port completely.
89 // Watch out for overwrites to avoid class cast craziness.
Sho SHIMIZUb89d4912015-09-09 14:37:59 -070090 boolean noOwrite = opc.type() == descr.type();
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070091
92 SparseAnnotations sa = combine(opc, descr.annotations());
93 if (noOwrite) {
94 return updateDescription((newPort == null) ? port : newPort, sa, descr);
95 } else {
96 // TODO: must reconstruct a different type of PortDescription.
97 log.info("Type rewrite from {} to {} required", descr.type(), opc.type());
98 }
99 return descr;
100 }
101
102 // updates a port description whose port type has not changed.
103 private static PortDescription updateDescription(
104 PortNumber port, SparseAnnotations sa, PortDescription descr) {
105 switch (descr.type()) {
106 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700107 if (descr instanceof OmsPortDescription) {
108 // TODO This block can go away once deprecation is complete.
109 OmsPortDescription oms = (OmsPortDescription) descr;
110 return omsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
111 oms.maxFrequency(), oms.grid(), sa);
112 }
113 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700114 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800115 // We might need to update lambda below with STATIC_LAMBDA.
116 if (descr instanceof OchPortDescription) {
117 // TODO This block can go away once deprecation is complete.
118 OchPortDescription och = (OchPortDescription) descr;
119 return ochPortDescription(port, och.isEnabled(), och.signalType(),
120 och.isTunable(), och.lambda(), sa);
121 }
122 return descr;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700123 case ODUCLT:
124 OduCltPortDescription odu = (OduCltPortDescription) descr;
125 return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
126 case PACKET:
127 case FIBER:
Ayaka Koshibe144bab02015-10-22 12:56:59 -0700128 case COPPER:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800129 // TODO: it should be safe to just return descr. confirm and fix
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700130 return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
131 descr.portSpeed(), sa);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200132 case OTU:
133 OtuPortDescription otu = (OtuPortDescription) descr;
134 return new OtuPortDescription(port, otu.isEnabled(), otu.signalType(), sa);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700135 default:
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700136 log.warn("Unsupported optical port type {} - can't update", descr.type());
137 return descr;
138 }
139 }
140
141 /**
142 * Generates an annotation from an existing annotation and OptcalPortConfig.
143 *
144 * @param opc the port config entity from network config
145 * @param an the annotation
146 * @return annotation combining both sources
147 */
148 public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
149 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
150 if (!opc.staticPort().isEmpty()) {
151 b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
152 }
153 if (opc.staticLambda().isPresent()) {
154 b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
155 }
156 // The following may not need to be carried.
157 if (!opc.name().isEmpty()) {
158 b.set(AnnotationKeys.PORT_NAME, opc.name());
159 }
160 return DefaultAnnotations.union(an, b.build());
161 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700162
163 /**
164 * Returns a description built from an existing port.
165 *
166 * @param port the device port
167 * @return a PortDescription based on the port
168 */
169 public static PortDescription descriptionOf(Port port) {
170 checkNotNull(port, "Must supply non-null Port");
Yafit Hadara9a73de2015-09-06 13:52:52 +0300171 final boolean isUp = port.isEnabled();
172 return descriptionOfPort(port, isUp);
173 }
174
175 /**
176 * Returns a description built from an existing port and reported status.
177 *
Brian O'Connor52515622015-10-09 17:03:44 -0700178 * @param port port
179 * @param isEnabled true if enabled
Yafit Hadara9a73de2015-09-06 13:52:52 +0300180 * @return a PortDescription based on the port
181 */
182 static PortDescription descriptionOf(Port port, boolean isEnabled) {
183 checkNotNull(port, "Must supply non-null Port");
184 final boolean isup = isEnabled;
185 return descriptionOfPort(port, isup);
186 }
187
188 private static PortDescription descriptionOfPort(Port port, final boolean isup) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700189 final PortNumber ptn = port.number();
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700190 final SparseAnnotations an = (SparseAnnotations) port.annotations();
191 switch (port.type()) {
192 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700193 if (port instanceof org.onosproject.net.OmsPort) {
194 // remove if-block once deprecation is complete
195 org.onosproject.net.OmsPort oms = (org.onosproject.net.OmsPort) port;
196 return omsPortDescription(ptn, isup, oms.minFrequency(),
197 oms.maxFrequency(), oms.grid(), an);
198 }
199 if (port.element().is(OpticalDevice.class)) {
200 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
201 if (optDevice.portIs(port, OmsPort.class)) {
202 OmsPort oms = optDevice.portAs(port, OmsPort.class).get();
203 return omsPortDescription(ptn, isup, oms.minFrequency(),
204 oms.maxFrequency(), oms.grid(), an);
205 }
206 }
207 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700208 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800209 if (port instanceof org.onosproject.net.OchPort) {
210 // remove if-block once old OchPort deprecation is complete
211 org.onosproject.net.OchPort och = (org.onosproject.net.OchPort) port;
212 return ochPortDescription(ptn, isup, och.signalType(),
213 och.isTunable(), och.lambda(), an);
214 }
215 if (port.element().is(OpticalDevice.class)) {
216 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
217 if (optDevice.portIs(port, OchPort.class)) {
218 OchPort och = optDevice.portAs(port, OchPort.class).get();
219 return ochPortDescription(ptn, isup, och.signalType(),
220 och.isTunable(), och.lambda(), an);
221 }
222 }
223 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
224
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700225 case ODUCLT:
226 OduCltPort odu = (OduCltPort) port;
227 return new OduCltPortDescription(ptn, isup, odu.signalType(), an);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200228 case OTU:
229 OtuPort otu = (OtuPort) port;
230 return new OtuPortDescription(ptn, isup, otu.signalType(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700231 default:
232 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
233 }
234 }
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700235}