tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
| 3 | import org.apache.karaf.shell.commands.Command; |
| 4 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 5 | import org.onlab.onos.cli.Comparators; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.Device; |
| 7 | import org.onlab.onos.net.device.DeviceService; |
| 8 | |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 9 | import java.util.Collections; |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 10 | import java.util.List; |
| 11 | |
| 12 | import static com.google.common.collect.Lists.newArrayList; |
| 13 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 14 | /** |
| 15 | * Lists all infrastructure devices. |
| 16 | */ |
| 17 | @Command(scope = "onos", name = "devices", |
| 18 | description = "Lists all infrastructure devices") |
| 19 | public class DevicesListCommand extends AbstractShellCommand { |
| 20 | |
| 21 | private static final String FMT = |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 22 | "id=%s, available=%s, role=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 23 | |
| 24 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 25 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 26 | DeviceService service = get(DeviceService.class); |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 27 | for (Device device : getSortedDevices(service)) { |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 28 | printDevice(service, device); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 30 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 33 | * Returns the list of devices sorted using the device ID URIs. |
| 34 | * |
| 35 | * @param service device service |
| 36 | * @return sorted device list |
| 37 | */ |
tom | 53945d5 | 2014-10-07 11:01:36 -0700 | [diff] [blame] | 38 | protected static List<Device> getSortedDevices(DeviceService service) { |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 39 | List<Device> devices = newArrayList(service.getDevices()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 40 | Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 41 | return devices; |
| 42 | } |
| 43 | |
| 44 | /** |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 45 | * Prints information about the specified device. |
| 46 | * |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 47 | * @param service device service |
| 48 | * @param device infrastructure device |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 49 | */ |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 50 | protected void printDevice(DeviceService service, Device device) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 51 | if (device != null) { |
| 52 | print(FMT, device.id(), service.isAvailable(device.id()), |
| 53 | service.getRole(device.id()), device.type(), |
| 54 | device.manufacturer(), device.hwVersion(), device.swVersion(), |
| 55 | device.serialNumber()); |
| 56 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 59 | } |