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; |
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; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.SparseAnnotations; |
| 29 | import org.onosproject.net.device.DefaultPortDescription; |
| 30 | import org.onosproject.net.device.OchPortDescription; |
| 31 | import org.onosproject.net.device.OduCltPortDescription; |
| 32 | import org.onosproject.net.device.OmsPortDescription; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 33 | import org.onosproject.net.device.OtuPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.PortDescription; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | /** |
| 38 | * Implementations of merge policies for various sources of optical port |
| 39 | * configuration information. This includes applications, provides, and network |
| 40 | * configurations. |
| 41 | */ |
| 42 | public final class OpticalPortOperator implements ConfigOperator { |
| 43 | |
| 44 | private static final Logger log = getLogger(OpticalPortOperator.class); |
| 45 | |
| 46 | private OpticalPortOperator() { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Generates a PortDescription containing fields from a PortDescription and |
| 51 | * an OpticalPortConfig. |
| 52 | * |
| 53 | * @param opc the port config entity from network config |
| 54 | * @param descr a PortDescription |
| 55 | * @return PortDescription based on both sources |
| 56 | */ |
| 57 | public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) { |
| 58 | if (opc == null) { |
| 59 | return descr; |
| 60 | } |
| 61 | |
| 62 | PortNumber port = descr.portNumber(); |
| 63 | final String name = opc.name(); |
| 64 | final String numName = opc.numberName(); |
| 65 | // if the description is null, or the current description port name != config name, |
| 66 | // create a new PortNumber. |
| 67 | PortNumber newPort = null; |
| 68 | if (port == null) { |
| 69 | // try to get the portNumber from the numName. |
| 70 | if (!numName.isEmpty()) { |
HIGUCHI Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 71 | final long pn = Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 72 | newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn); |
| 73 | } else { |
| 74 | // we don't have defining info (a port number value) |
| 75 | throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr); |
| 76 | } |
| 77 | } else if ((!name.isEmpty()) && !name.equals(port.name())) { |
HIGUCHI Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 78 | final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 79 | newPort = PortNumber.portNumber(pn, name); |
| 80 | } |
| 81 | |
| 82 | // Port type won't change unless we're overwriting a port completely. |
| 83 | // Watch out for overwrites to avoid class cast craziness. |
Sho SHIMIZU | b89d491 | 2015-09-09 14:37:59 -0700 | [diff] [blame] | 84 | boolean noOwrite = opc.type() == descr.type(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 85 | |
| 86 | SparseAnnotations sa = combine(opc, descr.annotations()); |
| 87 | if (noOwrite) { |
| 88 | return updateDescription((newPort == null) ? port : newPort, sa, descr); |
| 89 | } else { |
| 90 | // TODO: must reconstruct a different type of PortDescription. |
| 91 | log.info("Type rewrite from {} to {} required", descr.type(), opc.type()); |
| 92 | } |
| 93 | return descr; |
| 94 | } |
| 95 | |
| 96 | // updates a port description whose port type has not changed. |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Updates {@link PortDescription} using specified number and annotations. |
| 99 | * |
| 100 | * @param port {@link PortNumber} to use in updated description |
| 101 | * @param sa annotations to use in updated description |
| 102 | * @param descr base {@link PortDescription} |
| 103 | * @return updated {@link PortDescription} |
| 104 | */ |
| 105 | private static PortDescription updateDescription(PortNumber port, |
| 106 | SparseAnnotations sa, |
| 107 | PortDescription descr) { |
| 108 | |
| 109 | // TODO This switch can go away once deprecation is complete. |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 110 | switch (descr.type()) { |
| 111 | case OMS: |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 112 | if (descr instanceof OmsPortDescription) { |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 113 | OmsPortDescription oms = (OmsPortDescription) descr; |
| 114 | return omsPortDescription(port, oms.isEnabled(), oms.minFrequency(), |
| 115 | oms.maxFrequency(), oms.grid(), sa); |
| 116 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 117 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 118 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 119 | // We might need to update lambda below with STATIC_LAMBDA. |
| 120 | if (descr instanceof OchPortDescription) { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 121 | OchPortDescription och = (OchPortDescription) descr; |
| 122 | return ochPortDescription(port, och.isEnabled(), och.signalType(), |
| 123 | och.isTunable(), och.lambda(), sa); |
| 124 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 125 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 126 | case ODUCLT: |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 127 | if (descr instanceof OduCltPortDescription) { |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 128 | OduCltPortDescription odu = (OduCltPortDescription) descr; |
| 129 | return oduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa); |
| 130 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 131 | break; |
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 | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 135 | break; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 136 | case OTU: |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 137 | if (descr instanceof OtuPortDescription) { |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 138 | OtuPortDescription otu = (OtuPortDescription) descr; |
| 139 | return otuPortDescription(port, otu.isEnabled(), otu.signalType(), sa); |
| 140 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 141 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 142 | default: |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 143 | log.warn("Unsupported optical port type {} - can't update", descr.type()); |
| 144 | return descr; |
| 145 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 146 | if (port.exactlyEquals(descr.portNumber()) && sa.equals(descr.annotations())) { |
| 147 | // result is no-op |
| 148 | return descr; |
| 149 | } |
| 150 | return new DefaultPortDescription(port, |
| 151 | descr.isEnabled(), |
| 152 | descr.type(), |
| 153 | descr.portSpeed(), |
| 154 | sa); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Generates an annotation from an existing annotation and OptcalPortConfig. |
| 159 | * |
| 160 | * @param opc the port config entity from network config |
| 161 | * @param an the annotation |
| 162 | * @return annotation combining both sources |
| 163 | */ |
| 164 | public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) { |
| 165 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
| 166 | if (!opc.staticPort().isEmpty()) { |
| 167 | b.set(AnnotationKeys.STATIC_PORT, opc.staticPort()); |
| 168 | } |
| 169 | if (opc.staticLambda().isPresent()) { |
| 170 | b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get())); |
| 171 | } |
| 172 | // The following may not need to be carried. |
| 173 | if (!opc.name().isEmpty()) { |
| 174 | b.set(AnnotationKeys.PORT_NAME, opc.name()); |
| 175 | } |
| 176 | return DefaultAnnotations.union(an, b.build()); |
| 177 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 178 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 179 | } |