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