blob: 650e6a01b7a592558cbd03649624bf5cc61e64a5 [file] [log] [blame]
tom6d2a43e2014-09-08 01:50:20 -07001package org.onlab.onos.cli.net;
2
3import org.apache.karaf.shell.commands.Command;
4import org.onlab.onos.cli.AbstractShellCommand;
5import org.onlab.onos.net.Device;
6import org.onlab.onos.net.device.DeviceService;
7
tomd79f7ec2014-09-08 13:29:18 -07008import java.util.Collections;
9import java.util.Comparator;
10import java.util.List;
11
12import static com.google.common.collect.Lists.newArrayList;
13
tom6d2a43e2014-09-08 01:50:20 -070014/**
15 * Lists all infrastructure devices.
16 */
17@Command(scope = "onos", name = "devices",
18 description = "Lists all infrastructure devices")
19public class DevicesListCommand extends AbstractShellCommand {
20
21 private static final String FMT =
tomff7eb7c2014-09-08 12:49:03 -070022 "id=%s, available=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s";
tom6d2a43e2014-09-08 01:50:20 -070023
tomd79f7ec2014-09-08 13:29:18 -070024 protected static final Comparator<Device> ID_COMPARATOR = new Comparator<Device>() {
25 @Override
26 public int compare(Device d1, Device d2) {
27 return d1.id().uri().toString().compareTo(d2.id().uri().toString());
28 }
29 };
30
tom6d2a43e2014-09-08 01:50:20 -070031 @Override
32 protected Object doExecute() throws Exception {
tomff7eb7c2014-09-08 12:49:03 -070033 DeviceService service = getService(DeviceService.class);
tomd79f7ec2014-09-08 13:29:18 -070034 for (Device device : getSortedDevices(service)) {
tomff7eb7c2014-09-08 12:49:03 -070035 printDevice(device, service.isAvailable(device.id()));
tom6d2a43e2014-09-08 01:50:20 -070036 }
37 return null;
38 }
tomff7eb7c2014-09-08 12:49:03 -070039
40 /**
tomd79f7ec2014-09-08 13:29:18 -070041 * Returns the list of devices sorted using the device ID URIs.
42 *
43 * @param service device service
44 * @return sorted device list
45 */
46 protected List<Device> getSortedDevices(DeviceService service) {
47 List<Device> devices = newArrayList(service.getDevices());
48 Collections.sort(devices, ID_COMPARATOR);
49 return devices;
50 }
51
52 /**
tomff7eb7c2014-09-08 12:49:03 -070053 * Prints information about the specified device.
54 *
55 * @param device infrastructure device
56 * @param isAvailable true of device is available
57 */
58 protected void printDevice(Device device, boolean isAvailable) {
59 print(FMT, device.id(), isAvailable, device.type(),
60 device.manufacturer(), device.hwVersion(), device.swVersion(),
61 device.serialNumber());
62 }
63
tom6d2a43e2014-09-08 01:50:20 -070064}