blob: 85627371a3ebab2dc4ddf64f067788c9308d070e [file] [log] [blame]
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07003 *
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 HIGUCHIb9af6b72016-06-10 10:52:58 -070016package org.onosproject.net.optical.config;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070017
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080018import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070019import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070020import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070021import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070022import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070023
24import java.util.Set;
25
26import static com.google.common.base.MoreObjects.firstNonNull;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.config.PortConfigOperator;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070031import org.onosproject.net.AnnotationKeys;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070032import org.onosproject.net.ConnectPoint;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070033import org.onosproject.net.DefaultAnnotations;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070034import org.onosproject.net.Port;
35import org.onosproject.net.Port.Type;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070036import org.onosproject.net.PortNumber;
37import org.onosproject.net.SparseAnnotations;
38import org.onosproject.net.device.DefaultPortDescription;
39import org.onosproject.net.device.OchPortDescription;
40import org.onosproject.net.device.OduCltPortDescription;
41import org.onosproject.net.device.OmsPortDescription;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020042import org.onosproject.net.device.OtuPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070043import org.onosproject.net.device.PortDescription;
44import org.slf4j.Logger;
45
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070046import com.google.common.collect.Sets;
47
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070048/**
49 * Implementations of merge policies for various sources of optical port
50 * configuration information. This includes applications, provides, and network
51 * configurations.
52 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070053public final class OpticalPortOperator implements PortConfigOperator {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070054
55 private static final Logger log = getLogger(OpticalPortOperator.class);
56
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070057 /**
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 Koshibee39c23a2015-08-03 18:17:43 -070083 }
84
85 /**
86 * Generates a PortDescription containing fields from a PortDescription and
87 * an OpticalPortConfig.
88 *
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070089 * @param cp {@link ConnectPoint} representing the port.
90 * @param descr input {@link PortDescription}
91 * @return Combined {@link PortDescription}
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070092 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070093 @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 Koshibee39c23a2015-08-03 18:17:43 -0700104 if (opc == null) {
105 return descr;
106 }
107
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700108 PortNumber number = descr.portNumber();
109 // handle PortNumber "name" portion
110 if (!opc.name().isEmpty()) {
111 number = PortNumber.portNumber(descr.portNumber().toLong(), opc.name());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700112 }
113
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700114 // handle additional annotations
115 SparseAnnotations annotations = combine(opc, descr.annotations());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700116
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700117 // (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 Koshibee39c23a2015-08-03 18:17:43 -0700122 }
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700123
124 return updateDescription(number, annotations, descr);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700125 }
126
127 // updates a port description whose port type has not changed.
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700128 /**
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 Koshibee39c23a2015-08-03 18:17:43 -0700141 switch (descr.type()) {
142 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700143 if (descr instanceof OmsPortDescription) {
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700144 OmsPortDescription oms = (OmsPortDescription) descr;
145 return omsPortDescription(port, oms.isEnabled(), oms.minFrequency(),
146 oms.maxFrequency(), oms.grid(), sa);
147 }
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700148 break;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700149 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800150 // We might need to update lambda below with STATIC_LAMBDA.
151 if (descr instanceof OchPortDescription) {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800152 OchPortDescription och = (OchPortDescription) descr;
153 return ochPortDescription(port, och.isEnabled(), och.signalType(),
154 och.isTunable(), och.lambda(), sa);
155 }
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700156 break;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700157 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700158 if (descr instanceof OduCltPortDescription) {
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700159 OduCltPortDescription odu = (OduCltPortDescription) descr;
160 return oduCltPortDescription(port, odu.isEnabled(), odu.signalType(), sa);
161 }
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700162 break;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700163 case PACKET:
164 case FIBER:
Ayaka Koshibe144bab02015-10-22 12:56:59 -0700165 case COPPER:
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700166 break;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200167 case OTU:
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700168 if (descr instanceof OtuPortDescription) {
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700169 OtuPortDescription otu = (OtuPortDescription) descr;
170 return otuPortDescription(port, otu.isEnabled(), otu.signalType(), sa);
171 }
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700172 break;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700173 default:
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700174 log.warn("Unsupported optical port type {} - can't update", descr.type());
175 return descr;
176 }
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700177 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 Koshibee39c23a2015-08-03 18:17:43 -0700186 }
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 HIGUCHIb9af6b72016-06-10 10:52:58 -0700195 private static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700196 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700197 b.putAll(an);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700198 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 HIGUCHIb9af6b72016-06-10 10:52:58 -0700208 return b.build();
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700209 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700210
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700211
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700212}