blob: 32b78300a56f5e71ddefd616aabfd40e8a7a67e3 [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;
tom8350ba52014-10-16 07:22:02 -07009import org.apache.karaf.shell.commands.Option;
tom91c7bd02014-09-25 22:50:44 -070010import org.onlab.onos.cli.Comparators;
tomff7eb7c2014-09-08 12:49:03 -070011import org.onlab.onos.net.Device;
tom6d2a43e2014-09-08 01:50:20 -070012import org.onlab.onos.net.Port;
13import org.onlab.onos.net.device.DeviceService;
14
tomff7eb7c2014-09-08 12:49:03 -070015import java.util.ArrayList;
16import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070017import java.util.List;
18
tom6d2a43e2014-09-08 01:50:20 -070019import static org.onlab.onos.net.DeviceId.deviceId;
20
21/**
tomc290a122014-09-08 14:27:13 -070022 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070023 */
24@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070025 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070026public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070027
tomff7eb7c2014-09-08 12:49:03 -070028 private static final String FMT = " port=%s, state=%s";
tom6d2a43e2014-09-08 01:50:20 -070029
tom8350ba52014-10-16 07:22:02 -070030 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
31 required = false, multiValued = false)
32 private boolean enabled = false;
33
34 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
35 required = false, multiValued = false)
36 private boolean disabled = false;
37
tomc290a122014-09-08 14:27:13 -070038 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070039 required = false, multiValued = false)
40 String uri = null;
41
tom6d2a43e2014-09-08 01:50:20 -070042 @Override
tom0872a172014-09-23 11:24:26 -070043 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070044 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070045 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070046 if (outputJson()) {
47 print("%s", jsonPorts(service, getSortedDevices(service)));
48 } else {
49 for (Device device : getSortedDevices(service)) {
50 printDevice(service, device);
51 }
tomff7eb7c2014-09-08 12:49:03 -070052 }
tom32085cf2014-10-16 00:04:33 -070053
tomff7eb7c2014-09-08 12:49:03 -070054 } else {
tom9eb57fb2014-09-11 19:42:38 -070055 Device device = service.getDevice(deviceId(uri));
56 if (device == null) {
57 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070058 } else if (outputJson()) {
59 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070060 } else {
61 printDevice(service, device);
62 }
tom6d2a43e2014-09-08 01:50:20 -070063 }
tom6d2a43e2014-09-08 01:50:20 -070064 }
tomff7eb7c2014-09-08 12:49:03 -070065
tom32085cf2014-10-16 00:04:33 -070066 /**
67 * Produces JSON array containing ports of the specified devices.
68 *
69 * @param service device service
70 * @param devices collection of devices
71 * @return JSON array
72 */
tom8350ba52014-10-16 07:22:02 -070073 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070074 ObjectMapper mapper = new ObjectMapper();
75 ArrayNode result = mapper.createArrayNode();
76 for (Device device : devices) {
77 result.add(jsonPorts(service, mapper, device));
78 }
79 return result;
80 }
81
82 /**
83 * Produces JSON array containing ports of the specified device.
84 *
85 * @param service device service
86 * @param mapper object mapper
87 * @param device infrastructure devices
88 * @return JSON array
89 */
tom8350ba52014-10-16 07:22:02 -070090 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -070091 ObjectNode result = mapper.createObjectNode();
92 ArrayNode ports = mapper.createArrayNode();
93 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -070094 if (isIncluded(port)) {
95 ports.add(mapper.createObjectNode()
96 .put("port", port.number().toString())
97 .put("isEnabled", port.isEnabled()));
98 }
tom32085cf2014-10-16 00:04:33 -070099 }
100 return result.put("device", device.id().toString()).set("ports", ports);
101 }
102
tom8350ba52014-10-16 07:22:02 -0700103 // Determines if a port should be included in output.
104 private boolean isIncluded(Port port) {
105 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
106 !enabled && !disabled;
107 }
108
tomc290a122014-09-08 14:27:13 -0700109 @Override
110 protected void printDevice(DeviceService service, Device device) {
111 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -0700112 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700113 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700114 for (Port port : ports) {
tom8350ba52014-10-16 07:22:02 -0700115 if (isIncluded(port)) {
116 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
117 }
tomff7eb7c2014-09-08 12:49:03 -0700118 }
119 }
120
tom6d2a43e2014-09-08 01:50:20 -0700121}