ONOS-7445 ConfigurableTransceiver

- modified method signature based on what we decided on at F2F meeting

Change-Id: I0cf045a5385c65c80d5481437358c7bc23e4fdf0
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/ConfigurableTransceiver.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/ConfigurableTransceiver.java
index a94f597..b69e321 100644
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/ConfigurableTransceiver.java
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/ConfigurableTransceiver.java
@@ -15,8 +15,10 @@
  */
 package org.onosproject.odtn.behaviour;
 
+import java.util.Collections;
 import java.util.List;
 
+import org.onosproject.net.PortNumber;
 import org.onosproject.net.driver.HandlerBehaviour;
 
 import com.google.common.annotations.Beta;
@@ -30,11 +32,18 @@
     /**
      * Generate configuration to enable/disable transceiver.
      *
-     * @param componentName to enable/disable
+     * @param port to enable/disable
      * @param enable or disable
      * @return XML documents (List to handle configuration with multiple-roots)
      */
     // return type and how component get specified are likely to change in future
     @Beta
-    List<CharSequence> enable(String componentName, boolean enable);
+    List<CharSequence> enable(PortNumber port, boolean enable);
+
+    // defined only for the purpose of test command without device.
+    @Deprecated
+    default List<CharSequence> enable(String name, boolean enable) {
+        return Collections.emptyList();
+    }
+
 }
diff --git a/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/PlainTransceiver.java b/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/PlainTransceiver.java
index ae14e07..1379351 100644
--- a/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/PlainTransceiver.java
+++ b/apps/odtn/api/src/main/java/org/onosproject/odtn/behaviour/PlainTransceiver.java
@@ -15,18 +15,27 @@
  */
 package org.onosproject.odtn.behaviour;
 
+import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_NAME;
 import static org.onosproject.odtn.utils.YangToolUtil.toCharSequence;
 import static org.onosproject.odtn.utils.YangToolUtil.toCompositeData;
 import static org.onosproject.odtn.utils.YangToolUtil.toResourceData;
 import static org.onosproject.odtn.utils.YangToolUtil.toXmlCompositeStream;
+import static org.slf4j.LoggerFactory.getLogger;
 
+import java.util.Collections;
 import java.util.List;
 
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.odtn.utils.openconfig.Transceiver;
 import org.onosproject.yang.model.DataNode;
 import org.onosproject.yang.model.ResourceId;
+import org.slf4j.Logger;
 
+import com.google.common.base.Strings;
 import com.google.common.collect.Lists;
 
 /**
@@ -35,9 +44,27 @@
 public class PlainTransceiver extends AbstractHandlerBehaviour
         implements ConfigurableTransceiver {
 
+    private final Logger log = getLogger(getClass());
+
     @Override
-    public List<CharSequence> enable(String componentName, boolean enable) {
-        List<DataNode> nodes = Transceiver.enable(componentName, enable);
+    public List<CharSequence> enable(PortNumber number, boolean enable) {
+        DeviceId did = this.data().deviceId();
+        Port port = handler().get(DeviceService.class).getPort(did, number);
+        if (port == null) {
+            log.warn("{} does not exist on {}", number, did);
+            return Collections.emptyList();
+        }
+        String component = port.annotations().value(OC_NAME);
+        if (Strings.isNullOrEmpty(component)) {
+            log.warn("{} annotation not found on {}@{}", OC_NAME, number, did);
+            return Collections.emptyList();
+        }
+        return enable(component, enable);
+    }
+
+    @Override
+    public List<CharSequence> enable(String component, boolean enable) {
+        List<DataNode> nodes = Transceiver.enable(component, enable);
 
         ResourceId empty = ResourceId.builder().build();
         return Lists.transform(nodes,
diff --git a/apps/odtn/service/src/main/java/org/onosproject/odtn/cli/impl/OdtnManualTestCommand.java b/apps/odtn/service/src/main/java/org/onosproject/odtn/cli/impl/OdtnManualTestCommand.java
index b31ee83..c393487 100644
--- a/apps/odtn/service/src/main/java/org/onosproject/odtn/cli/impl/OdtnManualTestCommand.java
+++ b/apps/odtn/service/src/main/java/org/onosproject/odtn/cli/impl/OdtnManualTestCommand.java
@@ -37,8 +37,10 @@
 import org.onlab.util.XmlString;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.cli.net.DeviceIdCompleter;
+import org.onosproject.cli.net.PortNumberCompleter;
 import org.onosproject.config.DynamicConfigService;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
 import org.onosproject.netconf.NetconfController;
 import org.onosproject.netconf.NetconfDevice;
@@ -82,6 +84,14 @@
               required = false)
     String uri = null;
 
+    // injecting dependency for OSGi package import generation purpose
+    PortNumberCompleter portNoCompleter;
+    // Note: this will required Port information in device subystem
+    @Option(name = "--portNo", description = "PortNumber to send configuration to",
+            required = false)
+    String portNo = null;
+
+
     // TODO add completer for this?
     @Option(name = "--component",
             description = "Component name",
@@ -142,11 +152,19 @@
             break;
 
         case ENABLE_TRANSCEIVER:
-            nodes.addAll(transceiver.enable(componentName, true));
+            if (portNo != null) {
+                nodes.addAll(transceiver.enable(PortNumber.portNumber(portNo), true));
+            } else {
+                nodes.addAll(transceiver.enable(componentName, true));
+            }
             break;
 
         case DISABLE_TRANSCEIVER:
-            nodes.addAll(transceiver.enable(componentName, false));
+            if (portNo != null) {
+                nodes.addAll(transceiver.enable(PortNumber.portNumber(portNo), false));
+            } else {
+                nodes.addAll(transceiver.enable(componentName, false));
+            }
             break;
 
         case PRECONF_OPTICAL_CHANNEL:
diff --git a/apps/odtn/service/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/odtn/service/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 6611c01..939c9dd 100644
--- a/apps/odtn/service/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/odtn/service/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -25,12 +25,14 @@
             </completers>
             <optional-completers>
                 <entry key="--deviceId" value-ref="deviceIdCompleter"/>
+                <entry key="--portNo" value-ref="portNumberCompleter"/>
             </optional-completers>
         </command>
 
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onosproject.cli.net.DeviceIdCompleter"/>
+    <bean id="portNumberCompleter" class="org.onosproject.cli.net.PortNumberCompleter"/>
     <bean id="modeCompleter" class="org.onosproject.odtn.cli.impl.ModeCompleter"/>
 
 </blueprint>
\ No newline at end of file