Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.device.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 20 | |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 21 | import org.onosproject.net.config.ConfigOperator; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 22 | import org.onosproject.net.config.basics.OpticalPortConfig; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 23 | import org.onosproject.net.AnnotationKeys; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 25 | import org.onosproject.net.OchPort; |
| 26 | import org.onosproject.net.OduCltPort; |
| 27 | import org.onosproject.net.OmsPort; |
| 28 | import org.onosproject.net.Port; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.SparseAnnotations; |
| 31 | import org.onosproject.net.device.DefaultPortDescription; |
| 32 | import org.onosproject.net.device.OchPortDescription; |
| 33 | import org.onosproject.net.device.OduCltPortDescription; |
| 34 | import org.onosproject.net.device.OmsPortDescription; |
| 35 | import org.onosproject.net.device.PortDescription; |
| 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | /** |
| 39 | * Implementations of merge policies for various sources of optical port |
| 40 | * configuration information. This includes applications, provides, and network |
| 41 | * configurations. |
| 42 | */ |
| 43 | public final class OpticalPortOperator implements ConfigOperator { |
| 44 | |
| 45 | private static final Logger log = getLogger(OpticalPortOperator.class); |
| 46 | |
| 47 | private OpticalPortOperator() { |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Generates a PortDescription containing fields from a PortDescription and |
| 52 | * an OpticalPortConfig. |
| 53 | * |
| 54 | * @param opc the port config entity from network config |
| 55 | * @param descr a PortDescription |
| 56 | * @return PortDescription based on both sources |
| 57 | */ |
| 58 | public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) { |
| 59 | if (opc == null) { |
| 60 | return descr; |
| 61 | } |
| 62 | |
| 63 | PortNumber port = descr.portNumber(); |
| 64 | final String name = opc.name(); |
| 65 | final String numName = opc.numberName(); |
| 66 | // if the description is null, or the current description port name != config name, |
| 67 | // create a new PortNumber. |
| 68 | PortNumber newPort = null; |
| 69 | if (port == null) { |
| 70 | // try to get the portNumber from the numName. |
| 71 | if (!numName.isEmpty()) { |
HIGUCHI Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 72 | final long pn = Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 73 | newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn); |
| 74 | } else { |
| 75 | // we don't have defining info (a port number value) |
| 76 | throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr); |
| 77 | } |
| 78 | } else if ((!name.isEmpty()) && !name.equals(port.name())) { |
HIGUCHI Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 79 | final long pn = (numName.isEmpty()) ? port.toLong() : Long.parseLong(numName); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 80 | newPort = PortNumber.portNumber(pn, name); |
| 81 | } |
| 82 | |
| 83 | // Port type won't change unless we're overwriting a port completely. |
| 84 | // Watch out for overwrites to avoid class cast craziness. |
Sho SHIMIZU | b89d491 | 2015-09-09 14:37:59 -0700 | [diff] [blame] | 85 | boolean noOwrite = opc.type() == descr.type(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 86 | |
| 87 | SparseAnnotations sa = combine(opc, descr.annotations()); |
| 88 | if (noOwrite) { |
| 89 | return updateDescription((newPort == null) ? port : newPort, sa, descr); |
| 90 | } else { |
| 91 | // TODO: must reconstruct a different type of PortDescription. |
| 92 | log.info("Type rewrite from {} to {} required", descr.type(), opc.type()); |
| 93 | } |
| 94 | return descr; |
| 95 | } |
| 96 | |
| 97 | // updates a port description whose port type has not changed. |
| 98 | private static PortDescription updateDescription( |
| 99 | PortNumber port, SparseAnnotations sa, PortDescription descr) { |
| 100 | switch (descr.type()) { |
| 101 | case OMS: |
| 102 | OmsPortDescription oms = (OmsPortDescription) descr; |
| 103 | return new OmsPortDescription(port, oms.isEnabled(), oms.minFrequency(), |
| 104 | oms.maxFrequency(), oms.grid(), sa); |
| 105 | case OCH: |
| 106 | // We might need to update lambda below with STATIC_LAMBDA. |
| 107 | OchPortDescription och = (OchPortDescription) descr; |
| 108 | return new OchPortDescription(port, och.isEnabled(), och.signalType(), |
| 109 | och.isTunable(), och.lambda(), sa); |
| 110 | case ODUCLT: |
| 111 | OduCltPortDescription odu = (OduCltPortDescription) descr; |
| 112 | return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa); |
| 113 | case PACKET: |
| 114 | case FIBER: |
Ayaka Koshibe | 144bab0 | 2015-10-22 12:56:59 -0700 | [diff] [blame] | 115 | case COPPER: |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 116 | return new DefaultPortDescription(port, descr.isEnabled(), descr.type(), |
| 117 | descr.portSpeed(), sa); |
| 118 | default: |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 119 | log.warn("Unsupported optical port type {} - can't update", descr.type()); |
| 120 | return descr; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Generates an annotation from an existing annotation and OptcalPortConfig. |
| 126 | * |
| 127 | * @param opc the port config entity from network config |
| 128 | * @param an the annotation |
| 129 | * @return annotation combining both sources |
| 130 | */ |
| 131 | public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) { |
| 132 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
| 133 | if (!opc.staticPort().isEmpty()) { |
| 134 | b.set(AnnotationKeys.STATIC_PORT, opc.staticPort()); |
| 135 | } |
| 136 | if (opc.staticLambda().isPresent()) { |
| 137 | b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get())); |
| 138 | } |
| 139 | // The following may not need to be carried. |
| 140 | if (!opc.name().isEmpty()) { |
| 141 | b.set(AnnotationKeys.PORT_NAME, opc.name()); |
| 142 | } |
| 143 | return DefaultAnnotations.union(an, b.build()); |
| 144 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Returns a description built from an existing port. |
| 148 | * |
| 149 | * @param port the device port |
| 150 | * @return a PortDescription based on the port |
| 151 | */ |
| 152 | public static PortDescription descriptionOf(Port port) { |
| 153 | checkNotNull(port, "Must supply non-null Port"); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 154 | final boolean isUp = port.isEnabled(); |
| 155 | return descriptionOfPort(port, isUp); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns a description built from an existing port and reported status. |
| 160 | * |
Brian O'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 161 | * @param port port |
| 162 | * @param isEnabled true if enabled |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 163 | * @return a PortDescription based on the port |
| 164 | */ |
| 165 | static PortDescription descriptionOf(Port port, boolean isEnabled) { |
| 166 | checkNotNull(port, "Must supply non-null Port"); |
| 167 | final boolean isup = isEnabled; |
| 168 | return descriptionOfPort(port, isup); |
| 169 | } |
| 170 | |
| 171 | private static PortDescription descriptionOfPort(Port port, final boolean isup) { |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 172 | final PortNumber ptn = port.number(); |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 173 | final SparseAnnotations an = (SparseAnnotations) port.annotations(); |
| 174 | switch (port.type()) { |
| 175 | case OMS: |
| 176 | OmsPort oms = (OmsPort) port; |
| 177 | return new OmsPortDescription(ptn, isup, oms.minFrequency(), |
| 178 | oms.maxFrequency(), oms.grid(), an); |
| 179 | case OCH: |
| 180 | OchPort och = (OchPort) port; |
| 181 | return new OchPortDescription(ptn, isup, och.signalType(), |
| 182 | och.isTunable(), och.lambda(), an); |
| 183 | case ODUCLT: |
| 184 | OduCltPort odu = (OduCltPort) port; |
| 185 | return new OduCltPortDescription(ptn, isup, odu.signalType(), an); |
| 186 | default: |
| 187 | return new DefaultPortDescription(ptn, isup, port.type(), port.portSpeed(), an); |
| 188 | } |
| 189 | } |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 190 | } |