blob: 95f23affdecf79c77b93d7b2a69b7816160742c9 [file] [log] [blame]
tom6d2a43e2014-09-08 01:50:20 -07001package org.onlab.onos.cli.net;
2
3import org.apache.karaf.shell.commands.Argument;
4import org.apache.karaf.shell.commands.Command;
tomff7eb7c2014-09-08 12:49:03 -07005import org.onlab.onos.net.Device;
tom6d2a43e2014-09-08 01:50:20 -07006import org.onlab.onos.net.Port;
7import org.onlab.onos.net.device.DeviceService;
8
tomff7eb7c2014-09-08 12:49:03 -07009import java.util.ArrayList;
10import java.util.Collections;
11import java.util.Comparator;
12import java.util.List;
13
tom6d2a43e2014-09-08 01:50:20 -070014import 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")
tomff7eb7c2014-09-08 12:49:03 -070021public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070022
tomff7eb7c2014-09-08 12:49:03 -070023 private static final String FMT = " port=%s, state=%s";
tom6d2a43e2014-09-08 01:50:20 -070024
25 @Argument(index = 0, name = "deviceId", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070026 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 };
tom6d2a43e2014-09-08 01:50:20 -070036
37 @Override
38 protected Object doExecute() throws Exception {
39 DeviceService service = getService(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070040 if (uri == null) {
41 for (Device device : service.getDevices()) {
42 printDevicePorts(service, device);
43 }
44 } else {
45 printDevicePorts(service, service.getDevice(deviceId(uri)));
tom6d2a43e2014-09-08 01:50:20 -070046 }
47 return null;
48 }
tomff7eb7c2014-09-08 12:49:03 -070049
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
tom6d2a43e2014-09-08 01:50:20 -070059}