blob: 1382896d4be653c1943f99d8a0a431ae92f877de [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;
tomff7eb7c2014-09-08 12:49:03 -070011import java.util.List;
12
tom6d2a43e2014-09-08 01:50:20 -070013import static org.onlab.onos.net.DeviceId.deviceId;
14
15/**
tomc290a122014-09-08 14:27:13 -070016 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070017 */
18@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070019 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070020public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070021
tomff7eb7c2014-09-08 12:49:03 -070022 private static final String FMT = " port=%s, state=%s";
tom6d2a43e2014-09-08 01:50:20 -070023
tomc290a122014-09-08 14:27:13 -070024 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070025 required = false, multiValued = false)
26 String uri = null;
27
tom6d2a43e2014-09-08 01:50:20 -070028 @Override
tom0872a172014-09-23 11:24:26 -070029 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070030 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070031 if (uri == null) {
tomd79f7ec2014-09-08 13:29:18 -070032 for (Device device : getSortedDevices(service)) {
tomc290a122014-09-08 14:27:13 -070033 printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070034 }
35 } else {
tom9eb57fb2014-09-11 19:42:38 -070036 Device device = service.getDevice(deviceId(uri));
37 if (device == null) {
38 error("No such device %s", uri);
39 } else {
40 printDevice(service, device);
41 }
tom6d2a43e2014-09-08 01:50:20 -070042 }
tom6d2a43e2014-09-08 01:50:20 -070043 }
tomff7eb7c2014-09-08 12:49:03 -070044
tomc290a122014-09-08 14:27:13 -070045 @Override
46 protected void printDevice(DeviceService service, Device device) {
47 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070048 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -070049 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -070050 for (Port port : ports) {
51 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
52 }
53 }
54
tom6d2a43e2014-09-08 01:50:20 -070055}