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