Fix for Modulation and Power

Change-Id: I579726c25c0f2caad25441c4a737e5180f74bc85
diff --git a/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java b/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java
index 0ef23a2..817bba8 100644
--- a/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/PowerConfigCommand.java
@@ -82,9 +82,23 @@
         //  <component> and <optical-channel>.
         if (operation.equals("get")) {
             Optional<Long> val = powerConfig.getTargetPower(cp.port(), Direction.ALL);
-            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);
+            if (val.isPresent()) {
+                long power = val.orElse(Long.MIN_VALUE);
+                print("The target-output-power value in port %s on device %s is %d.",
+                        cp.port().toString(), cp.deviceId().toString(), power);
+            }
+            Optional<Long> currentPower = powerConfig.currentPower(cp.port(), Direction.ALL);
+            if (currentPower.isPresent()) {
+                long currentPowerVal = currentPower.orElse(Long.MIN_VALUE);
+                print("The current-output-power value in port %s on device %s is %d.",
+                        cp.port().toString(), cp.deviceId().toString(), currentPowerVal);
+            }
+            Optional<Long> currentInputPower = powerConfig.currentInputPower(cp.port(), Direction.ALL);
+            if (currentInputPower.isPresent()) {
+                long inputPowerVal = currentInputPower.orElse(Long.MIN_VALUE);
+                print("The current-input-power value in port %s on device %s is %d.",
+                        cp.port().toString(), cp.deviceId().toString(), inputPowerVal);
+            }
         } else if (operation.equals("edit-config")) {
             checkNotNull(value);
             powerConfig.setTargetPower(cp.port(), Direction.ALL, value);
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniModulationOpenConfig.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniModulationOpenConfig.java
index 5592041..c85e43e 100644
--- a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniModulationOpenConfig.java
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniModulationOpenConfig.java
@@ -240,24 +240,38 @@
             String filter = createModulationFilter(cassini, port);
             StringBuilder rpcReq = new StringBuilder();
             rpcReq.append(RPC_TAG_NETCONF_BASE)
-                    .append("<get>")
+                    .append("<get-config>")
+                    .append("<source>")
+                    .append("<" + DatastoreId.RUNNING + "/>")
+                    .append("</source>")
                     .append("<filter type='subtree'>")
                     .append(filter)
                     .append("</filter>")
-                    .append("</get>")
+                    .append("</get-config>")
                     .append(RPC_CLOSE_TAG);
-            log.info("RPC Call for Getting Modulation : \n {}", rpcReq.toString());
             XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString());
+            if (xconf == null) {
+                log.error("Error in executingRpc");
+                return Optional.empty();
+            }
             try {
                 HierarchicalConfiguration config =
-                        xconf.configurationAt("components/component/optical-channel/config");
+                        xconf.configurationAt("data/components/component/optical-channel/config");
 
-                String modulationScheme = String.valueOf(config.getString("modulation"));
+                String modulationScheme = String.valueOf(config.getString("modulation-format"));
                 /*Used for Internal Testing */
                 //String modulationScheme="DP16QAM";
-                ModulationScheme modulation = ModulationScheme.valueOf(modulationScheme);
+                ModulationScheme modulation;
+                if (modulationScheme.equalsIgnoreCase("dp-16-qam")) {
+                    modulation = ModulationScheme.DP_16QAM;
+                } else if (modulationScheme.equalsIgnoreCase("dp-8-qam")) {
+                    modulation = ModulationScheme.DP_8QAM;
+                } else {
+                    modulation = ModulationScheme.DP_QPSK;
+                }
                 return Optional.of(modulation);
             } catch (IllegalArgumentException e) {
+                log.error("Error in parsing config", e);
                 return Optional.empty();
             }
         }
@@ -388,9 +402,11 @@
                     .append(RPC_CLOSE_TAG);
             log.info("RPC call for Setting Modulation : {}", rpcReq.toString());
             XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString());
