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 | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 5 | import org.onlab.onos.net.Device; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.Port; |
| 7 | import org.onlab.onos.net.device.DeviceService; |
| 8 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 9 | import java.util.ArrayList; |
| 10 | import java.util.Collections; |
| 11 | import java.util.Comparator; |
| 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 | |
| 29 | private static final Comparator<Port> PORT_COMPARATOR = new Comparator<Port>() { |
| 30 | @Override |
| 31 | public int compare(Port p1, Port p2) { |
| 32 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 33 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 34 | } |
| 35 | }; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 36 | |
| 37 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 38 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 39 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 40 | if (uri == null) { |
tom | d79f7ec | 2014-09-08 13:29:18 -0700 | [diff] [blame] | 41 | for (Device device : getSortedDevices(service)) { |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 42 | printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 43 | } |
| 44 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 45 | Device device = service.getDevice(deviceId(uri)); |
| 46 | if (device == null) { |
| 47 | error("No such device %s", uri); |
| 48 | } else { |
| 49 | printDevice(service, device); |
| 50 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 51 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 52 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 53 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 54 | @Override |
| 55 | protected void printDevice(DeviceService service, Device device) { |
| 56 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 57 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
| 58 | Collections.sort(ports, PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 59 | for (Port port : ports) { |
| 60 | print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); |
| 61 | } |
| 62 | } |
| 63 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 64 | } |