Async netconf API + command for netconf troubleshooting

- Added async API for fetching entire config (+ state)
- netconf-get
   issue netcong rpc get
- netconf-get-config
   Revised to use async API to deal with slow device

Example usage:
  onos> netconf-get netconf:192.168.56.1:2022 | ppxml

 for ONOS-7481

Change-Id: Id012e984275109c93bdae113f3fec685a7b2211b
diff --git a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
index a38b6df..0a478fe 100644
--- a/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
+++ b/protocols/netconf/api/src/main/java/org/onosproject/netconf/NetconfSession.java
@@ -20,6 +20,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
 
@@ -59,6 +61,70 @@
         return request(request);
     }
 
+    /**
+     * Retrieves the specified configuration.
+     *
+     * @param datastore to retrieve configuration from
+     * @return specified configuration
+     *
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default CompletableFuture<CharSequence> asyncGetConfig(DatastoreId datastore) throws NetconfException {
+        StringBuilder rpc = new StringBuilder();
+        rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n");
+        rpc.append("<get-config>\n");
+        rpc.append("<source>\n");
+        rpc.append('<').append(checkNotNull(datastore)).append("/>");
+        rpc.append("</source>");
+        // filter here
+        rpc.append("</get-config>\n");
+        rpc.append("</rpc>");
+
+        return rpc(rpc.toString())
+                .thenApply(msg -> {
+                    // crude way of removing rpc-reply envelope
+                    int begin = msg.indexOf("<data>");
+                    int end = msg.lastIndexOf("</data>");
+                    if (begin != -1 && end != -1) {
+                        return msg.subSequence(begin + "<data>".length(), end);
+                    } else {
+                        // FIXME probably should exceptionally fail here.
+                        return msg;
+                    }
+                });
+    }
+
+    /**
+     * Retrieves running configuration and device state.
+     *
+     * @return running configuration and device state
+     *
+     * @throws NetconfException when there is a problem in the communication process on
+     * the underlying connection
+     */
+    default CompletableFuture<CharSequence> asyncGet() throws NetconfException {
+        StringBuilder rpc = new StringBuilder();
+        rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n");
+        rpc.append("<get>\n");
+        // filter here
+        rpc.append("</get>\n");
+        rpc.append("</rpc>");
+        return rpc(rpc.toString())
+                .thenApply(msg -> {
+                    // crude way of removing rpc-reply envelope
+                    int begin = msg.indexOf("<data>");
+                    int end = msg.lastIndexOf("</data>");
+                    if (begin != -1 && end != -1) {
+                        return msg.subSequence(begin + "<data>".length(), end);
+                    } else {
+                        // FIXME probably should exceptionally fail here.
+                        return msg;
+                    }
+                });
+
+    }
+
 
     /**
      * Retrieves the requested configuration, different from get-config.
@@ -109,7 +175,10 @@
      * @return specified configuration.
      * @throws NetconfException when there is a problem in the communication process on
      * the underlying connection
+     *
+     * @deprecated in 1.13.0 use async version instead.
      */
+    @Deprecated
     String getConfig(DatastoreId netconfTargetConfig) throws NetconfException;
 
     /**
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfGetCommand.java
similarity index 65%
copy from protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
copy to protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfGetCommand.java
index bac6bef..2d29fcd 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfGetCommand.java
@@ -16,10 +16,13 @@
 package org.onosproject.netconf.cli.impl;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.onosproject.netconf.DatastoreId.datastore;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.DeviceId;
 import org.onosproject.netconf.NetconfController;
@@ -28,29 +31,26 @@
 import org.onosproject.netconf.NetconfSession;
 
 /**
- * Command that gets the configuration of the specified type from the specified
- * device. If configuration cannot be retrieved it prints an error string.
+ * Command that retrieves running configuration and device state.
+ * If configuration cannot be retrieved it prints an error string.
  */
