blob: 9d68fb81cb15762d6e385ead7654e0b8cfe09df5 [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/**
tomc290a122014-09-08 14:27:13 -070017 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070018 */
19@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070020 description = "Lists all ports or 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
tomc290a122014-09-08 14:27:13 -070025 @Argument(index = 0, name = "uri", 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
tom0872a172014-09-23 11:24:26 -070038 protected void execute() {
tom6d2a43e2014-09-08 01:50:20 -070039 DeviceService service = getService(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070040 if (uri == null) {
tomd79f7ec2014-09-08 13:29:18 -070041 for (Device device : getSortedDevices(service)) {
tomc290a122014-09-08 14:27:13 -070042 printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070043 }
44 } else {
tom9eb57fb2014-09-11 19:42:38 -070045 Device device = service.getDevice(deviceId(uri));
46 if (device == null) {
47 error("No such device %s", uri);
48 } else {
49 printDevice(service, device);
50 }
tom6d2a43e2014-09-08 01:50:20 -070051 }
tom6d2a43e2014-09-08 01:50:20 -070052 }
tomff7eb7c2014-09-08 12:49:03 -070053
tomc290a122014-09-08 14:27:13 -070054 @Override
55 protected void printDevice(DeviceService service, Device device) {
56 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070057 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
58 Collections.sort(ports, PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -070059 for (Port port : ports) {
60 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
61 }
62 }
63
tom6d2a43e2014-09-08 01:50:20 -070064}