[ONOS-7850] powerconfig for Cassini equipment.
Change-Id: If8a03ca4f2fa29536a56a0ff55d77600baa11d1b
diff --git a/cli/src/main/java/org/onosproject/cli/net/NetconfOperationCompleter.java b/cli/src/main/java/org/onosproject/cli/net/NetconfOperationCompleter.java
new file mode 100644
index 0000000..c32be06
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/NetconfOperationCompleter.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2018-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 METRO-HAUL (761727).
+ */
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.CommandLine;
+import org.apache.karaf.shell.api.console.Completer;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.karaf.shell.support.completers.StringsCompleter;
+
+import java.util.List;
+import java.util.SortedSet;
+
+/**
+ * Netconf Operation Completer.
+ */
+@Service
+public class NetconfOperationCompleter implements Completer {
+
+ @Override
+ public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+ // Delegate string completer
+ StringsCompleter delegate = new StringsCompleter();
+ SortedSet<String> strings = delegate.getStrings();
+ // The available operations are defined in NETCONF protocol (https://tools.ietf.org/html/rfc6241#section-7).
+ strings.add("get");
+ strings.add("get-config");
+ strings.add("edit-config");
+ strings.add("copy-config");
+ strings.add("delete-config");
+ strings.add("lock");
+ strings.add("unlock");
+ strings.add("close-session");
+ strings.add("kill-session");
+ return delegate.complete(session, commandLine, candidates);
+ }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java b/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java
new file mode 100644
index 0000000..b7d5020
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2018-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 METRO-HAUL (761727).
+ */
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Device;
+import org.onosproject.net.Port;
+import org.onosproject.net.behaviour.PowerConfig;
+import org.onosproject.net.device.DeviceService;
+import org.slf4j.Logger;
+
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Get the target-output-power for specific optical-channel.
+ * This is a command for PowerConfig.
+ */
+@Service
+@Command(scope = "onos", name = "power-config",
+ description = "Get/Edit the target-output-power for specific optical-channel")
+public class PowerConfigCommand extends AbstractShellCommand {
+
+ private static final Logger log = getLogger(PowerConfigCommand.class);
+
+ @Argument(index = 0, name = "operation", description = "Netconf Operation including get, edit-config, etc.",
+ required = true, multiValued = false)
+ @Completion(NetconfOperationCompleter.class)
+ private String operation = null;
+
+ @Argument(index = 1, name = "connection point", description = "{DeviceID}/{PortNumber}",
+ required = true, multiValued = false)
+ @Completion(ConnectPointCompleter.class)
+ private String connectPoint = null;
+
+ @Argument(index = 2, name = "value", description = "target-output-power value. Unit: dBm",
+ required = false, multiValued = false)
+ private Long value = null;
+
+ @Override
+ protected void doExecute() throws Exception {
+ DeviceService deviceService = get(DeviceService.class);
+ ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPoint);
+ Port port = deviceService.getPort(cp);
+ if (port == null) {
+ print("[ERROR] %s does not exist", cp);
+ return;
+ }
+ Device device = deviceService.getDevice(cp.deviceId());
+ PowerConfig powerConfig = device.as(PowerConfig.class);
+ // FIXME the parameter "component" equals NULL now, because there is one-to-one mapping between
+ // <component> and <optical-channel>.
+ if (operation.equals("get")) {
+ Optional<Long> val = powerConfig.getTargetPower(cp.port(), null);
+ long power = val.isPresent() ? val.get() : Long.MIN_VALUE;
+ print("The target-output-power value in port %s on device %s is %d.",
+ cp.port().toString(), cp.deviceId().toString(), power);
+ } else if (operation.equals("edit-config")) {
+ checkNotNull(value);
+ powerConfig.setTargetPower(cp.port(), null, value);
+ } else {
+ log.warn("Operation {} are not supported now.", operation);
+ }
+ }
+}
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDevicePowerConfig.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDevicePowerConfig.java
new file mode 100644
index 0000000..48fde3c
--- /dev/null
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDevicePowerConfig.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2018-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 METRO-HAUL (761727).
+ */
+
+package org.onosproject.drivers.odtn;
+
+import com.google.common.collect.Range;
+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.OchSignal;
+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.NetconfController;
+import org.onosproject.netconf.NetconfDevice;
+import org.onosproject.netconf.NetconfException;
+import org.onosproject.netconf.NetconfSession;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Driver Implementation of the PowerConfig for OpenConfig terminal devices.
+ *
+ */
+public class CassiniTerminalDevicePowerConfig
+ extends AbstractHandlerBehaviour implements PowerConfig<OchSignal> {
+
+ private static final String RPC_TAG_NETCONF_BASE =
+ "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">";
+
+ private static final String RPC_CLOSE_TAG = "</rpc>";
+
+ private static final long NO_POWER = -50;
+
+ private static final Logger log = getLogger(CassiniTerminalDevicePowerConfig.class);
+
+ /**
+ * Returns the NetconfSession with the device for which the method was called.
+ *
+ * @param deviceId device indetifier
+ *
+ * @return The netconf session or null
+ */
+ private NetconfSession getNetconfSession(DeviceId deviceId) {
+ 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
+ */
+ private DeviceId did() {
+ return handler().data().deviceId();
+ }
+
+ /**
+ * Parse filtering string from port and component.
+ * @param portNumber Port Number
+ * @param component port component (optical-channel)
+ * @param power power value set.
+ * @return filtering string in xml format
+ */
+ private String parsePort(PortNumber portNumber, OchSignal component, Long power) {
+ if (component == null) {
+ DeviceService deviceService = DefaultServiceDirectory.getService(DeviceService.class);
+ DeviceId deviceId = handler().data().deviceId();
+ String name = deviceService.getPort(deviceId, portNumber).annotations().value("oc-name");
+ StringBuilder sb = new StringBuilder("<components xmlns=\"http://openconfig.net/yang/platform\">");
+ sb.append("<component>").append("<name>").append(name).append("</name>");
+ if (power != null) {
+ // This is an edit-config operation.
+ sb.append("<optical-channel xmlns=\"http://openconfig.net/yang/terminal-device\">")
+ .append("<config>")
+ .append("<target-output-power>")
+ .append(power)
+ .append("</target-output-power>")
+ .append("</config>")
+ .append("</optical-channel>");
+ }
+ sb.append("</component>").append("</components>");
+ return sb.toString();
+ } else {
+ log.error("Cannot process the component {}.", component.getClass());
+ return null;
+ }
+ }
+
+ /**
+ * Execute RPC request.
+ * @param session Netconf session
+ * @param message Netconf message in XML format
+ * @return XMLConfiguration object
+ */
+ private XMLConfiguration executeRpc(NetconfSession session, String message) {
+ try {
+ CompletableFuture<String> fut = session.rpc(message);
+ String rpcReply = fut.get();
+ XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply);
+ xconf.setExpressionEngine(new XPathExpressionEngine());
+ 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;
+ }
+
+ /**
+ * 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<Long> getTargetPower(PortNumber port, OchSignal component) {
+ NetconfSession session = getNetconfSession(did());
+ if (session == null) {
+ log.error("discoverPortDetails called with null session for {}", did());
+ return Optional.of(NO_POWER);
+ }
+
+ String filter = parsePort(port, component, null);
+ StringBuilder rpcReq = new StringBuilder();
+ rpcReq.append(RPC_TAG_NETCONF_BASE)
+ .append("<get>")
+ .append("<filter type='subtree'>")
+ .append(filter)
+ .append("</filter>")
+ .append("</get>")
+ .append(RPC_CLOSE_TAG);
+ XMLConfiguration xconf = executeRpc(session, rpcReq.toString());
+ HierarchicalConfiguration config =
+ xconf.configurationAt("data/components/component/optical-channel/config");
+ long power = Float.valueOf(config.getString("target-output-power")).longValue();
+ return Optional.of(power);
+ }
+
+ @Override
+ public void setTargetPower(PortNumber port, OchSignal component, long power) {
+ NetconfSession session = getNetconfSession(did());
+ if (session == null) {
+ log.error("setTargetPower called with null session for {}", did());
+ return;
+ }
+ String editConfig = parsePort(port, component, power);
+ StringBuilder rpcReq = new StringBuilder();
+ rpcReq.append(RPC_TAG_NETCONF_BASE)
+ .append("<edit-config>")
+ .append("<target><running/></target>")
+ .append("<config>")
+ .append(editConfig)
+ .append("</config>")
+ .append("</edit-config>")
+ .append(RPC_CLOSE_TAG);
+ XMLConfiguration xconf = 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());
+ }
+ }
+
+ @Override
+ public Optional<Long> currentPower(PortNumber port, OchSignal component) {
+ // FIXME
+ log.warn("Not Implemented Yet!");
+ return Optional.empty();
+ }
+
+ @Override
+ public Optional<Range<Long>> getTargetPowerRange(PortNumber port, OchSignal component) {
+ // FIXME
+ log.warn("Not Implemented Yet!");
+ return Optional.empty();
+ }
+
+ @Override
+ public Optional<Range<Long>> getInputPowerRange(PortNumber port, OchSignal component) {
+ // FIXME
+ log.warn("Not Implemented Yet!");
+ return Optional.empty();
+ }
+
+ @Override
+ public List<PortNumber> getPorts(OchSignal component) {
+ // FIXME
+ log.warn("Not Implemented Yet!");
+ return new ArrayList<PortNumber>();
+ }
+}
diff --git a/drivers/odtn-driver/src/main/resources/odtn-drivers.xml b/drivers/odtn-driver/src/main/resources/odtn-drivers.xml
index c4f2faa..8c7a799 100644
--- a/drivers/odtn-driver/src/main/resources/odtn-drivers.xml
+++ b/drivers/odtn-driver/src/main/resources/odtn-drivers.xml
@@ -64,6 +64,8 @@
impl="org.onosproject.drivers.odtn.openconfig.TerminalDeviceLambdaQuery"/>
<behaviour api="org.onosproject.net.flow.FlowRuleProgrammable"
impl="org.onosproject.drivers.odtn.CassiniFlowRuleProgrammable"/>
+ <behaviour api="org.onosproject.net.behaviour.PowerConfig"
+ impl="org.onosproject.drivers.odtn.CassiniTerminalDevicePowerConfig"/>
</driver>
<driver name="nokia-1830" manufacturer="nokia" hwVersion="1830" swVersion="R10.1.1">
<behaviour api="org.onosproject.net.device.DeviceDescriptionDiscovery"