Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 19 | |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 20 | import java.util.Optional; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 21 | import java.util.Set; |
| 22 | |
| 23 | import static com.google.common.base.MoreObjects.firstNonNull; |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 26 | import org.onosproject.net.config.Config; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.NetworkConfigService; |
| 28 | import org.onosproject.net.config.PortConfigOperator; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DefaultAnnotations; |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 32 | import org.onosproject.net.Port; |
| 33 | import org.onosproject.net.Port.Type; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.SparseAnnotations; |
| 36 | import org.onosproject.net.device.DefaultPortDescription; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.PortDescription; |
| 38 | import org.slf4j.Logger; |
| 39 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 40 | import com.google.common.collect.Sets; |
| 41 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Implementations of merge policies for various sources of optical port |
| 44 | * configuration information. This includes applications, provides, and network |
| 45 | * configurations. |
| 46 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 47 | public final class OpticalPortOperator implements PortConfigOperator { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 48 | |
| 49 | private static final Logger log = getLogger(OpticalPortOperator.class); |
| 50 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Port.Type this PortConfigOperator reacts on. |
| 53 | */ |
| 54 | private final Set<Port.Type> optical = Sets.immutableEnumSet(Port.Type.ODUCLT, |
| 55 | Port.Type.OMS, |
| 56 | Port.Type.OCH, |
| 57 | Port.Type.OTU, |
| 58 | Port.Type.FIBER, |
| 59 | Port.Type.PACKET); |
| 60 | |
| 61 | private NetworkConfigService networkConfigService; |
| 62 | |
| 63 | |
| 64 | public OpticalPortOperator() { |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void bindService(NetworkConfigService networkConfigService) { |
| 69 | this.networkConfigService = networkConfigService; |
| 70 | } |
| 71 | |
| 72 | private OpticalPortConfig lookupConfig(ConnectPoint cp) { |
| 73 | if (networkConfigService == null) { |
| 74 | return null; |
| 75 | } |
| 76 | return networkConfigService.getConfig(cp, OpticalPortConfig.class); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Generates a PortDescription containing fields from a PortDescription and |
| 81 | * an OpticalPortConfig. |
| 82 | * |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 83 | * @param cp {@link ConnectPoint} representing the port. |
| 84 | * @param descr input {@link PortDescription} |
| 85 | * @return Combined {@link PortDescription} |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 86 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public PortDescription combine(ConnectPoint cp, PortDescription descr) { |
| 89 | checkNotNull(cp); |
| 90 | |
| 91 | // short-circuit for non-optical ports |
| 92 | // must be removed if we need type override |
| 93 | if (descr != null && !optical.contains(descr.type())) { |
| 94 | return descr; |
| 95 | } |
| 96 | |
| 97 | OpticalPortConfig opc = lookupConfig(cp); |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 98 | if (descr == null || opc == null) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 99 | return descr; |
| 100 | } |
| 101 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 102 | PortNumber number = descr.portNumber(); |
| 103 | // handle PortNumber "name" portion |
| 104 | if (!opc.name().isEmpty()) { |
| 105 | number = PortNumber.portNumber(descr.portNumber().toLong(), opc.name()); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 108 | // handle additional annotations |
| 109 | SparseAnnotations annotations = combine(opc, descr.annotations()); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 110 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 111 | // (Future work) handle type overwrite? |
| 112 | Type type = firstNonNull(opc.type(), descr.type()); |
| 113 | if (type != descr.type()) { |
| 114 | // TODO: Do we need to be able to overwrite Port.Type? |
| 115 | log.warn("Port type overwrite requested for {}. Ignoring.", cp); |
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 | |
| 118 | return updateDescription(number, annotations, descr); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 119 | } |
| 120 | |
debmaiti | 9553ed7 | 2019-03-18 14:27:42 +0530 | [diff] [blame] | 121 | @Override |
| 122 | public PortDescription combine(ConnectPoint cp, PortDescription descr, Optional<Config> prevConf) { |
| 123 | return combine(cp, descr); |
| 124 | } |
| 125 | |
| 126 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 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 | |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 140 | if (port.exactlyEquals(descr.portNumber()) && sa.equals(descr.annotations())) { |
| 141 | // result is no-op |
| 142 | return descr; |
| 143 | } |
Yuta HIGUCHI | 2b1935d | 2018-03-01 21:41:06 -0800 | [diff] [blame] | 144 | return DefaultPortDescription.builder(descr) |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 145 | .withPortNumber(port) |
Yuta HIGUCHI | 2b1935d | 2018-03-01 21:41:06 -0800 | [diff] [blame] | 146 | .annotations(sa) |
| 147 | .build(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Generates an annotation from an existing annotation and OptcalPortConfig. |
| 152 | * |
| 153 | * @param opc the port config entity from network config |
| 154 | * @param an the annotation |
| 155 | * @return annotation combining both sources |
| 156 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 157 | private static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 158 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 159 | b.putAll(an); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 160 | if (!opc.staticPort().isEmpty()) { |
| 161 | b.set(AnnotationKeys.STATIC_PORT, opc.staticPort()); |
| 162 | } |
| 163 | if (opc.staticLambda().isPresent()) { |
| 164 | b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get())); |
| 165 | } |
| 166 | // The following may not need to be carried. |
| 167 | if (!opc.name().isEmpty()) { |
| 168 | b.set(AnnotationKeys.PORT_NAME, opc.name()); |
| 169 | } |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 170 | return b.build(); |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 171 | } |
Ayaka Koshibe | d0ab3c0 | 2015-09-04 15:43:46 -0700 | [diff] [blame] | 172 | |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 173 | |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 174 | } |