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.Argument; |
| 4 | import org.apache.karaf.shell.commands.Command; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame^] | 5 | import org.onlab.onos.cli.Comparators; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.Device; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.Port; |
| 8 | import org.onlab.onos.net.device.DeviceService; |
| 9 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 10 | import java.util.ArrayList; |
| 11 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 12 | import java.util.List; |
| 13 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 14 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 15 | |
| 16 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 17 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 18 | */ |
| 19 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 20 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 21 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 22 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 23 | private static final String FMT = " port=%s, state=%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 24 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 25 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 26 | required = false, multiValued = false) |
| 27 | String uri = null; |
| 28 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 30 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 31 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 32 | if (uri == null) { |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 33 | for (Device device : getSortedDevices(service)) { |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 34 | printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 35 | } |
| 36 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 37 | Device device = service.getDevice(deviceId(uri)); |
| 38 | if (device == null) { |
| 39 | error("No such device %s", uri); |
| 40 | } else { |
| 41 | printDevice(service, device); |
| 42 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 43 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 44 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 45 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 46 | @Override |
| 47 | protected void printDevice(DeviceService service, Device device) { |
| 48 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 49 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 50 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 51 | for (Port port : ports) { |
| 52 | print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); |
| 53 | } |
| 54 | } |
| 55 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 56 | } |