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; |
| 5 | import org.onlab.onos.net.Device; |
| 6 | import org.onlab.onos.net.device.DeviceService; |
| 7 | |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 8 | import java.util.Collections; |
| 9 | import java.util.Comparator; |
| 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 | |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 24 | 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 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 31 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame^] | 32 | protected void execute() { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 33 | DeviceService service = getService(DeviceService.class); |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 34 | for (Device device : getSortedDevices(service)) { |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 35 | printDevice(service, device); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 36 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 37 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 40 | * Returns the list of devices sorted using the device ID URIs. |
| 41 | * |
| 42 | * @param service device service |
| 43 | * @return sorted device list |
| 44 | */ |
| 45 | protected List<Device> getSortedDevices(DeviceService service) { |
| 46 | List<Device> devices = newArrayList(service.getDevices()); |
| 47 | Collections.sort(devices, ID_COMPARATOR); |
| 48 | return devices; |
| 49 | } |
| 50 | |
| 51 | /** |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 52 | * Prints information about the specified device. |
| 53 | * |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 54 | * @param service device service |
| 55 | * @param device infrastructure device |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 56 | */ |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 57 | protected void printDevice(DeviceService service, Device device) { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 58 | if (device != null) { |
| 59 | print(FMT, device.id(), service.isAvailable(device.id()), |
| 60 | service.getRole(device.id()), device.type(), |
| 61 | device.manufacturer(), device.hwVersion(), device.swVersion(), |
| 62 | device.serialNumber()); |
| 63 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 66 | } |