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 | /** |
| 17 | * Lists all infrastructure links. |
| 18 | */ |
| 19 | @Command(scope = "onos", name = "ports", |
| 20 | description = "Lists 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 | |
| 25 | @Argument(index = 0, name = "deviceId", 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 |
| 38 | protected Object doExecute() throws Exception { |
| 39 | DeviceService service = getService(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame^] | 40 | if (uri == null) { |
| 41 | for (Device device : service.getDevices()) { |
| 42 | printDevicePorts(service, device); |
| 43 | } |
| 44 | } else { |
| 45 | printDevicePorts(service, service.getDevice(deviceId(uri))); |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 46 | } |
| 47 | return null; |
| 48 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame^] | 49 | |
| 50 | private void printDevicePorts(DeviceService service, Device device) { |
| 51 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
| 52 | Collections.sort(ports, PORT_COMPARATOR); |
| 53 | printDevice(device, service.isAvailable(device.id())); |
| 54 | for (Port port : ports) { |
| 55 | print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); |
| 56 | } |
| 57 | } |
| 58 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 59 | } |