blob: f66ceddfff44d4ce199faaf6a3f27da1169847aa [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;
tom91c7bd02014-09-25 22:50:44 -07005import org.onlab.onos.cli.Comparators;
tomff7eb7c2014-09-08 12:49:03 -07006import org.onlab.onos.net.Device;
tom6d2a43e2014-09-08 01:50:20 -07007import org.onlab.onos.net.Port;
8import org.onlab.onos.net.device.DeviceService;
9
tomff7eb7c2014-09-08 12:49:03 -070010import java.util.ArrayList;
11import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070012import 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
tom6d2a43e2014-09-08 01:50:20 -070029 @Override
tom0872a172014-09-23 11:24:26 -070030 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070031 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070032 if (uri == null) {
tomd79f7ec2014-09-08 13:29:18 -070033 for (Device device : getSortedDevices(service)) {
tomc290a122014-09-08 14:27:13 -070034 printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070035 }
36 } else {
tom9eb57fb2014-09-11 19:42:38 -070037 Device device = service.getDevice(deviceId(uri));
38 if (device == null) {
39 error("No such device %s", uri);
40 } else {
41 printDevice(service, device);
42 }
tom6d2a43e2014-09-08 01:50:20 -070043 }
tom6d2a43e2014-09-08 01:50:20 -070044 }
tomff7eb7c2014-09-08 12:49:03 -070045
tomc290a122014-09-08 14:27:13 -070046 @Override
47 protected void printDevice(DeviceService service, Device device) {
48 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070049 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -070050 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -070051 for (Port port : ports) {
52 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
53 }
54 }
55
tom6d2a43e2014-09-08 01:50:20 -070056}