Provide a map between all devices and their driver names

- Introduce a new Java API in DriverService
- Implement a new CLI command device-drivers that dumps all devices and their driver names or a driver name of a device
- Include this CLI command as part of onos-diags

Change-Id: I978690e6af6c00dbfc09259b50449779b1167754
(cherry picked from commit dfaf4622e00256a8063ec4f12765381f7290a476)
diff --git a/cli/src/main/java/org/onosproject/cli/net/DeviceDriversCommand.java b/cli/src/main/java/org/onosproject/cli/net/DeviceDriversCommand.java
new file mode 100644
index 0000000..62103f9
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/DeviceDriversCommand.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.driver.DriverService;
+
+import java.util.Map;
+
+@Service
+@Command(scope = "onos", name = "device-drivers",
+        description = "list all devices and their driver names or a driver name of a device")
+public class DeviceDriversCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "uri", description = "Device ID",
+            required = false, multiValued = false)
+    @Completion(DeviceIdCompleter.class)
+    String uri = null;
+
+    @Override
+    protected void doExecute() {
+        DriverService service = get(DriverService.class);
+
+        if (uri == null) {
+            Map<DeviceId, String> deviceDriverNameMap = service.getDeviceDrivers();
+            if (outputJson()) {
+                json(deviceDriverNameMap);
+            } else {
+                deviceDriverNameMap.forEach((k, v) -> print("%s : %s", k.toString(), v));
+            }
+        } else {
+            DeviceId deviceId = DeviceId.deviceId(uri);
+            String driverName = service.getDriver(deviceId).name();
+            if (outputJson()) {
+                json(deviceId, driverName);
+            } else {
+                print("%s : %s", deviceId.toString(), driverName);
+            }
+        }
+    }
+
+    private void json(Map<DeviceId, String> map) {
+        ObjectNode result = mapper().createObjectNode();
+        map.forEach((k, v) -> {
+            result.put(k.toString(), v);
+        });
+        print("%s", result.toString());
+    }
+
+    private void json(DeviceId deviceId, String driverName) {
+        ObjectNode result = mapper().createObjectNode();
+        result.put(deviceId.toString(), driverName);
+        print("%s", result.toString());
+    }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverService.java b/core/api/src/main/java/org/onosproject/net/driver/DriverService.java
index e4432d1..2b7930b 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DriverService.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverService.java
@@ -17,6 +17,7 @@
 
 import org.onosproject.net.DeviceId;
 
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -57,6 +58,13 @@
     Driver getDriver(DeviceId deviceId);
 
     /**
+     * Returns a map between all devices and their driver names.
+     *
+     * @return map of (device id, driver name)
+     */
+    Map<DeviceId, String> getDeviceDrivers();
+
+    /**
      * Creates a new driver handler for interacting with the specified device.
      * The driver is looked-up using the same semantics as
      * {@link #getDriver(DeviceId)} method.
diff --git a/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java b/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java
index b2644db..776648d 100644
--- a/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java
+++ b/core/api/src/test/java/org/onosproject/net/driver/DriverServiceAdapter.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.driver;
 
+import java.util.Map;
 import java.util.Set;
 
 import org.onosproject.net.DeviceId;
@@ -55,6 +56,11 @@
     }
 
     @Override
+    public Map<DeviceId, String> getDeviceDrivers() {
+        return null;
+    }
+
+    @Override
     public void addListener(DriverListener listener) {
     }
 
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
index 31032b3..1814722 100644
--- a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
@@ -40,6 +40,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -150,6 +152,15 @@
                               NO_DRIVER);
     }
 
+    @Override
+    public Map<DeviceId, String> getDeviceDrivers() {
+        Map<DeviceId, String> deviceDriverNameMap = new HashMap<>();
+        deviceService.getDevices().forEach(device -> {
+            deviceDriverNameMap.put(device.id(), getDriver(device.id()).name());
+        });
+        return deviceDriverNameMap;
+    }
+
     private Driver getPipeconfMergedDriver(DeviceId deviceId) {
         PiPipeconfId pipeconfId = pipeconfService.ofDevice(deviceId).orElse(null);
         if (pipeconfId == null) {
diff --git a/tools/package/runtime/bin/onos-diagnostics b/tools/package/runtime/bin/onos-diagnostics
index 9a085ee..c2e9f77 100755
--- a/tools/package/runtime/bin/onos-diagnostics
+++ b/tools/package/runtime/bin/onos-diagnostics
@@ -70,6 +70,7 @@
     "cfg get"
 
     "devices"
+    "device-drivers"
     "links"
     "hosts"
     "interfaces"