Host list command added to CLI

Change-Id: I52d11d3b1d56650e92357b6cb2c67007be742114
diff --git a/cli/src/main/java/org/onlab/onos/cli/net/HostIdCompleter.java b/cli/src/main/java/org/onlab/onos/cli/net/HostIdCompleter.java
new file mode 100644
index 0000000..6da2d82
--- /dev/null
+++ b/cli/src/main/java/org/onlab/onos/cli/net/HostIdCompleter.java
@@ -0,0 +1,32 @@
+package org.onlab.onos.cli.net;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.SortedSet;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.net.Host;
+import org.onlab.onos.net.host.HostService;
+
+public class HostIdCompleter implements Completer {
+
+    @Override
+    public int complete(String buffer, int cursor, List<String> candidates) {
+        // Delegate string completer
+        StringsCompleter delegate = new StringsCompleter();
+
+        HostService service = AbstractShellCommand.get(HostService.class);
+        Iterator<Host> it = service.getHosts().iterator();
+        SortedSet<String> strings = delegate.getStrings();
+        while (it.hasNext()) {
+            strings.add(it.next().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/onlab/onos/cli/net/HostsListCommand.java b/cli/src/main/java/org/onlab/onos/cli/net/HostsListCommand.java
new file mode 100644
index 0000000..47aa688
--- /dev/null
+++ b/cli/src/main/java/org/onlab/onos/cli/net/HostsListCommand.java
@@ -0,0 +1,65 @@
+package org.onlab.onos.cli.net;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.onos.cli.AbstractShellCommand;
+import org.onlab.onos.net.Host;
+import org.onlab.onos.net.host.HostService;
+
+/**
+ * Lists all currently-known hosts.
+ */
+@Command(scope = "onos", name = "hosts",
+        description = "Lists all currently-known hosts.")
+public class HostsListCommand extends AbstractShellCommand {
+
+    private static final String FMT =
+            "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
+
+    protected static final Comparator<Host> ID_COMPARATOR = new Comparator<Host>() {
+        @Override
+        public int compare(Host h1, Host h2) {
+            return h1.id().uri().toString().compareTo(h2.id().uri().toString());
+        }
+    };
+
+    @Override
+    protected Object doExecute() throws Exception {
+        HostService service = getService(HostService.class);
+        for (Host host : getSortedHosts(service)) {
+            printHost(host);
+        }
+        return null;
+    }
+
+    /**
+     * Returns the list of devices sorted using the device ID URIs.
+     *
+     * @param service device service
+     * @return sorted device list
+     */
+    protected List<Host> getSortedHosts(HostService service) {
+        List<Host> hosts = newArrayList(service.getHosts());
+        Collections.sort(hosts, ID_COMPARATOR);
+        return hosts;
+    }
+
+    /**
+     * Prints information about a host.
+     *
+     * @param host
+     */
+    protected void printHost(Host host) {
+        if (host != null) {
+            print(FMT, host.id(), host.mac(),
+                    host.location().deviceId(),
+                    host.location().port(),
+                    host.vlan(), host.ipAddresses());
+        }
+    }
+ }
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 1ed7931..55fec58 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -55,10 +55,19 @@
                 <ref component-id="clusterIdCompleter"/>
             </completers>
         </command>
+
+        <command>
+            <action class="org.onlab.onos.cli.net.HostsListCommand"/>
+            <completers>
+                <ref component-id="hostIdCompleter"/>
+            </completers>
+        </command>
+
     </command-bundle>
 
     <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/>
     <bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/>
     <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
+    <bean id="hostIdCompleter" class="org.onlab.onos.cli.net.HostIdCompleter"/>
 
 </blueprint>
diff --git a/providers/of/host/src/main/java/org/onlab/onos/provider/of/host/impl/OpenFlowHostProvider.java b/providers/of/host/src/main/java/org/onlab/onos/provider/of/host/impl/OpenFlowHostProvider.java
index 6e5d600..3f87be5 100644
--- a/providers/of/host/src/main/java/org/onlab/onos/provider/of/host/impl/OpenFlowHostProvider.java
+++ b/providers/of/host/src/main/java/org/onlab/onos/provider/of/host/impl/OpenFlowHostProvider.java
@@ -19,6 +19,7 @@
 import org.onlab.onos.net.host.HostProviderService;
 import org.onlab.onos.net.provider.AbstractProvider;
 import org.onlab.onos.net.provider.ProviderId;
+import org.onlab.onos.of.controller.Dpid;
 import org.onlab.onos.of.controller.OpenFlowController;
 import org.onlab.onos.of.controller.OpenFlowPacketContext;
 import org.onlab.onos.of.controller.PacketListener;
@@ -61,7 +62,7 @@
     @Activate
     public void activate() {
         providerService = providerRegistry.register(this);
-        controller.addPacketListener(0, listener);
+        controller.addPacketListener(10, listener);
 
         log.info("Started");
     }
@@ -92,7 +93,7 @@
                 HostId hid = HostId.hostId(
                         eth.getSourceMAC(), vlan);
                 HostLocation hloc = new HostLocation(
-                        DeviceId.deviceId("of:" + Long.toHexString(pktCtx.dpid().value())),
+                        DeviceId.deviceId(Dpid.uri(pktCtx.dpid())),
                         PortNumber.portNumber(pktCtx.inPort()),
                         System.currentTimeMillis());
                 ARP arp = (ARP) eth.getPayload();