blob: 0044d0c7b8ed7be43106cda0d05dc4855aadf9ae [file] [log] [blame]
tom6d2a43e2014-09-08 01:50:20 -07001package org.onlab.onos.cli.net;
2
tom32085cf2014-10-16 00:04:33 -07003import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
6import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -07007import org.apache.karaf.shell.commands.Argument;
8import org.apache.karaf.shell.commands.Command;
tom91c7bd02014-09-25 22:50:44 -07009import org.onlab.onos.cli.Comparators;
tomff7eb7c2014-09-08 12:49:03 -070010import org.onlab.onos.net.Device;
tom6d2a43e2014-09-08 01:50:20 -070011import org.onlab.onos.net.Port;
12import org.onlab.onos.net.device.DeviceService;
13
tomff7eb7c2014-09-08 12:49:03 -070014import java.util.ArrayList;
15import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070016import java.util.List;
17
tom6d2a43e2014-09-08 01:50:20 -070018import static org.onlab.onos.net.DeviceId.deviceId;
19
20/**
tomc290a122014-09-08 14:27:13 -070021 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070022 */
23@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070024 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070025public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070026
tomff7eb7c2014-09-08 12:49:03 -070027 private static final String FMT = " port=%s, state=%s";
tom6d2a43e2014-09-08 01:50:20 -070028
tomc290a122014-09-08 14:27:13 -070029 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070030 required = false, multiValued = false)
31 String uri = null;
32
tom6d2a43e2014-09-08 01:50:20 -070033 @Override
tom0872a172014-09-23 11:24:26 -070034 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070035 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070036 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070037 if (outputJson()) {
38 print("%s", jsonPorts(service, getSortedDevices(service)));
39 } else {
40 for (Device device : getSortedDevices(service)) {
41 printDevice(service, device);
42 }
tomff7eb7c2014-09-08 12:49:03 -070043 }
tom32085cf2014-10-16 00:04:33 -070044
tomff7eb7c2014-09-08 12:49:03 -070045 } else {
tom9eb57fb2014-09-11 19:42:38 -070046 Device device = service.getDevice(deviceId(uri));
47 if (device == null) {
48 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070049 } else if (outputJson()) {
50 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070051 } else {
52 printDevice(service, device);
53 }
tom6d2a43e2014-09-08 01:50:20 -070054 }
tom6d2a43e2014-09-08 01:50:20 -070055 }
tomff7eb7c2014-09-08 12:49:03 -070056
tom32085cf2014-10-16 00:04:33 -070057 /**
58 * Produces JSON array containing ports of the specified devices.
59 *
60 * @param service device service
61 * @param devices collection of devices
62 * @return JSON array
63 */
64 public static JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
65 ObjectMapper mapper = new ObjectMapper();
66 ArrayNode result = mapper.createArrayNode();
67 for (Device device : devices) {
68 result.add(jsonPorts(service, mapper, device));
69 }
70 return result;
71 }
72
73 /**
74 * Produces JSON array containing ports of the specified device.
75 *
76 * @param service device service
77 * @param mapper object mapper
78 * @param device infrastructure devices
79 * @return JSON array
80 */
81 public static JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
82 ObjectNode result = mapper.createObjectNode();
83 ArrayNode ports = mapper.createArrayNode();
84 for (Port port : service.getPorts(device.id())) {
85 ports.add(mapper.createObjectNode()
86 .put("port", port.number().toString())
87 .put("isEnabled", port.isEnabled()));
88 }
89 return result.put("device", device.id().toString()).set("ports", ports);
90 }
91
tomc290a122014-09-08 14:27:13 -070092 @Override
93 protected void printDevice(DeviceService service, Device device) {
94 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -070095 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -070096 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -070097 for (Port port : ports) {
98 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
99 }
100 }
101
tom6d2a43e2014-09-08 01:50:20 -0700102}