-
-            // The successful reply should be "<rpc-reply ...><ok /></rpc-reply>"
-            if (!xconf.getRoot().getChild(0).getName().equals("ok")) {
+            if (xconf == null) {
+                log.error("The <edit-config> operation to set target-modulation of Port({}:{}) is failed.",
+                        port.toString(), component.toString());
+            } else if (!xconf.getRoot().getChild(0).getName().equals("ok")) {
+                // The successful reply should be "<rpc-reply ...><ok /></rpc-reply>"
                 response = false;
                 log.error("The <edit-config> operation to set target-modulation of Port({}:{}) is failed.",
                         port.toString(), component.toString());
diff --git a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDeviceDiscovery.java b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDeviceDiscovery.java
index ebc1c65..6411d4a 100644
--- a/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDeviceDiscovery.java
+++ b/drivers/odtn-driver/src/main/java/org/onosproject/drivers/odtn/CassiniTerminalDeviceDiscovery.java
@@ -25,6 +25,7 @@
 import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
 import org.onlab.packet.ChassisId;
 import org.onosproject.drivers.utilities.XmlConfigParser;
+import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.ChannelSpacing;
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.Device;
@@ -262,7 +263,7 @@
         if (type.equals(OC_TRANSPORT_TYPES_OPTICAL_CHANNEL)) {
             String portName = component.getString("optical-channel/config/line-port");
             String portIndex = portName.split("-")[1];
-            annotations.putIfAbsent("name", portName);
+            annotations.putIfAbsent(AnnotationKeys.PORT_NAME, portName);
             annotations.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value());
             annotations.putIfAbsent(ONOS_PORT_INDEX, portIndex);
             annotations.putIfAbsent(CONNECTION_ID, "connection-" + portIndex);
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
index 33dc66b..7b312bf 100644
--- 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
@@ -19,6 +19,7 @@
 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;
@@ -36,6 +37,7 @@
 import org.onosproject.netconf.NetconfSession;
 import org.slf4j.Logger;
 
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
@@ -47,7 +49,6 @@
 
 /**
  * Driver Implementation of the PowerConfig for OpenConfig terminal devices.
- *
  */
 public class CassiniTerminalDevicePowerConfig<T>
         extends AbstractHandlerBehaviour implements PowerConfig<T> {
@@ -67,7 +68,6 @@
      * 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) {
@@ -91,6 +91,7 @@
 
     /**
      * Execute RPC request.
+     *
      * @param session Netconf session
      * @param message Netconf message in XML format
      * @return XMLConfiguration object
@@ -114,7 +115,8 @@
 
     /**
      * Get the target-output-power value on specific optical-channel.
-     * @param port the port
+     *
+     * @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
@@ -164,6 +166,7 @@
 
     /**
      * Set the ComponentType to invoke proper methods for different template T.
+     *
      * @param component the component.
      */
     void checkState(Object component) {
@@ -195,6 +198,7 @@
             public Optional<Long> getTargetPower(PortNumber port, Object component) {
                 return super.getTargetPower(port, component);
             }
+
             @Override
             public void setTargetPower(PortNumber port, Object component, long power) {
                 super.setTargetPower(port, component, power);
@@ -217,12 +221,12 @@
         };
 
 
-
         CassiniTerminalDevicePowerConfig cassini;
 
         /**
          * mirror method in the internal class.
-         * @param port port
+         *
+         * @param port      port
          * @param component component
          * @return target power
          */
@@ -232,16 +236,23 @@
             String filter = parsePort(cassini, port, null, null);
             StringBuilder rpcReq = new StringBuilder();
             rpcReq.append(RPC_TAG_NETCONF_BASE)
-                    .append("<get>")
+                    .append("<get-config>")
+                    .append("<source>")
+                    .append("<" + DatastoreId.RUNNING + "/>")
+                    .append("</source>")
                     .append("<filter type='subtree'>")
                     .append(filter)
                     .append("</filter>")
-                    .append("</get>")
+                    .append("</get-config>")
                     .append(RPC_CLOSE_TAG);
             XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString());
+            if (xconf == null) {
+                log.error("Error in executingRpc");
+                return Optional.empty();
+            }
             try {
                 HierarchicalConfiguration config =
-                        xconf.configurationAt("components/component/optical-channel/state");
+                        xconf.configurationAt("data/components/component/optical-channel/config");
                 long power = Float.valueOf(config.getString("target-output-power")).longValue();
                 return Optional.of(power);
             } catch (IllegalArgumentException e) {
@@ -251,9 +262,10 @@
 
         /**
          * mirror method in the internal class.
-         * @param port port
+         *
+         * @param port      port
          * @param component component
-         * @param power target value
+         * @param power     target value
          */
         void setTargetPower(PortNumber port, Object component, long power) {
             NetconfSession session = cassini.getNetconfSession(cassini.did());
@@ -278,22 +290,23 @@
             try {
                 session.commit();
             } catch (NetconfException e) {
-                log.error("error committing channel power");
+                log.error("error committing channel power", e);
             }
         }
 
         /**
          * mirror method in the internal class.
-         * @param port port
+         *
+         * @param port      port
          * @param component the component.
          * @return current output power.
          */
         Optional<Long> currentPower(PortNumber port, Object component) {
             XMLConfiguration xconf = getOpticalChannelState(
-                                    cassini, port, "<output-power><instant/></output-power>");
+                    cassini, port, "<output-power><instant/></output-power>");
             try {
                 HierarchicalConfiguration config =
-                        xconf.configurationAt("components/component/optical-channel/state/output-power");
+                        xconf.configurationAt("data/components/component/optical-channel/state/output-power");
                 long currentPower = Float.valueOf(config.getString("instant")).longValue();
                 return Optional.of(currentPower);
             } catch (IllegalArgumentException e) {
@@ -303,7 +316,8 @@
 
         /**
          * mirror method in the internal class.
-         * @param port port
+         *
+         * @param port      port
          * @param component the component
          * @return current input power
          */
@@ -312,7 +326,7 @@
                     cassini, port, "<input-power><instant/></input-power>");
             try {
                 HierarchicalConfiguration config =
-                        xconf.configurationAt("components/component/optical-channel/state/input-power");
+                        xconf.configurationAt("data/components/component/optical-channel/state/input-power");
                 long currentPower = Float.valueOf(config.getString("instant")).longValue();
                 return Optional.of(currentPower);
             } catch (IllegalArgumentException e) {
@@ -321,9 +335,9 @@
         }
 
         Optional<Range<Long>> getTargetPowerRange(PortNumber port, Object component) {
-                long targetMin = -30;
-                long targetMax = 1;
-                return Optional.of(Range.open(targetMin, targetMax));
+            long targetMin = -30;
+            long targetMax = 1;
+            return Optional.of(Range.open(targetMin, targetMax));
         }
 
         Optional<Range<Long>> getInputPowerRange(PortNumber port, Object component) {
@@ -340,13 +354,14 @@
 
         /**
          * Get filtered content under <optical-channel><state>.
-         * @param pc power config instance
-         * @param port the port number
+         *
+         * @param pc         power config instance
+         * @param port       the port number
          * @param underState the filter condition
          * @return RPC reply
          */
         private static XMLConfiguration getOpticalChannelState(CassiniTerminalDevicePowerConfig pc,
-                                                 PortNumber port, String underState) {
+                                                               PortNumber port, String underState) {
             NetconfSession session = pc.getNetconfSession(pc.did());
             checkNotNull(session);
             String name = ocName(pc, port);
@@ -358,14 +373,23 @@
                     .append(underState)
                     .append("</state></optical-channel></component></components></filter></get>")
                     .append(RPC_CLOSE_TAG);
+            log.info("Getting Optical Channel State {}", rpcReq.toString());
+            StringWriter stringWriter = new StringWriter();
             XMLConfiguration xconf = pc.executeRpc(session, rpcReq.toString());
+            try {
+                xconf.save(stringWriter);
+            } catch (ConfigurationException e) {
+                log.error("XML Config Exception ", e);
+            }
+            log.info("Optical Channel State {}", stringWriter.toString());
             return xconf;
         }
 
 
         /**
          * Extract component name from portNumber's annotations.
-         * @param pc power config instance
+         *
+         * @param pc         power config instance
          * @param portNumber the port number
          * @return the component name
          */
@@ -376,12 +400,12 @@
         }
 
 
-
         /**
          * Parse filtering string from port and component.
+         *
          * @param portNumber Port Number
-         * @param component port component (optical-channel)
-         * @param power power value set.
+         * @param component  port component (optical-channel)
+         * @param power      power value set.
          * @return filtering string in xml format
          */
         private static String parsePort(CassiniTerminalDevicePowerConfig pc, PortNumber portNumber,