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; |
| 19 | |
| 20 | import org.onosproject.incubator.net.config.ConfigOperator; |
| 21 | import org.onosproject.incubator.net.config.basics.OpticalPortConfig; |
| 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.SparseAnnotations; |
| 26 | import org.onosproject.net.device.DefaultPortDescription; |
| 27 | import org.onosproject.net.device.OchPortDescription; |
| 28 | import org.onosproject.net.device.OduCltPortDescription; |
| 29 | import org.onosproject.net.device.OmsPortDescription; |
| 30 | import org.onosproject.net.device.PortDescription; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | /** |
| 34 | * Implementations of merge policies for various sources of optical port |
| 35 | * configuration information. This includes applications, provides, and network |
| 36 | * configurations. |
| 37 | */ |
| 38 | public final class OpticalPortOperator implements ConfigOperator { |
| 39 | |
| 40 | private static final Logger log = getLogger(OpticalPortOperator.class); |
| 41 | |
| 42 | private OpticalPortOperator() { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Generates a PortDescription containing fields from a PortDescription and |
| 47 | * an OpticalPortConfig. |
| 48 | * |
| 49 | * @param opc the port config entity from network config |
| 50 | * @param descr a PortDescription |
| 51 | * @return PortDescription based on both sources |
| 52 | */ |
| 53 | public static PortDescription combine(OpticalPortConfig opc, PortDescription descr) { |
| 54 | if (opc == null) { |
| 55 | return descr; |
| 56 | } |
| 57 | |
| 58 | PortNumber port = descr.portNumber(); |
| 59 | final String name = opc.name(); |
| 60 | final String numName = opc.numberName(); |
| 61 | // if the description is null, or the current description port name != config name, |
| 62 | // create a new PortNumber. |
| 63 | PortNumber newPort = null; |
| 64 | if (port == null) { |
| 65 | // try to get the portNumber from the numName. |
| 66 | if (!numName.isEmpty()) { |
| 67 | final long pn = Long.valueOf(numName); |
| 68 | newPort = (!name.isEmpty()) ? PortNumber.portNumber(pn, name) : PortNumber.portNumber(pn); |
| 69 | } else { |
| 70 | // we don't have defining info (a port number value) |
| 71 | throw new RuntimeException("Possible misconfig, bailing on handling for: \n\t" + descr); |
| 72 | } |
| 73 | } else if ((!name.isEmpty()) && !name.equals(port.name())) { |
| 74 | final long pn = (numName.isEmpty()) ? port.toLong() : Long.valueOf(numName); |
| 75 | newPort = PortNumber.portNumber(pn, name); |
| 76 | } |
| 77 | |
| 78 | // Port type won't change unless we're overwriting a port completely. |
| 79 | // Watch out for overwrites to avoid class cast craziness. |
| 80 | boolean noOwrite = (opc.type() == descr.type()) ? true : false; |
| 81 | |
| 82 | SparseAnnotations sa = combine(opc, descr.annotations()); |
| 83 | if (noOwrite) { |
| 84 | return updateDescription((newPort == null) ? port : newPort, sa, descr); |
| 85 | } else { |
| 86 | // TODO: must reconstruct a different type of PortDescription. |
| 87 | log.info("Type rewrite from {} to {} required", descr.type(), opc.type()); |
| 88 | } |
| 89 | return descr; |
| 90 | } |
| 91 | |
| 92 | // updates a port description whose port type has not changed. |
| 93 | private static PortDescription updateDescription( |
| 94 | PortNumber port, SparseAnnotations sa, PortDescription descr) { |
| 95 | switch (descr.type()) { |
| 96 | case OMS: |
| 97 | OmsPortDescription oms = (OmsPortDescription) descr; |
| 98 | return new OmsPortDescription(port, oms.isEnabled(), oms.minFrequency(), |
| 99 | oms.maxFrequency(), oms.grid(), sa); |
| 100 | case OCH: |
| 101 | // We might need to update lambda below with STATIC_LAMBDA. |
| 102 | OchPortDescription och = (OchPortDescription) descr; |
| 103 | return new OchPortDescription(port, och.isEnabled(), och.signalType(), |
| 104 | och.isTunable(), och.lambda(), sa); |
| 105 | case ODUCLT: |
| 106 | OduCltPortDescription odu = (OduCltPortDescription) descr; |
| 107 | return new OduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa); |
| 108 | case PACKET: |
| 109 | case FIBER: |
| 110 | return new DefaultPortDescription(port, descr.isEnabled(), descr.type(), |
| 111 | descr.portSpeed(), sa); |
| 112 | default: |
| 113 | // this includes copper ports. |
| 114 | log.warn("Unsupported optical port type {} - can't update", descr.type()); |
| 115 | return descr; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Generates an annotation from an existing annotation and OptcalPortConfig. |
| 121 | * |
| 122 | * @param opc the port config entity from network config |
| 123 | * @param an the annotation |
| 124 | * @return annotation combining both sources |
| 125 | */ |
| 126 | public static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) { |
| 127 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
| 128 | if (!opc.staticPort().isEmpty()) { |
| 129 | b.set(AnnotationKeys.STATIC_PORT, opc.staticPort()); |
| 130 | } |
| 131 | if (opc.staticLambda().isPresent()) { |
| 132 | b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get())); |
| 133 | } |
| 134 | // The following may not need to be carried. |
| 135 | if (!opc.name().isEmpty()) { |
| 136 | b.set(AnnotationKeys.PORT_NAME, opc.name()); |
| 137 | } |
| 138 | return DefaultAnnotations.union(an, b.build()); |
| 139 | } |
| 140 | } |