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 | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame^] | 19 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkNotNull; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 22 | |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 23 | import org.onosproject.net.config.ConfigOperator; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.basics.OpticalPortConfig; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 25 | import org.onosproject.net.AnnotationKeys; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 27 | import org.onosproject.net.OtuPort; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 28 | import org.onosproject.net.OduCltPort; |
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 | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame^] | 39 | import org.onosproject.net.optical.OmsPort; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 40 | import org.onosproject.net.optical.OpticalDevice; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 41 | import 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 | */ |
| 48 | public 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 Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 77 | final long pn = Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 78 | 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 Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 84 | final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 85 | 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 SHIMIZU | b89d491 | 2015-09-09 14:37:59 -0700 | [diff] [blame] | 90 | boolean noOwrite = opc.type() == descr.type(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 91 | |
| 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 Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame^] | 107 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 114 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 115 | // 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 123 | case ODUCLT: |
| 124 | OduCltPortDescription odu = (OduCltPortDescription) descr; |
| 125 | return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa); |
| 126 | case PACKET: |
| 127 | case FIBER: |
Ayaka Koshibe | 144bab0 | 2015-10-22 12:56:59 -0700 | [diff] [blame] | 128 | case COPPER: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 129 | // TODO: it should be safe to just return descr. confirm and fix |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 130 | return new DefaultPortDescription(port, descr.isEnabled(), descr.type(), |
| 131 | descr.portSpeed(), sa); |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 132 | case OTU: |
| 133 | OtuPortDescription otu = (OtuPortDescription) descr; |
| 134 | return new OtuPortDescription(port, otu.isEnabled(), otu.signalType(), sa); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 135 | default: |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 136 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 162 | |
| 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 Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 171 | 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'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 178 | * @param port port |
| 179 | * @param isEnabled true if enabled |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 180 | * @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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 189 | final PortNumber ptn = port.number(); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 190 | final SparseAnnotations an = (SparseAnnotations) port.annotations(); |
| 191 | switch (port.type()) { |
| 192 | case OMS: |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame^] | 193 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 208 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 209 | 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 Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 225 | case ODUCLT: |
| 226 | OduCltPort odu = (OduCltPort) port; |
| 227 | return new OduCltPortDescription(ptn, isup, odu.signalType(), an); |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 228 | case OTU: |
| 229 | OtuPort otu = (OtuPort) port; |
| 230 | return new OtuPortDescription(ptn, isup, otu.signalType(), an); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 231 | default: |
| 232 | return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an); |
| 233 | } |
| 234 | } |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 235 | } |