ONOS-6159 Add command completers for v-net CLI

Change-Id: Ie048da32cdba6d2e9ed88b6d9d0404749750b0c8
diff --git a/cli/src/main/java/org/onosproject/cli/net/vnet/TenantCompleter.java b/cli/src/main/java/org/onosproject/cli/net/vnet/TenantCompleter.java
new file mode 100644
index 0000000..e4e62c2
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/vnet/TenantCompleter.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.cli.net.vnet;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.TenantId;
+import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
+
+import java.util.List;
+import java.util.SortedSet;
+
+/**
+ * Tenant Id completer.
+ */
+public class TenantCompleter implements Completer {
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // Delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+
+        // Fetch our service and feed it's offerings to the string completer
+        VirtualNetworkAdminService service = AbstractShellCommand.get(VirtualNetworkAdminService.class);
+
+        SortedSet<String> strings = delegate.getStrings();
+
+        for (TenantId tenantId : service.getTenantIds()) {
+            strings.add(tenantId.id());
+        }
+
+        // Now let the completer do the work for figuring out what to offer.
+        return delegate.complete(buffer, cursor, candidates);
+    }
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualDeviceCompleter.java b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualDeviceCompleter.java
new file mode 100644
index 0000000..6a28885
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualDeviceCompleter.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.cli.net.vnet;
+
+import static org.onlab.osgi.DefaultServiceDirectory.getService;
+
+import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.incubator.net.virtual.VirtualDevice;
+import org.onosproject.incubator.net.virtual.VirtualNetworkService;
+import org.onosproject.utils.Comparators;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Virtual device completer.
+ *
+ * Assumes the first argument which can be parsed to a number is network id.
+ */
+public class VirtualDeviceCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        ArgumentList args = getArgumentList();
+        //parse argument list for network id
+        String[] argsArray = args.getArguments();
+        for (String str : argsArray) {
+            if (str.matches("[0-9]+")) {
+                long networkId = Long.valueOf(str);
+                return getSortedVirtualDevices(networkId).stream()
+                        .map(virtualDevice -> virtualDevice.id().toString())
+                        .collect(Collectors.toList());
+            }
+        }
+        return Collections.singletonList("Missing network id");
+    }
+
+    /**
+     * Returns the list of virtual devices sorted using the network identifier.
+     *
+     * @param networkId network id
+     * @return sorted virtual device list
+     */
+    private List<VirtualDevice> getSortedVirtualDevices(long networkId) {
+        VirtualNetworkService service = getService(VirtualNetworkService.class);
+
+        List<VirtualDevice> virtualDevices = new ArrayList<>();
+        virtualDevices.addAll(service.getVirtualDevices(NetworkId.networkId(networkId)));
+        Collections.sort(virtualDevices, Comparators.VIRTUAL_DEVICE_COMPARATOR);
+        return virtualDevices;
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualHostCompleter.java b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualHostCompleter.java
new file mode 100644
index 0000000..42ac811
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualHostCompleter.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.cli.net.vnet;
+
+import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.incubator.net.virtual.VirtualHost;
+import org.onosproject.incubator.net.virtual.VirtualNetworkService;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.onlab.osgi.DefaultServiceDirectory.getService;
+
+/**
+ * Virtual host completer.
+ *
+ * Assumes the first argument which can be parsed to a number is network id.
+ */
+public class VirtualHostCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        ArgumentList args = getArgumentList();
+        //parse argument list for network id
+        String[] argsArray = args.getArguments();
+        for (String str : argsArray) {
+            if (str.matches("[0-9]+")) {
+                long networkId = Long.valueOf(str);
+                return getSortedVirtualHosts(networkId).stream()
+                        .map(virtualHost -> virtualHost.id().toString())
+                        .collect(Collectors.toList());
+            }
+        }
+        return Collections.singletonList("Missing network id");
+    }
+
+    /**
+     * Returns the list of virtual hosts sorted using the host identifier.
+     *
+     * @param networkId network id
+     * @return virtual host list
+     */
+    private List<VirtualHost> getSortedVirtualHosts(long networkId) {
+        VirtualNetworkService service = getService(VirtualNetworkService.class);
+
+        List<VirtualHost> virtualHosts = new ArrayList<>();
+        virtualHosts.addAll(service.getVirtualHosts(NetworkId.networkId(networkId)));
+        return virtualHosts;
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualNetworkCompleter.java b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualNetworkCompleter.java
new file mode 100644
index 0000000..f4e7604
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualNetworkCompleter.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.cli.net.vnet;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.incubator.net.virtual.TenantId;
+import org.onosproject.incubator.net.virtual.VirtualNetwork;
+import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
+import org.onosproject.utils.Comparators;
+
+import java.util.List;
+import java.util.Set;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.SortedSet;
+
+/**
+ * Virtual network completer.
+ */
+public class VirtualNetworkCompleter implements Completer {
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // Delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+
+        // Fetch our service and feed it's offerings to the string completer
+        VirtualNetworkAdminService service = AbstractShellCommand.get(VirtualNetworkAdminService.class);
+
+        List<VirtualNetwork> virtualNetworks = new ArrayList<>();
+
+        Set<TenantId> tenantSet = service.getTenantIds();
+        tenantSet.forEach(tenantId -> virtualNetworks.addAll(service.getVirtualNetworks(tenantId)));
+
+        Collections.sort(virtualNetworks, Comparators.VIRTUAL_NETWORK_COMPARATOR);
+
+        SortedSet<String> strings = delegate.getStrings();
+        virtualNetworks.forEach(virtualNetwork -> strings.add(virtualNetwork.id().toString()));
+
+        // Now let the completer do the work for figuring out what to offer.
+        return delegate.complete(buffer, cursor, candidates);
+    }
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualPortCompleter.java b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualPortCompleter.java
new file mode 100644
index 0000000..9fbf8fa
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/vnet/VirtualPortCompleter.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+package org.onosproject.cli.net.vnet;
+
+import static org.onlab.osgi.DefaultServiceDirectory.getService;
+
+import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.incubator.net.virtual.NetworkId;
+import org.onosproject.incubator.net.virtual.VirtualNetworkService;
+import org.onosproject.incubator.net.virtual.VirtualPort;
+import org.onosproject.net.DeviceId;
+import org.onosproject.utils.Comparators;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+/**
+ * Virtual port completer.
+ *
+ * Assumes the first argument which can be parsed to a number is network id
+ * and the argument right before the one being completed is device id
+ */
+public class VirtualPortCompleter extends AbstractChoicesCompleter {
+    @Override
+    protected List<String> choices() {
+        ArgumentList args = getArgumentList();
+        //parse argument list for network id
+        String[] argsArray = args.getArguments();
+        for (String str : argsArray) {
+            if (str.matches("[0-9]+")) {
+                long networkId = Long.valueOf(str);
+                String deviceId = argsArray[argsArray.length - 1];
+                return getSortedVirtualPorts(networkId, deviceId).stream()
+                        .map(virtualPort -> virtualPort.number().toString())
+                        .collect(Collectors.toList());
+            }
+        }
+
+        return Collections.singletonList("Missing network id");
+    }
+
+    /**
+     * Returns the list of virtual ports sorted using the port number.
+     *
+     * @param networkId network id.
+     * @param deviceId device id
+     * @return sorted virtual port list
+     */
+    private List<VirtualPort> getSortedVirtualPorts(long networkId, String deviceId) {
+        VirtualNetworkService service = getService(VirtualNetworkService.class);
+
+        List<VirtualPort> virtualPorts = new ArrayList<>();
+        virtualPorts.addAll(service.getVirtualPorts(NetworkId.networkId(networkId),
+                DeviceId.deviceId(deviceId)));
+        Collections.sort(virtualPorts, Comparators.VIRTUAL_PORT_COMPARATOR);
+        return virtualPorts;
+    }
+
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 393649d..1441f1b 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -711,63 +711,163 @@
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.TenantRemoveCommand"/>
+            <completers>
+                <ref component-id="tenantCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualNetworkListCommand"/>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualNetworkCreateCommand"/>
+            <completers>
+                <ref component-id="tenantCompleter"/>
+                <ref component-id="nullCompleter"/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualNetworkRemoveCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualDeviceListCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualDeviceCreateCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualDeviceRemoveCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="nullCompleter"/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualLinkListCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualLinkCreateCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="virtualPortCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="virtualPortCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualLinkRemoveCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="virtualPortCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="virtualPortCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualPortListCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualPortCreateCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="nullCompleter"/>
+                <ref component-id="deviceIdCompleter"/>
+                <ref component-id="portNumberCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualPortBindCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="virtualPortCompleter"/>
+                <ref component-id="deviceIdCompleter"/>
+                <ref component-id="portNumberCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualPortRemoveCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="virtualPortCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualHostListCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualHostCreateCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualHostRemoveCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="virtualHostCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualNetworkIntentCreateCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualNetworkIntentRemoveCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.vnet.VirtualFlowsListCommand"/>
+            <completers>
+                <ref component-id="virtualNetworkCompleter"/>
+                <ref component-id="flowRuleStatusCompleter"/>
+                <ref component-id="virtualDeviceCompleter"/>
+                <ref component-id="placeholderCompleter"/>
+                <null/>
+            </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.DpisListCommand"/>
@@ -844,4 +944,9 @@
 
     <bean id="linkTypeCompleter" class="org.onosproject.cli.net.completer.LinkTypeCompleter"/>
 
+    <bean id="tenantCompleter" class="org.onosproject.cli.net.vnet.TenantCompleter"/>
+    <bean id="virtualNetworkCompleter" class="org.onosproject.cli.net.vnet.VirtualNetworkCompleter"/>
+    <bean id="virtualDeviceCompleter" class="org.onosproject.cli.net.vnet.VirtualDeviceCompleter"/>
+    <bean id="virtualPortCompleter" class="org.onosproject.cli.net.vnet.VirtualPortCompleter"/>
+    <bean id="virtualHostCompleter" class="org.onosproject.cli.net.vnet.VirtualHostCompleter"/>
 </blueprint>