blob: f495a21f88ad6a6acbfcdb05fad9f0be3c247864 [file] [log] [blame]
Alessio Giorgetti648b5382018-02-15 18:35:45 +01001/*
2 * Copyright 2017-present Open Networking Foundation
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 */
16
17package org.onosproject.drivers.lumentum;
18
19import com.google.common.collect.Range;
alessio1bf2a632019-06-04 15:47:39 +020020import org.onosproject.drivers.odtn.impl.DeviceConnectionCache;
21import org.onosproject.net.DeviceId;
Alessio Giorgetti648b5382018-02-15 18:35:45 +010022import org.onosproject.net.PortNumber;
23import org.onosproject.net.OchSignal;
24import org.onosproject.net.behaviour.PowerConfig;
25import org.onosproject.net.driver.AbstractHandlerBehaviour;
26
27import java.util.HashSet;
28import java.util.Optional;
29import java.util.Set;
30import java.util.stream.IntStream;
31
alessio1bf2a632019-06-04 15:47:39 +020032import org.onosproject.net.flow.FlowRule;
33import org.onosproject.netconf.NetconfController;
34import org.onosproject.netconf.NetconfException;
35import org.onosproject.netconf.NetconfSession;
Alessio Giorgetti648b5382018-02-15 18:35:45 +010036import org.slf4j.Logger;
37
alessio1bf2a632019-06-04 15:47:39 +020038import static com.google.common.base.Preconditions.checkNotNull;
Alessio Giorgetti648b5382018-02-15 18:35:45 +010039import static org.slf4j.LoggerFactory.getLogger;
40
41public class LumentumPowerConfig<T> extends AbstractHandlerBehaviour
42 implements PowerConfig<T> {
43
44 // log
45 private final Logger log = getLogger(getClass());
46
47
48 @Override
Andrea Campanelladadf6402019-08-07 15:24:11 +020049 public Optional<Double> getTargetPower(PortNumber port, T component) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +010050 return Optional.ofNullable(acquireTargetPower(port, component));
51 }
52
alessio1bf2a632019-06-04 15:47:39 +020053 //Used by the ROADM app to set the "attenuation" parameter
Alessio Giorgetti648b5382018-02-15 18:35:45 +010054 @Override
Andrea Campanelladadf6402019-08-07 15:24:11 +020055 public void setTargetPower(PortNumber port, T component, double power) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +010056 if (component instanceof OchSignal) {
57 setConnectionTargetPower(port, (OchSignal) component, power);
58 } else {
59 setPortTargetPower(port, power);
60 }
61 }
62
63 @Override
Andrea Campanelladadf6402019-08-07 15:24:11 +020064 public Optional<Double> currentPower(PortNumber port, T component) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +010065 return Optional.ofNullable(acquireCurrentPower(port, component));
66 }
67
68 @Override
Andrea Campanelladadf6402019-08-07 15:24:11 +020069 public Optional<Range<Double>> getTargetPowerRange(PortNumber portNumber, T component) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +010070
71 log.debug("Lumentum getTargetPowerRange {}", portNumber);
72 //TODO automatically read if a port is input or output
73
74 Set<PortNumber> outputPorts = new HashSet<>();
75
76 //Output port on the optical-line
77 outputPorts.add(PortNumber.portNumber(3001));
78
79 //Output ports of the demux module (module=2)
80 IntStream.rangeClosed(5201, 5220)
81 .forEach(i -> outputPorts.add(PortNumber.portNumber(i)));
82
83 if (outputPorts.contains(portNumber)) {
84 return Optional.ofNullable(getTxPowerRange(portNumber, component));
85 }
86 return Optional.empty();
87 }
88
89 @Override
Andrea Campanelladadf6402019-08-07 15:24:11 +020090 public Optional<Range<Double>> getInputPowerRange(PortNumber portNumber, T component) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +010091
92 log.debug("Lumentum getInputPowerRange {}", portNumber);
93 //TODO automatically read if a port is input or output
94
95 Set<PortNumber> inputPorts = new HashSet<>();
96
97 //Input port on the optical-line
98 inputPorts.add(PortNumber.portNumber(3001));
99
100 //Input ports of the mux module (module=1)
101 IntStream.rangeClosed(4101, 4120)
102 .forEach(i -> inputPorts.add(PortNumber.portNumber(i)));
103
104 if (inputPorts.contains(portNumber)) {
105 return Optional.ofNullable(getRxPowerRange(portNumber, component));
106 }
107 return Optional.empty();
108 }
109
110 //TODO implement actual get configuration from the device
111 //This is used by ROADM application to retrieve attenuation parameter, with T instanceof OchSignal
alessio1bf2a632019-06-04 15:47:39 +0200112 //The ROADM app expresses the attenuation in 0.01 dB units
Andrea Campanelladadf6402019-08-07 15:24:11 +0200113 private Double acquireTargetPower(PortNumber port, T component) {
alessiod4a2b842019-04-30 18:43:17 +0200114 log.debug("Lumentum get port {} target power...", port);
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100115
116 if (component instanceof OchSignal) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100117
alessio1bf2a632019-06-04 15:47:39 +0200118 Set<FlowRule> rules = getConnectionCache().get(did());
119 FlowRule rule;
120
121 if (rules == null) {
122 log.error("Lumentum NETCONF fail to retrieve attenuation signal {} port {}", component, port);
Andrea Campanelladadf6402019-08-07 15:24:11 +0200123 return 0.0;
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100124 } else {
alessio1bf2a632019-06-04 15:47:39 +0200125 rule = rules.stream()
126 .filter(c -> ((LumentumFlowRule) c).getOutputPort() == port)
127 .filter(c -> ((LumentumFlowRule) c).ochSignal() == component)
128 .findFirst()
129 .orElse(null);
130 }
131
132 if (rule == null) {
133 log.error("Lumentum NETCONF fail to retrieve attenuation signal {} port {}", component, port);
Andrea Campanelladadf6402019-08-07 15:24:11 +0200134 return 0.0;
alessio1bf2a632019-06-04 15:47:39 +0200135 } else {
136 log.debug("Lumentum NETCONF on port {} attenuation {}", port,
137 (((LumentumFlowRule) rule).attenuation * 100));
Andrea Campanelladadf6402019-08-07 15:24:11 +0200138 return ((LumentumFlowRule) rule).attenuation * 100;
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100139 }
140 }
141
Andrea Campanelladadf6402019-08-07 15:24:11 +0200142 return 0.0;
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100143 }
144
145 //TODO implement actual get configuration from the device
alessio1bf2a632019-06-04 15:47:39 +0200146 //This is used by ROADM application to retrieve power parameter, with T instanceof OchSignal
Andrea Campanelladadf6402019-08-07 15:24:11 +0200147 private Double acquireCurrentPower(PortNumber port, T component) {
alessiod4a2b842019-04-30 18:43:17 +0200148 log.debug("Lumentum get port {} current power...", port);
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100149
150 if (component instanceof OchSignal) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100151
alessio1bf2a632019-06-04 15:47:39 +0200152 Set<FlowRule> rules = getConnectionCache().get(did());
153 FlowRule rule;
154
155 if (rules == null) {
156 log.error("Lumentum NETCONF fail to retrieve power signal {} port {}", component, port);
Andrea Campanelladadf6402019-08-07 15:24:11 +0200157 return 0.0;
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100158 } else {
alessio1bf2a632019-06-04 15:47:39 +0200159 rule = rules.stream()
160 .filter(c -> ((LumentumFlowRule) c).getInputPort() == port)
161 .filter(c -> ((LumentumFlowRule) c).ochSignal() == component)
162 .findFirst()
163 .orElse(null);
164 }
165
166 if (rule == null) {
167 log.error("Lumentum NETCONF fail to retrieve power signal {} port {}", component, port);
Andrea Campanelladadf6402019-08-07 15:24:11 +0200168 return 0.0;
alessio1bf2a632019-06-04 15:47:39 +0200169 } else {
170 log.debug("Lumentum NETCONF on port {} power {}", port, (((LumentumFlowRule) rule).inputPower));
Andrea Campanelladadf6402019-08-07 15:24:11 +0200171 return ((double) (((LumentumFlowRule) rule).inputPower * 100));
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100172 }
173 }
174
Andrea Campanelladadf6402019-08-07 15:24:11 +0200175 return 0.0;
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100176 }
177
178 //TODO implement actual get configuration from the device
179 //Return PowerRange -60 dBm to 60 dBm
Andrea Campanelladadf6402019-08-07 15:24:11 +0200180 private Range<Double> getTxPowerRange(PortNumber port, T component) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100181 log.debug("Get port {} tx power range...", port);
Andrea Campanelladadf6402019-08-07 15:24:11 +0200182 return Range.closed(-60.0, 60.0);
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100183 }
184
185 //TODO implement actual get configuration from the device
186 //Return PowerRange -60dBm to 60 dBm
Andrea Campanelladadf6402019-08-07 15:24:11 +0200187 private Range<Double> getRxPowerRange(PortNumber port, T component) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100188 log.debug("Get port {} rx power range...", port);
Andrea Campanelladadf6402019-08-07 15:24:11 +0200189 return Range.closed(-60.0, 60.0);
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100190 }
191
192 //TODO implement configuration on the device
193 //Nothing to do
Andrea Campanelladadf6402019-08-07 15:24:11 +0200194 private void setPortTargetPower(PortNumber port, double power) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100195 log.debug("Set port {} target power {}", port, power);
196 }
197
alessio1bf2a632019-06-04 15:47:39 +0200198 //Used by the ROADM app to set the "attenuation" parameter
Andrea Campanelladadf6402019-08-07 15:24:11 +0200199 private void setConnectionTargetPower(PortNumber port, OchSignal signal, double power) {
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100200 log.debug("Set connection target power {} ochsignal {} port {}", power, signal, port);
alessio1bf2a632019-06-04 15:47:39 +0200201
202 Set<FlowRule> rules = getConnectionCache().get(did());
203 FlowRule rule = null;
204
205 if (rules == null) {
206 log.error("Lumentum NETCONF fail to retrieve power signal {} port {}", signal, port);
207 } else {
208 rule = rules.stream()
209 .filter(c -> ((LumentumFlowRule) c).getOutputPort() == port)
210 .filter(c -> ((LumentumFlowRule) c).ochSignal() == signal)
211 .findFirst()
212 .orElse(null);
213 }
214
215 if (rule == null) {
216 log.error("Lumentum NETCONF fail to retrieve attenuation signal {} port {}", signal, port);
217 } else {
218 log.debug("Lumentum NETCONF setting attenuation {} on port {} signal {}", power, port, signal);
219
220 int moduleId = ((LumentumFlowRule) rule).getConnectionModule();
221 int connId = ((LumentumFlowRule) rule).getConnectionId();
222
223 editConnection(moduleId, connId, power);
224 }
225 }
226
227
228 private DeviceConnectionCache getConnectionCache() {
229 return DeviceConnectionCache.init();
230 }
231
232 //Following Lumentum documentation <edit-config> operation to edit connection parameter
233 //Currently only edit the "attenuation" parameter
Andrea Campanelladadf6402019-08-07 15:24:11 +0200234 private boolean editConnection(int moduleId, int connectionId, double attenuation) {
alessio1bf2a632019-06-04 15:47:39 +0200235
236 double attenuationDouble = ((double) attenuation);
237
238 StringBuilder stringBuilder = new StringBuilder();
239 stringBuilder.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + "\n");
240 stringBuilder.append("<edit-config>" + "\n");
241 stringBuilder.append("<target>" + "\n");
242 stringBuilder.append("<running/>" + "\n");
243 stringBuilder.append("</target>" + "\n");
244 stringBuilder.append("<config>" + "\n");
245 stringBuilder.append("<connections xmlns=\"http://www.lumentum.com/lumentum-ote-connection\">" + "\n");
246 stringBuilder.append("<connection>" + "\n");
247 stringBuilder.append("" +
248 "<dn>ne=1;chassis=1;card=1;module=" + moduleId + ";connection=" + connectionId + "</dn>" + "\n");
249 stringBuilder.append("<config>" + "\n");
250 stringBuilder.append("<attenuation>" + attenuationDouble + "</attenuation>" + "\n");
251 stringBuilder.append("</config>" + "\n");
252 stringBuilder.append("</connection>" + "\n");
253 stringBuilder.append("</connections>" + "\n");
254 stringBuilder.append("</config>" + "\n");
255 stringBuilder.append("</edit-config>" + "\n");
256 stringBuilder.append("</rpc>" + "\n");
257
258 log.info("Lumentum ROADM20 - edit-connection sent to device {}", did());
259 log.debug("Lumentum ROADM20 - edit-connection sent to device {} {}", did(), stringBuilder);
260
261 return editCrossConnect(stringBuilder.toString());
262 }
263
264 private boolean editCrossConnect(String xcString) {
265 NetconfSession session = getNetconfSession();
266
267 if (session == null) {
268 log.error("Lumentum NETCONF - session not found for device {}", handler().data().deviceId());
269 return false;
270 }
271
272 try {
273 return session.editConfig(xcString);
274 } catch (NetconfException e) {
275 log.error("Failed to edit the CrossConnect edid-cfg for device {}",
276 handler().data().deviceId(), e);
277 log.debug("Failed configuration {}", xcString);
278 return false;
279 }
280 }
281
282 /**
283 * Helper method to get the device id.
284 */
285 private DeviceId did() {
286 return data().deviceId();
287 }
288
289 /**
290 * Helper method to get the Netconf session.
291 */
292 private NetconfSession getNetconfSession() {
293 NetconfController controller =
294 checkNotNull(handler().get(NetconfController.class));
295 return controller.getNetconfDevice(did()).getSession();
Alessio Giorgetti648b5382018-02-15 18:35:45 +0100296 }
297}