-
Notifications
You must be signed in to change notification settings - Fork 0
/
FujitsuPowerConfig.java
528 lines (481 loc) · 20.5 KB
/
FujitsuPowerConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* Copyright 2022-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This work was partially supported by EC H2020 project B5G-OPEN(101016663).
*/
package org.onosproject.drivers.odtn;
import com.google.common.collect.Range;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
import org.onlab.osgi.DefaultServiceDirectory;
import org.onosproject.drivers.utilities.XmlConfigParser;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.behaviour.PowerConfig;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.driver.AbstractHandlerBehaviour;
import org.onosproject.netconf.*;
import org.slf4j.Logger;
import java.io.StringWriter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
public class FujitsuPowerConfig<T> extends AbstractHandlerBehaviour implements PowerConfig<T> {
public static final String RPC_TAG_NETCONF_BASE =
"<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
public static final String RPC_CLOSE_TAG = "</rpc>";
public static final String TARGET_POWER = "TargetPower";
public static final String CURRENT_POWER = "CurrentPower";
public static final String CURRENT_INPUT_POWER = "CurrentInputPower";
public static final Logger log = getLogger(FujitsuPowerConfig.class);
public ComponentType state = ComponentType.DIRECTION;
/**
* Returns the NetconfSession with the device for which the method was called.
*
* @param deviceId device indetifier
* @param userName username to access the device
* @param passwd password to access the device
* @return The netconf session or null
*/
public NetconfSession getNetconfSession(DeviceId deviceId, String userName, String passwd) {
NetconfController controller = handler().get(NetconfController.class);
NetconfDevice ncdev = controller.getDevicesMap().get(deviceId);
if (ncdev == null) {
log.trace("No netconf device, returning null session");
return null;
}
return ncdev.getSession();
}
/**
* Get the deviceId for which the methods apply.
*
* @return The deviceId as contained in the handler data
*/
public DeviceId did() {
return handler().data().deviceId();
}
/**
* Execute RPC request.
*
* @param session Netconf session
* @param message Netconf message in XML format
* @return XMLConfiguration object
*/
public XMLConfiguration executeRpc(NetconfSession session, String message) {
try {
if (log.isDebugEnabled()) {
try {
StringWriter stringWriter = new StringWriter();
XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(message);
xconf.setExpressionEngine(new XPathExpressionEngine());
xconf.save(stringWriter);
log.debug("Request {}", stringWriter.toString());
} catch (ConfigurationException e) {
log.error("XML Config Exception ", e);
}
}
CompletableFuture<String> fut = session.rpc(message);
String rpcReply = fut.get();
XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply);
xconf.setExpressionEngine(new XPathExpressionEngine());
if (log.isDebugEnabled()) {
try {
StringWriter stringWriter = new StringWriter();
xconf.save(stringWriter);
log.debug("Response {}", stringWriter.toString());
} catch (ConfigurationException e) {
log.error("XML Config Exception ", e);
}
}
return xconf;
} catch (NetconfException ne) {
log.error("Exception on Netconf protocol: {}.", ne);
} catch (InterruptedException ie) {
log.error("Interrupted Exception: {}.", ie);
} catch (ExecutionException ee) {
log.error("Concurrent Exception while executing Netconf operation: {}.", ee);
}
return null;
}
/**
* Construct a rpc target power message.
*
* @param filter to build rpc
* @return RPC payload
*/
public StringBuilder getTargetPowerRequestRpc(String filter) {
StringBuilder rpc = new StringBuilder();
rpc.append("<get-config>")
.append("<source>")
.append("<" + DatastoreId.RUNNING + "/>")
.append("</source>")
.append("<filter type='subtree'>")
.append(filter)
.append("</filter>")
.append("</get-config>");
return rpc;
}
/**
* Construct a rpc target power message.
*
* @return RPC payload
*/
public DatastoreId getDataStoreId() {
return DatastoreId.CANDIDATE;
}
/**
* Construct a rpc target power message.
*
* @param name for optical channel name
* @param underState to build rpc for setting configuration
* @return RPC payload
*/
public StringBuilder getOpticalChannelStateRequestRpc(String name, String underState) {
StringBuilder rpc = new StringBuilder();
rpc.append("<name>").append(name).append("</name>")
.append("<optical-channel xmlns=\"http://openconfig.net/yang/terminal-device\">")
.append("<state>")
.append(underState)
.append("</state></optical-channel></component></components></filter></get>");
return rpc;
}
/**
* Construct a rpc target power message.
*
* @param name for optical channel name
* @param power to build rpc for setting configuration
* @return RPC payload
*/
public StringBuilder parsePortRequestRpc(Double power, String name) {
StringBuilder rpc = new StringBuilder();
rpc.append("<component>").append("<name>").append(name).append("</name>").
append("<config>").append("<name>").append(name).append("</name>").
append("</config>").
append("<optical-channel xmlns=\"http://openconfig.net/yang/terminal-device\">")
.append("<config>");
if(power!=null) {
rpc.append("<target-output-power>")
.append(power)
.append("</target-output-power>");
}
rpc.append("</config>")
.append("</optical-channel>");
return rpc;
}
/**
* Get the target-output-power value on specific optical-channel.
*
* @param port the port
* @param component the port component. It should be 'oc-name' in the Annotations of Port.
* 'oc-name' could be mapped to '/component/name' in openconfig yang.
* @return target power value
*/
@Override
public Optional<Double> getTargetPower(PortNumber port, T component) {
checkState(component);
return state.getTargetPower(port, component);
}
@Override
public void setTargetPower(PortNumber port, T component, double power) {
checkState(component);
state.setTargetPower(port, component, power);
}
@Override
public Optional<Double> currentPower(PortNumber port, T component) {
checkState(component);
return state.currentPower(port, component);
}
@Override
public Optional<Double> currentInputPower(PortNumber port, T component) {
checkState(component);
return state.currentInputPower(port, component);
}
@Override
public Optional<Range<Double>> getTargetPowerRange(PortNumber port, T component) {
double targetMax = 1.0;
double targetMin = -5;
//return state.getTargetPowerRange(port, component);
return Optional.of(Range.open(targetMin,targetMax));
}
@Override
public Optional<Range<Double>> getInputPowerRange(PortNumber port, T component) {
double targetMax = 1.0;
double targetMin = -30;
return Optional.of(Range.open(targetMin,targetMax));
//return state.getInputPowerRange(port, component);
}
@Override
public List<PortNumber> getPorts(T component) {
checkState(component);
return state.getPorts(component);
}
/**
* Set the ComponentType to invoke proper methods for different template T.
*
* @param component the component.
*/
public void checkState(Object component) {
String clsName = component.getClass().getName();
switch (clsName) {
case "org.onosproject.net.Direction":
state = FujitsuPowerConfig.ComponentType.DIRECTION;
break;
case "org.onosproject.net.OchSignal":
state = FujitsuPowerConfig.ComponentType.OCHSIGNAL;
break;
default:
log.error("Cannot parse the component type {}.", clsName);
log.info("The component content is {}.", component.toString());
}
state.driver = this;
}
/**
*
* @param param the config parameter.
* @return array of string
*/
public Map<String, String> buildRpcString(String param) {
Map<String, String> rpcMap = new HashMap<String, String>();
switch (param) {
case TARGET_POWER:
rpcMap.put("TARGET_OUTPUT_PATH", "data/components/component/optical-channel/config");
rpcMap.put("TARGET_OUTPUT_LEAF", "target-output-power");
case CURRENT_POWER:
rpcMap.put("CURRENT_POWER_PATH", "data/components/component/optical-channel/state/output-power");
rpcMap.put("CURRENT_POWER_ROUTE", "<output-power><instant/></output-power>");
rpcMap.put("CURRENT_POWER_LEAF", "instant");
default:
rpcMap.put("CURRENT_INPUT_POWER_PATH", "data/components/component/optical-channel/state/input-power");
rpcMap.put("CURRENT_INPUT_POWER_ROUTE", "<input-power><instant/></input-power>");
rpcMap.put("CURRENT_INPUT_POWER_LEAF", "instant");
}
return rpcMap;
}
/**
* Component type.
*/
public enum ComponentType {
/**
* Direction.
*/
DIRECTION() {
@Override
public Optional<Double> getTargetPower(PortNumber port, Object component) {
return super.getTargetPower(port, component);
}
@Override
public void setTargetPower(PortNumber port, Object component, double power) {
super.setTargetPower(port, component, power);
}
},
/**
* OchSignal.
*/
OCHSIGNAL() {
@Override
public Optional<Double> getTargetPower(PortNumber port, Object component) {
return super.getTargetPower(port, component);
}
@Override
public void setTargetPower(PortNumber port, Object component, double power) {
super.setTargetPower(port, component, power);
}
};
public FujitsuPowerConfig driver;
/**
* mirror method in the internal class.
*
* @param port port
* @param component component
* @return target power
*/
public Optional<Double> getTargetPower(PortNumber port, Object component) {
NetconfSession session = driver.getNetconfSession(driver.did(), "", "");
checkNotNull(session);
String filter = parsePort(driver, port, null, null);
StringBuilder rpcReq = new StringBuilder();
rpcReq.append(RPC_TAG_NETCONF_BASE)
.append(driver.getTargetPowerRequestRpc(filter))
.append(RPC_CLOSE_TAG);
XMLConfiguration xconf = driver.executeRpc(session, rpcReq.toString());
if (xconf == null) {
log.error("Error in executingRpc");
return Optional.empty();
}
try {
Map<String, String> rpcMap = driver.buildRpcString(TARGET_POWER);
String configString = rpcMap.get("TARGET_OUTPUT_PATH"),
paramStr = rpcMap.get("TARGET_OUTPUT_LEAF");
HierarchicalConfiguration config =
xconf.configurationAt(configString);
if (config == null || config.getString(paramStr) == null) {
return Optional.empty();
}
double power = Float.valueOf(config.getString(paramStr)).doubleValue();
return Optional.of(power);
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
/**
* mirror method in the internal class.
*
* @param port port
* @param component component
* @param power target value
*/
public void setTargetPower(PortNumber port, Object component, double power) {
NetconfSession session = driver.getNetconfSession(driver.did(), "", "");
checkNotNull(session);
String editConfig = parsePort(driver, port, null, power);;
StringBuilder rpcReq = new StringBuilder();
rpcReq.append(RPC_TAG_NETCONF_BASE)
.append("<edit-config>")
.append("<target><" + driver.getDataStoreId() + "/></target>")
.append("<config>")
.append(editConfig)
.append("</config>")
.append("</edit-config>")
.append(RPC_CLOSE_TAG);
log.info("Setting power {}", rpcReq.toString());
XMLConfiguration xconf = driver.executeRpc(session, rpcReq.toString());
// The successful reply should be "<rpc-reply ...><ok /></rpc-reply>"
if (!xconf.getRoot().getChild(0).getName().equals("ok")) {
log.error("The <edit-config> operation to set target-output-power of Port({}:{}) is failed.",
port.toString(), component.toString());
}
try {
session.commit();
} catch (NetconfException e) {
log.error("error committing channel power", e);
}
}
/**
* mirror method in the internal class.
*
* @param port port
* @param component the component.
* @return current output power.
*/
public Optional<Double> currentPower(PortNumber port, Object component) {
Map<String, String> rpcMap = driver.buildRpcString(CURRENT_POWER);
String configString = rpcMap.get("CURRENT_POWER_PATH"),
queryStr = rpcMap.get("CURRENT_POWER_ROUTE"),
paramStr = rpcMap.get("CURRENT_POWER_LEAF");
XMLConfiguration xconf = getOpticalChannelState(
driver, port, queryStr);
try {
HierarchicalConfiguration config =
xconf.configurationAt(configString);
if (config == null || config.getString(paramStr) == null) {
return Optional.empty();
}
double currentPower = Float.valueOf(config.getString(paramStr)).doubleValue();
return Optional.of(currentPower);
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
/**
* mirror method in the internal class.
*
* @param port port
* @param component the component
* @return current input power
*/
public Optional<Double> currentInputPower(PortNumber port, Object component) {
Map<String, String> rpcMap = driver.buildRpcString(CURRENT_INPUT_POWER);
String configString = rpcMap.get("CURRENT_INPUT_POWER_PATH"),
queryStr = rpcMap.get("CURRENT_INPUT_POWER_ROUTE"),
paramStr = rpcMap.get("CURRENT_INPUT_POWER_LEAF");
XMLConfiguration xconf = getOpticalChannelState(
driver, port, queryStr);
try {
HierarchicalConfiguration config =
xconf.configurationAt(configString);
if (config == null || config.getString(paramStr) == null) {
return Optional.empty();
}
double currentPower = Float.valueOf(config.getString(paramStr)).doubleValue();
return Optional.of(currentPower);
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
public List<PortNumber> getPorts(Object component) {
// FIXME
log.warn("Not Implemented Yet!");
return new ArrayList<PortNumber>();
}
/**
* Get filtered content under <optical-channel><state>.
*
* @param pc power config instance
* @param port the port number
* @param underState the filter condition
* @return RPC reply
*/
public static XMLConfiguration getOpticalChannelState(FujitsuPowerConfig pc,
PortNumber port, String underState) {
NetconfSession session = pc.getNetconfSession(pc.did(), "", "");
checkNotNull(session);
String name = FujitsuTerminalDeviceDiscovery.mapPortNumberToFujitsuName(port);
StringBuilder rpcReq = new StringBuilder(RPC_TAG_NETCONF_BASE);
rpcReq.append("<get><filter><components xmlns=\"http://openconfig.net/yang/platform\"><component>")
.append(pc.getOpticalChannelStateRequestRpc(name, underState))
.append(RPC_CLOSE_TAG);
XMLConfiguration xconf = pc.executeRpc(session, rpcReq.toString());
return xconf;
}
/**
* Extract component name from portNumber's annotations.
*
* @param pc power config instance
* @param portNumber the port number
* @return the component name
*/
public static String ocName(FujitsuPowerConfig pc, PortNumber portNumber) {
DeviceService deviceService = DefaultServiceDirectory.getService(DeviceService.class);
DeviceId deviceId = pc.handler().data().deviceId();
return deviceService.getPort(deviceId, portNumber).annotations().value("oc-name");
}
/**
* Parse filtering string from port and component.
*
* @param portNumber Port Number
* @param component port component (optical-channel)
* @param power power value set
* @param pc instance of Power config implementation
* @return filtering string in xml format
*/
public static String parsePort(FujitsuPowerConfig pc, PortNumber portNumber,
Object component, Double power) {
if (component == null) {
String name = FujitsuTerminalDeviceDiscovery.mapPortNumberToFujitsuName(portNumber);
StringBuilder sb = new StringBuilder("<components xmlns=\"http://openconfig.net/yang/platform\">");
sb.append(pc.parsePortRequestRpc(power, name));
sb.append("</component>").append("</components>");
return sb.toString();
} else {
log.error("Cannot process the component {}.", component.getClass());
return null;
}
}
}
}