Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.net.device.impl; |
| 17 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 18 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 19 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 20 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 21 | import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 22 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 24 | |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.ConfigOperator; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 26 | import org.onosproject.net.config.basics.OpticalPortConfig; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 27 | import org.onosproject.net.AnnotationKeys; |
| 28 | import org.onosproject.net.DefaultAnnotations; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Port; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.SparseAnnotations; |
| 32 | import org.onosproject.net.device.DefaultPortDescription; |
| 33 | import org.onosproject.net.device.OchPortDescription; |
| 34 | import org.onosproject.net.device.OduCltPortDescription; |
| 35 | import org.onosproject.net.device.OmsPortDescription; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 36 | import org.onosproject.net.device.OtuPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.PortDescription; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 38 | import org.onosproject.net.optical.OchPort; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 39 | import org.onosproject.net.optical.OduCltPort; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 40 | import org.onosproject.net.optical.OmsPort; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 41 | import org.onosproject.net.optical.OpticalDevice; |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 42 | import org.onosproject.net.optical.OtuPort; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 43 | import 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 | */ |
| 50 | public 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 Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 79 | final long pn = Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 80 | 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 Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 86 | final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 87 | 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 SHIMIZU | b89d491 | 2015-09-09 14:37:59 -0700 | [diff] [blame] | 92 | boolean noOwrite = opc.type() == descr.type(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 93 | |
| 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 Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 109 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 116 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 117 | // 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 125 | case ODUCLT: |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 126 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 132 | case PACKET: |
| 133 | case FIBER: |
Ayaka Koshibe | 144bab0 | 2015-10-22 12:56:59 -0700 | [diff] [blame] | 134 | case COPPER: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 135 | // TODO: it should be safe to just return descr. confirm and fix |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 136 | return new DefaultPortDescription(port, descr.isEnabled(), descr.type(), |
| 137 | descr.portSpeed(), sa); |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 138 | case OTU: |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 139 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 145 | default: |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 146 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 172 | |
| 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 Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 181 | 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'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 188 | * @param port port |
| 189 | * @param isEnabled true if enabled |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 190 | * @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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 199 | final PortNumber ptn = port.number(); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 200 | final SparseAnnotations an = (SparseAnnotations) port.annotations(); |
| 201 | switch (port.type()) { |
| 202 | case OMS: |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 203 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 218 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 219 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 235 | case ODUCLT: |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 236 | 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 Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 249 | case OTU: |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 250 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 263 | default: |
| 264 | return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an); |
| 265 | } |
| 266 | } |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 267 | } |