Andrea Campanella
Committed by Gerrit Code Review

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

Change-Id: I1ab9e9865c499240256aa72760b225976e0f19d2
...@@ -29,6 +29,9 @@ import org.onosproject.net.Annotations; ...@@ -29,6 +29,9 @@ import org.onosproject.net.Annotations;
29 import com.fasterxml.jackson.databind.ObjectMapper; 29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ObjectNode; 30 import com.fasterxml.jackson.databind.node.ObjectNode;
31 31
32 +import java.util.Set;
33 +import java.util.TreeSet;
34 +
32 /** 35 /**
33 * Base abstraction of Karaf shell commands. 36 * Base abstraction of Karaf shell commands.
34 */ 37 */
...@@ -95,6 +98,24 @@ public abstract class AbstractShellCommand extends AbstractAction implements Cod ...@@ -95,6 +98,24 @@ public abstract class AbstractShellCommand extends AbstractAction implements Cod
95 } 98 }
96 99
97 /** 100 /**
101 + * Produces a string image of the specified key/value annotations.
102 + * Excludes the keys in the given Set.
103 + *
104 + * @param annotations key/value annotations
105 + * @param excludedKeys keys not to add in the resulting string
106 + * @return string image with ", k1=v1, k2=v2, ..." pairs
107 + */
108 + public static String annotations(Annotations annotations, Set<String> excludedKeys) {
109 + StringBuilder sb = new StringBuilder();
110 + Set<String> keys = new TreeSet<>(annotations.keys());
111 + keys.removeAll(excludedKeys);
112 + for (String key : keys) {
113 + sb.append(", ").append(key).append('=').append(annotations.value(key));
114 + }
115 + return sb.toString();
116 + }
117 +
118 + /**
98 * Produces a JSON object from the specified key/value annotations. 119 * Produces a JSON object from the specified key/value annotations.
99 * 120 *
100 * @param mapper ObjectMapper to use while converting to JSON 121 * @param mapper ObjectMapper to use while converting to JSON
......
...@@ -15,18 +15,21 @@ ...@@ -15,18 +15,21 @@
15 */ 15 */
16 package org.onosproject.cli.net; 16 package org.onosproject.cli.net;
17 17
18 -import java.util.Collections; 18 +import com.fasterxml.jackson.databind.JsonNode;
19 -import java.util.List; 19 +import com.fasterxml.jackson.databind.ObjectMapper;
20 - 20 +import com.fasterxml.jackson.databind.node.ArrayNode;
21 +import com.google.common.collect.ImmutableSet;
21 import org.apache.karaf.shell.commands.Command; 22 import org.apache.karaf.shell.commands.Command;
23 +import org.apache.karaf.shell.commands.Option;
22 import org.onosproject.cli.AbstractShellCommand; 24 import org.onosproject.cli.AbstractShellCommand;
23 -import org.onosproject.utils.Comparators; 25 +import org.onosproject.net.AnnotationKeys;
24 import org.onosproject.net.Device; 26 import org.onosproject.net.Device;
25 import org.onosproject.net.device.DeviceService; 27 import org.onosproject.net.device.DeviceService;
28 +import org.onosproject.net.driver.DriverService;
29 +import org.onosproject.utils.Comparators;
26 30
27 -import com.fasterxml.jackson.databind.JsonNode; 31 +import java.util.Collections;
28 -import com.fasterxml.jackson.databind.ObjectMapper; 32 +import java.util.List;
29 -import com.fasterxml.jackson.databind.node.ArrayNode;
30 33
31 import static com.google.common.collect.Lists.newArrayList; 34 import static com.google.common.collect.Lists.newArrayList;
32 35
...@@ -34,20 +37,27 @@ import static com.google.common.collect.Lists.newArrayList; ...@@ -34,20 +37,27 @@ import static com.google.common.collect.Lists.newArrayList;
34 * Lists all infrastructure devices. 37 * Lists all infrastructure devices.
35 */ 38 */
36 @Command(scope = "onos", name = "devices", 39 @Command(scope = "onos", name = "devices",
37 - description = "Lists all infrastructure devices") 40 + description = "Lists all infrastructure devices")
38 public class DevicesListCommand extends AbstractShellCommand { 41 public class DevicesListCommand extends AbstractShellCommand {
39 42
40 private static final String FMT = 43 private static final String FMT =
41 - "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s%s"; 44 + "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s, driver=%s%s";
45 +
46 + private static final String FMT_SHORT =
47 + "id=%s, available=%s, role=%s, type=%s, driver=%s";
48 +
49 + @Option(name = "-s", aliases = "--short", description = "Show short output only",
50 + required = false, multiValued = false)
51 + private boolean shortOnly = false;
42 52
43 @Override 53 @Override
44 protected void execute() { 54 protected void execute() {
45 - DeviceService service = get(DeviceService.class); 55 + DeviceService deviceService = get(DeviceService.class);
46 if (outputJson()) { 56 if (outputJson()) {
47 - print("%s", json(getSortedDevices(service))); 57 + print("%s", json(getSortedDevices(deviceService)));
48 } else { 58 } else {
49 - for (Device device : getSortedDevices(service)) { 59 + for (Device device : getSortedDevices(deviceService)) {
50 - printDevice(service, device); 60 + printDevice(deviceService, device);
51 } 61 }
52 } 62 }
53 } 63 }
...@@ -82,16 +92,22 @@ public class DevicesListCommand extends AbstractShellCommand { ...@@ -82,16 +92,22 @@ public class DevicesListCommand extends AbstractShellCommand {
82 /** 92 /**
83 * Prints information about the specified device. 93 * Prints information about the specified device.
84 * 94 *
85 - * @param service device service 95 + * @param deviceService device service
86 - * @param device infrastructure device 96 + * @param device infrastructure device
87 */ 97 */
88 - protected void printDevice(DeviceService service, Device device) { 98 + protected void printDevice(DeviceService deviceService, Device device) {
89 if (device != null) { 99 if (device != null) {
90 - print(FMT, device.id(), service.isAvailable(device.id()), 100 + String driver = get(DriverService.class).getDriver(device.id()).name();
91 - service.getRole(device.id()), device.type(), 101 + if (shortOnly) {
92 - device.manufacturer(), device.hwVersion(), device.swVersion(), 102 + print(FMT_SHORT, device.id(), deviceService.isAvailable(device.id()),
93 - device.serialNumber(), annotations(device.annotations())); 103 + deviceService.getRole(device.id()), device.type(), driver);
104 + } else {
105 + print(FMT, device.id(), deviceService.isAvailable(device.id()),
106 + deviceService.getRole(device.id()), device.type(),
107 + device.manufacturer(), device.hwVersion(), device.swVersion(),
108 + device.serialNumber(), driver,
109 + annotations(device.annotations(), ImmutableSet.of(AnnotationKeys.DRIVER)));
110 + }
94 } 111 }
95 } 112 }
96 -
97 } 113 }
......
...@@ -15,18 +15,18 @@ ...@@ -15,18 +15,18 @@
15 */ 15 */
16 package org.onosproject.cli.net; 16 package org.onosproject.cli.net;
17 17
18 -import java.util.Collections; 18 +import com.fasterxml.jackson.databind.JsonNode;
19 -import java.util.List; 19 +import com.fasterxml.jackson.databind.ObjectMapper;
20 - 20 +import com.fasterxml.jackson.databind.node.ArrayNode;
21 import org.apache.karaf.shell.commands.Command; 21 import org.apache.karaf.shell.commands.Command;
22 +import org.apache.karaf.shell.commands.Option;
22 import org.onosproject.cli.AbstractShellCommand; 23 import org.onosproject.cli.AbstractShellCommand;
23 -import org.onosproject.utils.Comparators;
24 import org.onosproject.net.Host; 24 import org.onosproject.net.Host;
25 import org.onosproject.net.host.HostService; 25 import org.onosproject.net.host.HostService;
26 +import org.onosproject.utils.Comparators;
26 27
27 -import com.fasterxml.jackson.databind.JsonNode; 28 +import java.util.Collections;
28 -import com.fasterxml.jackson.databind.ObjectMapper; 29 +import java.util.List;
29 -import com.fasterxml.jackson.databind.node.ArrayNode;
30 30
31 import static com.google.common.collect.Lists.newArrayList; 31 import static com.google.common.collect.Lists.newArrayList;
32 32
...@@ -34,12 +34,19 @@ import static com.google.common.collect.Lists.newArrayList; ...@@ -34,12 +34,19 @@ import static com.google.common.collect.Lists.newArrayList;
34 * Lists all currently-known hosts. 34 * Lists all currently-known hosts.
35 */ 35 */
36 @Command(scope = "onos", name = "hosts", 36 @Command(scope = "onos", name = "hosts",
37 - description = "Lists all currently-known hosts.") 37 + description = "Lists all currently-known hosts.")
38 public class HostsListCommand extends AbstractShellCommand { 38 public class HostsListCommand extends AbstractShellCommand {
39 39
40 private static final String FMT = 40 private static final String FMT =
41 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s"; 41 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s";
42 42
43 + private static final String FMT_SHORT =
44 + "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
45 +
46 + @Option(name = "-s", aliases = "--short", description = "Show short output only",
47 + required = false, multiValued = false)
48 + private boolean shortOnly = false;
49 +
43 @Override 50 @Override
44 protected void execute() { 51 protected void execute() {
45 HostService service = get(HostService.class); 52 HostService service = get(HostService.class);
...@@ -79,12 +86,14 @@ public class HostsListCommand extends AbstractShellCommand { ...@@ -79,12 +86,14 @@ public class HostsListCommand extends AbstractShellCommand {
79 * @param host end-station host 86 * @param host end-station host
80 */ 87 */
81 protected void printHost(Host host) { 88 protected void printHost(Host host) {
82 - if (host != null) { 89 + if (shortOnly) {
90 + print(FMT_SHORT, host.id(), host.mac(),
91 + host.location().deviceId(), host.location().port(),
92 + host.vlan(), host.ipAddresses());
93 + } else {
83 print(FMT, host.id(), host.mac(), 94 print(FMT, host.id(), host.mac(),
84 - host.location().deviceId(), 95 + host.location().deviceId(), host.location().port(),
85 - host.location().port(), 96 + host.vlan(), host.ipAddresses(), annotations(host.annotations()));
86 - host.vlan(), host.ipAddresses(),
87 - annotations(host.annotations()));
88 } 97 }
89 } 98 }
90 } 99 }
......