blob: 0029fffce8467b82dfec248f973f73a5b870308f [file] [log] [blame]
Andrea Campanella3a361452019-08-02 10:17:53 +02001/*
2 * Copyright 2018-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 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
17 */
18
19package org.onosproject.drivers.odtn;
20
21import com.google.common.collect.Range;
22import org.apache.commons.configuration.HierarchicalConfiguration;
23import org.apache.commons.configuration.XMLConfiguration;
24import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
25import org.onlab.osgi.DefaultServiceDirectory;
26import org.onosproject.drivers.utilities.XmlConfigParser;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.behaviour.PowerConfig;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.driver.AbstractHandlerBehaviour;
32import org.onosproject.netconf.NetconfController;
33import org.onosproject.netconf.NetconfDevice;
34import org.onosproject.netconf.NetconfException;
35import org.onosproject.netconf.NetconfSession;
36import org.slf4j.Logger;
37
38import java.util.ArrayList;
39import java.util.List;
40import java.util.Optional;
41import java.util.concurrent.CompletableFuture;
42import java.util.concurrent.ExecutionException;
43
44import static com.google.common.base.Preconditions.checkNotNull;
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Driver Implementation of the PowerConfig for OpenConfig terminal devices.
49 *
50 */
51public class CassiniTerminalDevicePowerConfigExt<T>
52 extends AbstractHandlerBehaviour implements PowerConfig<T> {
53
54 private static final String RPC_TAG_NETCONF_BASE =
55 "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
56
57 private static final String RPC_CLOSE_TAG = "</rpc>";
58
59 private static final long NO_POWER = -50;
60
61 private static final Logger log = getLogger(CassiniTerminalDevicePowerConfigExt.class);
62
63 private ComponentType state = ComponentType.DIRECTION;
64
65 /**
66 * Returns the NetconfSession with the device for which the method was called.
67 *
68 * @param deviceId device indetifier
69 *
70 * @return The netconf session or null
71 */
72 private NetconfSession getNetconfSession(DeviceId deviceId) {
73 NetconfController controller = handler().get(NetconfController.class);
74 NetconfDevice ncdev = controller.getDevicesMap().get(deviceId);
75 if (ncdev == null) {
76 log.trace("No netconf device, returning null session");
77 return null;
78 }
79 return ncdev.getSession();
80 }
81
82 /**
83 * Get the deviceId for which the methods apply.
84 *
85 * @return The deviceId as contained in the handler data
86 */
87 private DeviceId did() {
88 return handler().data().deviceId();
89 }
90
91 /**
92 * Execute RPC request.
93 * @param session Netconf session
94 * @param message Netconf message in XML format
95 * @return XMLConfiguration object
96 */
97 private XMLConfiguration executeRpc(NetconfSession session, String message) {
98 try {
99 CompletableFuture<String> fut = session.rpc(message);
100 String rpcReply = fut.get();
101 XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply);
102 xconf.setExpressionEngine(new XPathExpressionEngine());
103 return xconf;
104 } catch (NetconfException ne) {
105 log.error("Exception on Netconf protocol: {}.", ne);
106 } catch (InterruptedException ie) {
107 log.error("Interrupted Exception: {}.", ie);
108 } catch (ExecutionException ee) {
109 log.error("Concurrent Exception while executing Netconf operation: {}.", ee);
110 }
111 return null;
112 }
113
114 /**
115 * Get the target-output-power value on specific optical-channel.
116 * @param port the port
117 * @param component the port component. It should be 'oc-name' in the Annotations of Port.
118 * 'oc-name' could be mapped to '/component/name' in openconfig yang.
119 * @return target power value
120 */
121 @Override
122 public Optional<Long> getTargetPower(PortNumber port, T component) {
123 checkState(component);
124 return state.getTargetPower(port, component);
125 }
126
127 @Override
128 public void setTargetPower(PortNumber port, T component, long power) {
129 checkState(component);
130 state.setTargetPower(port, component, power);
131 }
132
133 @Override
134 public Optional<Long> currentPower(PortNumber port, T component) {
135 checkState(component);
136 return state.currentPower(port, component);
137 }
138
139 @Override
140 public Optional<Long> currentInputPower(PortNumber port, T component) {
141 checkState(component);
142 return state.currentInputPower(port, component);
143 }
144
145 @Override
146 public Optional<Range<Long>> getTargetPowerRange(PortNumber port, T component) {
147 checkState(component);
148 return state.getTargetPowerRange(port, component);
149 }
150
151 @Override
152 public Optional<Range<Long>> getInputPowerRange(PortNumber port, T component) {
153 checkState(component);
154 return state.getInputPowerRange(port, component);
155 }
156
157 @Override
158 public List<PortNumber> getPorts(T component) {
159 checkState(component);
160 return state.getPorts(component);
161 }
162
163
164 /**
165 * Set the ComponentType to invoke proper methods for different template T.
166 * @param component the component.
167 */
168 void checkState(Object component) {
169 String clsName = component.getClass().getName();
170 switch (clsName) {
171 case "org.onosproject.net.Direction":
172 state = ComponentType.DIRECTION;
173 break;
174 case "org.onosproject.net.OchSignal":
175 state = ComponentType.OCHSIGNAL;
176 break;
177 default:
178 log.error("Cannot parse the component type {}.", clsName);
179 log.info("The component content is {}.", component.toString());
180 }
181 state.cassini = this;
182 }
183
184 /**
185 * Component type.
186 */
187 enum ComponentType {
188
189 /**
190 * Direction.
191 */
192 DIRECTION() {
193 @Override
194 public Optional<Long> getTargetPower(PortNumber port, Object component) {
195 return super.getTargetPower(port, component);
196 }
197 @Override
198 public void setTargetPower(PortNumber port, Object component, long power) {
199 super.setTargetPower(port, component, power);
200 }
201 },
202
203 /**
204 * OchSignal.
205 */
206 OCHSIGNAL() {
207 @Override
208 public Optional<Long> getTargetPower(PortNumber port, Object component) {
209 return super.getTargetPower(port, component);
210 }
211
212 @Override
213 public void setTargetPower(PortNumber port, Object component, long power) {
214 super.setTargetPower(port, component, power);
215 }
216 };
217
218
219
220 CassiniTerminalDevicePowerConfigExt cassini;
221
222 /**
223 * mirror method in the internal class.
224 * @param port port
225 * @param component component
226 * @return target power
227 */
228 Optional<Long> getTargetPower(PortNumber port, Object component) {
229 NetconfSession session = cassini.getNetconfSession(cassini.did());
230 checkNotNull(session);
231 String filter = parsePort(cassini, port, null, null);
232 StringBuilder rpcReq = new StringBuilder();
233 rpcReq.append(RPC_TAG_NETCONF_BASE)
234 .append("<get>")
235 .append("<filter type='subtree'>")
236 .append(filter)
237 .append("</filter>")
238 .append("</get>")
239 .append(RPC_CLOSE_TAG);
240 XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString());
241 try {
242 HierarchicalConfiguration config =
243 xconf.configurationAt("data/components/component/optical-channel/config");
244 long power = Float.valueOf(config.getString("target-output-power")).longValue();
245 return Optional.of(power);
246 } catch (IllegalArgumentException e) {
247 return Optional.empty();
248 }
249 }
250
251 /**
252 * mirror method in the internal class.
253 * @param port port
254 * @param component component
255 * @param power target value
256 */
257 void setTargetPower(PortNumber port, Object component, long power) {
258 NetconfSession session = cassini.getNetconfSession(cassini.did());
259 checkNotNull(session);
260 String editConfig = parsePort(cassini, port, null, power);
261 StringBuilder rpcReq = new StringBuilder();
262 rpcReq.append(RPC_TAG_NETCONF_BASE)
263 .append("<edit-config>")
264 .append("<target><running/></target>")
265 .append("<config>")
266 .append(editConfig)
267 .append("</config>")
268 .append("</edit-config>")
269 .append(RPC_CLOSE_TAG);
270 XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString());
271 // The successful reply should be "<rpc-reply ...><ok /></rpc-reply>"
272 if (!xconf.getRoot().getChild(0).getName().equals("ok")) {
273 log.error("The <edit-config> operation to set target-output-power of Port({}:{}) is failed.",
274 port.toString(), component.toString());
275 }
276 }
277
278 /**
279 * mirror method in the internal class.
280 * @param port port
281 * @param component the component.
282 * @return current output power.
283 */
284 Optional<Long> currentPower(PortNumber port, Object component) {
285 XMLConfiguration xconf = getOpticalChannelState(
286 cassini, port, "<output-power><instant/></output-power>");
287 try {
288 HierarchicalConfiguration config =
289 xconf.configurationAt("data/components/component/optical-channel/state/output-power");
290 long currentPower = Float.valueOf(config.getString("instant")).longValue();
291 return Optional.of(currentPower);
292 } catch (IllegalArgumentException e) {
293 return Optional.empty();
294 }
295 }
296
297 /**
298 * mirror method in the internal class.
299 * @param port port
300 * @param component the component
301 * @return current input power
302 */
303 Optional<Long> currentInputPower(PortNumber port, Object component) {
304 XMLConfiguration xconf = getOpticalChannelState(
305 cassini, port, "<input-power><instant/></input-power>");
306 try {
307 HierarchicalConfiguration config =
308 xconf.configurationAt("data/components/component/optical-channel/state/input-power");
309 long currentPower = Float.valueOf(config.getString("instant")).longValue();
310 return Optional.of(currentPower);
311 } catch (IllegalArgumentException e) {
312 return Optional.empty();
313 }
314 }
315
316 Optional<Range<Long>> getTargetPowerRange(PortNumber port, Object component) {
317 XMLConfiguration xconf = getOpticalChannelState(
318 cassini, port, "<target-power-range/>");
319 try {
320 HierarchicalConfiguration config =
321 xconf.configurationAt("data/components/component/optical-channel/state/target-power-range");
322 long targetMin = Float.valueOf(config.getString("min")).longValue();
323 long targetMax = Float.valueOf(config.getString("max")).longValue();
324 return Optional.of(Range.open(targetMin, targetMax));
325 } catch (IllegalArgumentException e) {
326 return Optional.empty();
327 }
328
329 }
330
331 Optional<Range<Long>> getInputPowerRange(PortNumber port, Object component) {
332 XMLConfiguration xconf = getOpticalChannelState(
333 cassini, port, "<input-power-range/>");
334 try {
335 HierarchicalConfiguration config =
336 xconf.configurationAt("data/components/component/optical-channel/state/input-power-range");
337 long inputMin = Float.valueOf(config.getString("min")).longValue();
338 long inputMax = Float.valueOf(config.getString("max")).longValue();
339 return Optional.of(Range.open(inputMin, inputMax));
340 } catch (IllegalArgumentException e) {
341 return Optional.empty();
342 }
343 }
344
345 List<PortNumber> getPorts(Object component) {
346 // FIXME
347 log.warn("Not Implemented Yet!");
348 return new ArrayList<PortNumber>();
349 }
350
351 /**
352 * Get filtered content under <optical-channel><state>.
353 * @param pc power config instance
354 * @param port the port number
355 * @param underState the filter condition
356 * @return RPC reply
357 */
358 private static XMLConfiguration getOpticalChannelState(CassiniTerminalDevicePowerConfigExt pc,
359 PortNumber port, String underState) {
360 NetconfSession session = pc.getNetconfSession(pc.did());
361 checkNotNull(session);
362 String name = ocName(pc, port);
363 StringBuilder rpcReq = new StringBuilder(RPC_TAG_NETCONF_BASE);
364 rpcReq.append("<get><filter><components xmlns=\"http://openconfig.net/yang/platform\"><component>")
365 .append("<name>").append(name).append("</name>")
366 .append("<optical-channel xmlns=\"http://openconfig.net/yang/terminal-device\">")
367 .append("<state>")
368 .append(underState)
369 .append("</state></optical-channel></component></components></filter></get>")
370 .append(RPC_CLOSE_TAG);
371 XMLConfiguration xconf = pc.executeRpc(session, rpcReq.toString());
372 return xconf;
373 }
374
375
376 /**
377 * Extract component name from portNumber's annotations.
378 * @param pc power config instance
379 * @param portNumber the port number
380 * @return the component name
381 */
382 private static String ocName(CassiniTerminalDevicePowerConfigExt pc, PortNumber portNumber) {
383 DeviceService deviceService = DefaultServiceDirectory.getService(DeviceService.class);
384 DeviceId deviceId = pc.handler().data().deviceId();
385 return deviceService.getPort(deviceId, portNumber).annotations().value("oc-name");
386 }
387
388
389
390 /**
391 * Parse filtering string from port and component.
392 * @param portNumber Port Number
393 * @param component port component (optical-channel)
394 * @param power power value set.
395 * @return filtering string in xml format
396 */
397 private static String parsePort(CassiniTerminalDevicePowerConfigExt pc, PortNumber portNumber,
398 Object component, Long power) {
399 if (component == null) {
400 String name = ocName(pc, portNumber);
401 StringBuilder sb = new StringBuilder("<components xmlns=\"http://openconfig.net/yang/platform\">");
402 sb.append("<component>").append("<name>").append(name).append("</name>");
403 if (power != null) {
404 // This is an edit-config operation.
405 sb.append("<optical-channel xmlns=\"http://openconfig.net/yang/terminal-device\">")
406 .append("<config>")
407 .append("<target-output-power>")
408 .append(power)
409 .append("</target-output-power>")
410 .append("</config>")
411 .append("</optical-channel>");
412 }
413 sb.append("</component>").append("</components>");
414 return sb.toString();
415 } else {
416 log.error("Cannot process the component {}.", component.getClass());
417 return null;
418 }
419 }
420 }
421}