Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-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 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.config; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 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; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.firstNonNull; |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.config.PortConfigOperator; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 31 | import org.onosproject.net.AnnotationKeys; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Port; |
| 35 | import org.onosproject.net.Port.Type; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.SparseAnnotations; |
| 38 | import org.onosproject.net.device.DefaultPortDescription; |
| 39 | import org.onosproject.net.device.OchPortDescription; |
| 40 | import org.onosproject.net.device.OduCltPortDescription; |
| 41 | import org.onosproject.net.device.OmsPortDescription; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 42 | import org.onosproject.net.device.OtuPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 43 | import org.onosproject.net.device.PortDescription; |
| 44 | import org.slf4j.Logger; |
| 45 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 46 | import com.google.common.collect.Sets; |
| 47 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Implementations of merge policies for various sources of optical port |
| 50 | * configuration information. This includes applications, provides, and network |
| 51 | * configurations. |
| 52 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 53 | public final class OpticalPortOperator implements PortConfigOperator { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 54 | |
| 55 | private static final Logger log = getLogger(OpticalPortOperator.class); |
| 56 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Port.Type this PortConfigOperator reacts on. |
| 59 | */ |
| 60 | private final Set<Port.Type> optical = Sets.immutableEnumSet(Port.Type.ODUCLT, |
| 61 | Port.Type.OMS, |
| 62 | Port.Type.OCH, |
| 63 | Port.Type.OTU, |
| 64 | Port.Type.FIBER, |
| 65 | Port.Type.PACKET); |
| 66 | |
| 67 | private NetworkConfigService networkConfigService; |
| 68 | |
| 69 | |
| 70 | public OpticalPortOperator() { |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void bindService(NetworkConfigService networkConfigService) { |
| 75 | this.networkConfigService = networkConfigService; |
| 76 | } |
| 77 | |
| 78 | private OpticalPortConfig lookupConfig(ConnectPoint cp) { |
| 79 | if (networkConfigService == null) { |
| 80 | return null; |
| 81 | } |
| 82 | return networkConfigService.getConfig(cp, OpticalPortConfig.class); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Generates a PortDescription containing fields from a PortDescription and |
| 87 | * an OpticalPortConfig. |
| 88 | * |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 89 | * @param cp {@link ConnectPoint} representing the port. |
| 90 | * @param descr input {@link PortDescription} |
| 91 | * @return Combined {@link PortDescription} |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 92 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public PortDescription combine(ConnectPoint cp, PortDescription descr) { |
| 95 | checkNotNull(cp); |
| 96 | |
| 97 | // short-circuit for non-optical ports |
| 98 | // must be removed if we need type override |
| 99 | if (descr != null && !optical.contains(descr.type())) { |
| 100 | return descr; |
| 101 | } |
| 102 | |
| 103 | OpticalPortConfig opc = lookupConfig(cp); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 104 | if (opc == null) { |
| 105 | return descr; |
| 106 | } |
| 107 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 108 | PortNumber number = descr.portNumber(); |
| 109 | // handle PortNumber "name" portion |
| 110 | if (!opc.name().isEmpty()) { |
| 111 | number = PortNumber.portNumber(descr.portNumber().toLong(), opc.name()); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 114 | // handle additional annotations |
| 115 | SparseAnnotations annotations = combine(opc, descr.annotations()); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 116 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 117 | // (Future work) handle type overwrite? |
| 118 | Type type = firstNonNull(opc.type(), descr.type()); |
| 119 | if (type != descr.type()) { |
| 120 | // TODO: Do we need to be able to overwrite Port.Type? |
| 121 | log.warn("Port type overwrite requested for {}. Ignoring.", cp); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 122 | } |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 123 | |
| 124 | return updateDescription(number, annotations, descr); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // updates a port description whose port type has not changed. |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Updates {@link PortDescription} using specified number and annotations. |
| 130 | * |
| 131 | * @param port {@link PortNumber} to use in updated description |
| 132 | * @param sa annotations to use in updated description |
| 133 | * @param descr base {@link PortDescription} |
| 134 | * @return updated {@link PortDescription} |
| 135 | */ |
| 136 | private static PortDescription updateDescription(PortNumber port, |
| 137 | SparseAnnotations sa, |
| 138 | PortDescription descr) { |
| 139 | |
| 140 | // TODO This switch can go away once deprecation is complete. |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 141 | switch (descr.type()) { |
| 142 | case OMS: |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 143 | if (descr instanceof OmsPortDescription) { |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 144 | OmsPortDescription oms = (OmsPortDescription) descr; |
| 145 | return omsPortDescription(port, oms.isEnabled(), oms.minFrequency(), |
| 146 | oms.maxFrequency(), oms.grid(), sa); |
| 147 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 148 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 149 | case OCH: |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 150 | // We might need to update lambda below with STATIC_LAMBDA. |
| 151 | if (descr instanceof OchPortDescription) { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 152 | OchPortDescription och = (OchPortDescription) descr; |
| 153 | return ochPortDescription(port, och.isEnabled(), och.signalType(), |
| 154 | och.isTunable(), och.lambda(), sa); |
| 155 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 156 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 157 | case ODUCLT: |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 158 | if (descr instanceof OduCltPortDescription) { |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 159 | OduCltPortDescription odu = (OduCltPortDescription) descr; |
| 160 | return oduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa); |
| 161 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 162 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 163 | case PACKET: |
| 164 | case FIBER: |
Ayaka Koshibe | 144bab0 | 2015-10-22 12:56:59 -0700 | [diff] [blame] | 165 | case COPPER: |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 166 | break; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 167 | case OTU: |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 168 | if (descr instanceof OtuPortDescription) { |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 169 | OtuPortDescription otu = (OtuPortDescription) descr; |
| 170 | return otuPortDescription(port, otu.isEnabled(), otu.signalType(), sa); |
| 171 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 172 | break; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 173 | default: |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 174 | log.warn("Unsupported optical port type {} - can't update", descr.type()); |
| 175 | return descr; |
| 176 | } |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 177 | if (port.exactlyEquals(descr.portNumber()) && sa.equals(descr.annotations())) { |
| 178 | // result is no-op |
| 179 | return descr; |
| 180 | } |
| 181 | return new DefaultPortDescription(port, |
| 182 | descr.isEnabled(), |
| 183 | descr.type(), |
| 184 | descr.portSpeed(), |
| 185 | sa); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Generates an annotation from an existing annotation and OptcalPortConfig. |
| 190 | * |
| 191 | * @param opc the port config entity from network config |
| 192 | * @param an the annotation |
| 193 | * @return annotation combining both sources |
| 194 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 195 | private static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 196 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 197 | b.putAll(an); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 198 | if (!opc.staticPort().isEmpty()) { |
| 199 | b.set(AnnotationKeys.STATIC_PORT, opc.staticPort()); |
| 200 | } |
| 201 | if (opc.staticLambda().isPresent()) { |
| 202 | b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get())); |
| 203 | } |
| 204 | // The following may not need to be carried. |
| 205 | if (!opc.name().isEmpty()) { |
| 206 | b.set(AnnotationKeys.PORT_NAME, opc.name()); |
| 207 | } |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 208 | return b.build(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 209 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 210 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 211 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 212 | } |