-@Command(scope = "onos", name = "netconf-get-config",
-        description = "Gets the configuration of the specified type from the" +
-                "specified device.")
-public class NetconfConfigGetCommand extends AbstractShellCommand {
+@Command(scope = "onos", name = "netconf-get",
+        description = "Retrieve running configuration and "
+                + "device state information from specified device.")
+public class NetconfGetCommand extends AbstractShellCommand {
 
-    @Argument(index = 0, name = "uri", description = "Device ID",
+    @Argument(index = 0, name = "deviceId", description = "Device ID",
             required = true, multiValued = false)
     String uri = null;
 
-    @Argument(index = 1, name = "cfgType",
-              description = "Configuration datastore name (running, etc.)",
-              required = true, multiValued = false)
-    String cfgType = null;
-
-
-    private DeviceId deviceId;
+    @Option(name = "--timeout",
+            description = "Timeout in seconds",
+            required = false)
+    long timeoutSec = 30;
 
     @Override
     protected void execute() {
-        deviceId = DeviceId.deviceId(uri);
+        DeviceId deviceId = DeviceId.deviceId(uri);
 
         NetconfController controller = get(NetconfController.class);
         checkNotNull(controller, "Netconf controller is null");
@@ -68,9 +68,10 @@
         }
 
         try {
-            String res = session.getConfig(datastore(cfgType.toLowerCase()));
+            CharSequence res = session.asyncGet()
+                    .get(timeoutSec, TimeUnit.SECONDS);
             print("%s", res);
-        } catch (NetconfException e) {
+        } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) {
             log.error("Configuration could not be retrieved", e);
             print("Error occurred retrieving configuration");
         }
diff --git a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfGetConfigCommand.java
similarity index 74%
rename from protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
rename to protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfGetConfigCommand.java
index bac6bef..2aedaa9 100644
--- a/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfConfigGetCommand.java
+++ b/protocols/netconf/ctl/src/main/java/org/onosproject/netconf/cli/impl/NetconfGetConfigCommand.java
@@ -18,8 +18,13 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onosproject.netconf.DatastoreId.datastore;
 
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.DeviceId;
 import org.onosproject.netconf.NetconfController;
@@ -34,17 +39,21 @@
 @Command(scope = "onos", name = "netconf-get-config",
         description = "Gets the configuration of the specified type from the" +
                 "specified device.")
-public class NetconfConfigGetCommand extends AbstractShellCommand {
+public class NetconfGetConfigCommand extends AbstractShellCommand {
 
-    @Argument(index = 0, name = "uri", description = "Device ID",
+    @Argument(index = 0, name = "deviceId", description = "Device ID",
             required = true, multiValued = false)
     String uri = null;
 
-    @Argument(index = 1, name = "cfgType",
+    @Argument(index = 1, name = "datastore",
               description = "Configuration datastore name (running, etc.)",
-              required = true, multiValued = false)
-    String cfgType = null;
+              required = false, multiValued = false)
+    String datastore = "running";
 
+    @Option(name = "--timeout",
+            description = "Timeout in seconds",
+            required = false)
+    long timeoutSec = 30;
 
     private DeviceId deviceId;
 
@@ -68,9 +77,10 @@
         }
 
         try {
-            String res = session.getConfig(datastore(cfgType.toLowerCase()));
+            CharSequence res = session.asyncGetConfig(datastore(datastore.toLowerCase()))
+                                .get(timeoutSec, TimeUnit.SECONDS);
             print("%s", res);
-        } catch (NetconfException e) {
+        } catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) {
             log.error("Configuration could not be retrieved", e);
             print("Error occurred retrieving configuration");
         }
diff --git a/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 73993ce..019014e 100644
--- a/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/protocols/netconf/ctl/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -26,7 +26,7 @@
         </command>
 
         <command>
-            <action class="org.onosproject.netconf.cli.impl.NetconfConfigGetCommand"/>
+            <action class="org.onosproject.netconf.cli.impl.NetconfGetConfigCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
                 <ref component-id="targetConfigurationsCompleter"/>
@@ -35,6 +35,14 @@
         </command>
 
         <command>
+            <action class="org.onosproject.netconf.cli.impl.NetconfGetCommand"/>
+            <completers>
+                <ref component-id="deviceIdCompleter"/>
+                <null/>
+            </completers>
+        </command>
+
+        <command>
             <action class="org.onosproject.netconf.cli.impl.NetconfRpcTestCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>