Adding driver information and short version to cli print of devices and hosts

Change-Id: I1ab9e9865c499240256aa72760b225976e0f19d2
diff --git a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
index 8945d62..6305f76 100644
--- a/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/AbstractShellCommand.java
@@ -29,6 +29,9 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
+import java.util.Set;
+import java.util.TreeSet;
+
 /**
  * Base abstraction of Karaf shell commands.
  */
@@ -95,6 +98,24 @@
     }
 
     /**
+     * Produces a string image of the specified key/value annotations.
+     * Excludes the keys in the given Set.
+     *
+     * @param annotations  key/value annotations
+     * @param excludedKeys keys not to add in the resulting string
+     * @return string image with ", k1=v1, k2=v2, ..." pairs
+     */
+    public static String annotations(Annotations annotations, Set<String> excludedKeys) {
+        StringBuilder sb = new StringBuilder();
+        Set<String> keys = new TreeSet<>(annotations.keys());
+        keys.removeAll(excludedKeys);
+        for (String key : keys) {
+            sb.append(", ").append(key).append('=').append(annotations.value(key));
+        }
+        return sb.toString();
+    }
+
+    /**
      * Produces a JSON object from the specified key/value annotations.
      *
      * @param mapper ObjectMapper to use while converting to JSON
diff --git a/cli/src/main/java/org/onosproject/cli/net/DevicesListCommand.java b/cli/src/main/java/org/onosproject/cli/net/DevicesListCommand.java
index f558a77..2c0f664 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DevicesListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DevicesListCommand.java
@@ -15,18 +15,21 @@
  */
 package org.onosproject.cli.net;
 
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.karaf.shell.commands.Command;
-import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.utils.Comparators;
-import org.onosproject.net.Device;
-import org.onosproject.net.device.DeviceService;
-
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.ImmutableSet;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Device;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.utils.Comparators;
+
+import java.util.Collections;
+import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 
@@ -34,20 +37,27 @@
  * Lists all infrastructure devices.
  */
 @Command(scope = "onos", name = "devices",
-         description = "Lists all infrastructure devices")
+        description = "Lists all infrastructure devices")
 public class DevicesListCommand extends AbstractShellCommand {
 
     private static final String FMT =
-            "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s%s";
+            "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s, driver=%s%s";
+
+    private static final String FMT_SHORT =
+            "id=%s, available=%s, role=%s, type=%s, driver=%s";
+
+    @Option(name = "-s", aliases = "--short", description = "Show short output only",
+            required = false, multiValued = false)
+    private boolean shortOnly = false;
 
     @Override
     protected void execute() {
-        DeviceService service = get(DeviceService.class);
+        DeviceService deviceService = get(DeviceService.class);
         if (outputJson()) {
-            print("%s", json(getSortedDevices(service)));
+            print("%s", json(getSortedDevices(deviceService)));
         } else {
-            for (Device device : getSortedDevices(service)) {
-                printDevice(service, device);
+            for (Device device : getSortedDevices(deviceService)) {
+                printDevice(deviceService, device);
             }
         }
     }
@@ -82,16 +92,22 @@
     /**
      * Prints information about the specified device.
      *
-     * @param service device service
-     * @param device  infrastructure device
+     * @param deviceService device service
+     * @param device        infrastructure device
      */
-    protected void printDevice(DeviceService service, Device device) {
+    protected void printDevice(DeviceService deviceService, Device device) {
         if (device != null) {
-            print(FMT, device.id(), service.isAvailable(device.id()),
-                  service.getRole(device.id()), device.type(),
-                  device.manufacturer(), device.hwVersion(), device.swVersion(),
-                  device.serialNumber(), annotations(device.annotations()));
+            String driver = get(DriverService.class).getDriver(device.id()).name();
+            if (shortOnly) {
+                print(FMT_SHORT, device.id(), deviceService.isAvailable(device.id()),
+                      deviceService.getRole(device.id()), device.type(), driver);
+            } else {
+                print(FMT, device.id(), deviceService.isAvailable(device.id()),
+                      deviceService.getRole(device.id()), device.type(),
+                      device.manufacturer(), device.hwVersion(), device.swVersion(),
+                      device.serialNumber(), driver,
+                      annotations(device.annotations(), ImmutableSet.of(AnnotationKeys.DRIVER)));
+            }
         }
     }
-
 }
diff --git a/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java
index 8fd12ba..e2032c3 100644
--- a/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/HostsListCommand.java
@@ -15,18 +15,18 @@
  */
 package org.onosproject.cli.net;
 
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.karaf.shell.commands.Command;
-import org.onosproject.cli.AbstractShellCommand;
-import org.onosproject.utils.Comparators;
-import org.onosproject.net.Host;
-import org.onosproject.net.host.HostService;
-
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.Host;
+import org.onosproject.net.host.HostService;
+import org.onosproject.utils.Comparators;
+
+import java.util.Collections;
+import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 
@@ -34,12 +34,19 @@
  * Lists all currently-known hosts.
  */
 @Command(scope = "onos", name = "hosts",
-         description = "Lists all currently-known 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%s";
 
+    private static final String FMT_SHORT =
+            "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
+
+    @Option(name = "-s", aliases = "--short", description = "Show short output only",
+            required = false, multiValued = false)
+    private boolean shortOnly = false;
+
     @Override
     protected void execute() {
         HostService service = get(HostService.class);
@@ -79,12 +86,14 @@
      * @param host end-station host
      */
     protected void printHost(Host host) {
-        if (host != null) {
+        if (shortOnly) {
+            print(FMT_SHORT, host.id(), host.mac(),
+                  host.location().deviceId(), host.location().port(),
+                  host.vlan(), host.ipAddresses());
+        } else {
             print(FMT, host.id(), host.mac(),
-                  host.location().deviceId(),
-                  host.location().port(),
-                  host.vlan(), host.ipAddresses(),
-                  annotations(host.annotations()));
+                  host.location().deviceId(), host.location().port(),
+                  host.vlan(), host.ipAddresses(), annotations(host.annotations()));
         }
     }
 }