blob: ca720e2541ba23921353f9c205e34cb6049235c0 [file] [log] [blame]
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07001/*
2 * Copyright 2014-2015 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 */
16package org.onosproject.net.device.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070019import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070020
Ray Milkeya4122362015-08-18 15:19:08 -070021import org.onosproject.net.config.ConfigOperator;
Thomas Vachuska4998caa2015-08-26 13:28:38 -070022import org.onosproject.net.config.basics.OpticalPortConfig;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070023import org.onosproject.net.AnnotationKeys;
24import org.onosproject.net.DefaultAnnotations;
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -070025import org.onosproject.net.OchPort;
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;
38import org.slf4j.Logger;
39
40/**
41 * Implementations of merge policies for various sources of optical port
42 * configuration information. This includes applications, provides, and network
43 * configurations.
44 */
45public final class OpticalPortOperator implements ConfigOperator {
46
47 private static final Logger log = getLogger(OpticalPortOperator.class);
48
49 private OpticalPortOperator() {
50 }
51
52 /**
53 * Generates a PortDescription containing fields from a PortDescription and
54 * an OpticalPortConfig.
55 *
56 * @param opc the port config entity from network config
57 * @param descr a PortDescription
58 * @return PortDescription based on both sources
59 */
60 public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) {
61 if (opc == null) {
62 return descr;
63 }
64
65 PortNumber port = descr.portNumber();
66 final String name = opc.name();
67 final String numName = opc.numberName();
68 // if the description is null, or the current description port name != config name,
69 // create a new PortNumber.
70 PortNumber newPort = null;
71 if (port == null) {
72 // try to get the portNumber from the numName.
73 if (!numName.isEmpty()) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070074 final long pn = Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070075 newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn);
76 } else {
77 // we don't have defining info (a port number value)
78 throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr);
79 }
80 } else if ((!name.isEmpty()) && !name.equals(port.name())) {
HIGUCHI Yuta4a6a70a2015-09-04 14:42:10 -070081 final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070082 newPort = PortNumber.portNumber(pn, name);
83 }
84
85 // Port type won't change unless we're overwriting a port completely.
86 // Watch out for overwrites to avoid class cast craziness.
Sho SHIMIZUb89d4912015-09-09 14:37:59 -070087 boolean noOwrite = opc.type() == descr.type();
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070088
89 SparseAnnotations sa = combine(opc, descr.annotations());
90 if (noOwrite) {
91 return updateDescription((newPort == null) ? port : newPort, sa, descr);
92 } else {
93 // TODO: must reconstruct a different type of PortDescription.
94 log.info("Type rewrite from {} to {} required", descr.type(), opc.type());
95 }
96 return descr;
97 }
98
99 // updates a port description whose port type has not changed.
100 private static PortDescription updateDescription(
101 PortNumber port, SparseAnnotations sa, PortDescription descr) {
102 switch (descr.type()) {
103 case OMS:
104 OmsPortDescription oms = (OmsPortDescription) descr;
105 return new OmsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
106 oms.maxFrequency(), oms.grid(), sa);
107 case OCH:
108 // We might need to update lambda below with STATIC_LAMBDA.
109 OchPortDescription och = (OchPortDescription) descr;
110 return new OchPortDescription(port, och.isEnabled(), och.signalType(),
111 och.isTunable(), och.lambda(), sa);
112 case ODUCLT:
113 OduCltPortDescription odu = (OduCltPortDescription) descr;
114 return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
115 case PACKET:
116 case FIBER:
Ayaka Koshibe144bab02015-10-22 12:56:59 -0700117 case COPPER:
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700118 return new DefaultPortDescription(port, descr.isEnabled(), descr.type(),
119 descr.portSpeed(), sa);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200120 case OTU:
121 OtuPortDescription otu = (OtuPortDescription) descr;
122 return new OtuPortDescription(port, otu.isEnabled(), otu.signalType(), sa);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700123 default:
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700124 log.warn("Unsupported optical port type {} - can't update", descr.type());
125 return descr;
126 }
127 }
128
129 /**
130 * Generates an annotation from an existing annotation and OptcalPortConfig.
131 *
132 * @param opc the port config entity from network config
133 * @param an the annotation
134 * @return annotation combining both sources
135 */
136 public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
137 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
138 if (!opc.staticPort().isEmpty()) {
139 b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
140 }
141 if (opc.staticLambda().isPresent()) {
142 b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
143 }
144 // The following may not need to be carried.
145 if (!opc.name().isEmpty()) {
146 b.set(AnnotationKeys.PORT_NAME, opc.name());
147 }
148 return DefaultAnnotations.union(an, b.build());
149 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700150
151 /**
152 * Returns a description built from an existing port.
153 *
154 * @param port the device port
155 * @return a PortDescription based on the port
156 */
157 public static PortDescription descriptionOf(Port port) {
158 checkNotNull(port, "Must supply non-null Port");
Yafit Hadara9a73de2015-09-06 13:52:52 +0300159 final boolean isUp = port.isEnabled();
160 return descriptionOfPort(port, isUp);
161 }
162
163 /**
164 * Returns a description built from an existing port and reported status.
165 *
Brian O'Connor52515622015-10-09 17:03:44 -0700166 * @param port port
167 * @param isEnabled true if enabled
Yafit Hadara9a73de2015-09-06 13:52:52 +0300168 * @return a PortDescription based on the port
169 */
170 static PortDescription descriptionOf(Port port, boolean isEnabled) {
171 checkNotNull(port, "Must supply non-null Port");
172 final boolean isup = isEnabled;
173 return descriptionOfPort(port, isup);
174 }
175
176 private static PortDescription descriptionOfPort(Port port, final boolean isup) {
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700177 final PortNumber ptn = port.number();
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700178 final SparseAnnotations an = (SparseAnnotations) port.annotations();
179 switch (port.type()) {
180 case OMS:
181 OmsPort oms = (OmsPort) port;
182 return new OmsPortDescription(ptn, isup, oms.minFrequency(),
183 oms.maxFrequency(), oms.grid(), an);
184 case OCH:
185 OchPort och = (OchPort) port;
186 return new OchPortDescription(ptn, isup, och.signalType(),
187 och.isTunable(), och.lambda(), an);
188 case ODUCLT:
189 OduCltPort odu = (OduCltPort) port;
190 return new OduCltPortDescription(ptn, isup, odu.signalType(), an);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200191 case OTU:
192 OtuPort otu = (OtuPort) port;
193 return new OtuPortDescription(ptn, isup, otu.signalType(), an);
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700194 default:
195 return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an);
196 }
197 }
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700198}