blob: ee5ac99b198b8c0b786f3b28f66879db99d84e28 [file] [log] [blame]
Ayaka Koshibee39c23a2015-08-03 18:17:43 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
18import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070019
20import java.util.Set;
21
22import static com.google.common.base.MoreObjects.firstNonNull;
23import static com.google.common.base.Preconditions.checkNotNull;
24
25import org.onosproject.net.config.NetworkConfigService;
26import org.onosproject.net.config.PortConfigOperator;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070027import org.onosproject.net.AnnotationKeys;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070028import org.onosproject.net.ConnectPoint;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070029import org.onosproject.net.DefaultAnnotations;
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070030import org.onosproject.net.Port;
31import org.onosproject.net.Port.Type;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070032import org.onosproject.net.PortNumber;
33import org.onosproject.net.SparseAnnotations;
34import org.onosproject.net.device.DefaultPortDescription;
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070035import org.onosproject.net.device.PortDescription;
36import org.slf4j.Logger;
37
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070038import com.google.common.collect.Sets;
39
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070040/**
41 * Implementations of merge policies for various sources of optical port
42 * configuration information. This includes applications, provides, and network
43 * configurations.
44 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070045public final class OpticalPortOperator implements PortConfigOperator {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070046
47 private static final Logger log = getLogger(OpticalPortOperator.class);
48
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070049 /**
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 Koshibee39c23a2015-08-03 18:17:43 -070075 }
76
77 /**
78 * Generates a PortDescription containing fields from a PortDescription and
79 * an OpticalPortConfig.
80 *
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070081 * @param cp {@link ConnectPoint} representing the port.
82 * @param descr input {@link PortDescription}
83 * @return Combined {@link PortDescription}
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070084 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070085 @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 Milkey74e59132018-01-17 15:24:52 -080096 if (descr == null || opc == null) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070097 return descr;
98 }
99
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700100 PortNumber number = descr.portNumber();
101 // handle PortNumber "name" portion
102 if (!opc.name().isEmpty()) {
103 number = PortNumber.portNumber(descr.portNumber().toLong(), opc.name());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700104 }
105
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700106 // handle additional annotations
107 SparseAnnotations annotations = combine(opc, descr.annotations());
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700108
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700109 // (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 Koshibee39c23a2015-08-03 18:17:43 -0700114 }
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700115
116 return updateDescription(number, annotations, descr);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700117 }
118
119 // updates a port description whose port type has not changed.
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -0700120 /**
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 Yutacc10558d2016-06-03 14:27:05 -0700132 if (port.exactlyEquals(descr.portNumber()) && sa.equals(descr.annotations())) {
133 // result is no-op
134 return descr;
135 }
136 return new DefaultPortDescription(port,
137 descr.isEnabled(),
138 descr.type(),
139 descr.portSpeed(),
140 sa);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700141 }
142
143 /**
144 * Generates an annotation from an existing annotation and OptcalPortConfig.
145 *
146 * @param opc the port config entity from network config
147 * @param an the annotation
148 * @return annotation combining both sources
149 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700150 private static SparseAnnotations combine(OpticalPortConfig opc, SparseAnnotations an) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700151 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700152 b.putAll(an);
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700153 if (!opc.staticPort().isEmpty()) {
154 b.set(AnnotationKeys.STATIC_PORT, opc.staticPort());
155 }
156 if (opc.staticLambda().isPresent()) {
157 b.set(AnnotationKeys.STATIC_LAMBDA, String.valueOf(opc.staticLambda().get()));
158 }
159 // The following may not need to be carried.
160 if (!opc.name().isEmpty()) {
161 b.set(AnnotationKeys.PORT_NAME, opc.name());
162 }
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700163 return b.build();
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700164 }
Ayaka Koshibed0ab3c02015-09-04 15:43:46 -0700165
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -0700166
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700167}