tom

Added option to show only enabled/disabled ports

...@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; ...@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
6 import com.fasterxml.jackson.databind.node.ObjectNode; 6 import com.fasterxml.jackson.databind.node.ObjectNode;
7 import org.apache.karaf.shell.commands.Argument; 7 import org.apache.karaf.shell.commands.Argument;
8 import org.apache.karaf.shell.commands.Command; 8 import org.apache.karaf.shell.commands.Command;
9 +import org.apache.karaf.shell.commands.Option;
9 import org.onlab.onos.cli.Comparators; 10 import org.onlab.onos.cli.Comparators;
10 import org.onlab.onos.net.Device; 11 import org.onlab.onos.net.Device;
11 import org.onlab.onos.net.Port; 12 import org.onlab.onos.net.Port;
...@@ -26,6 +27,14 @@ public class DevicePortsListCommand extends DevicesListCommand { ...@@ -26,6 +27,14 @@ public class DevicePortsListCommand extends DevicesListCommand {
26 27
27 private static final String FMT = " port=%s, state=%s"; 28 private static final String FMT = " port=%s, state=%s";
28 29
30 + @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 +
29 @Argument(index = 0, name = "uri", description = "Device ID", 38 @Argument(index = 0, name = "uri", description = "Device ID",
30 required = false, multiValued = false) 39 required = false, multiValued = false)
31 String uri = null; 40 String uri = null;
...@@ -61,7 +70,7 @@ public class DevicePortsListCommand extends DevicesListCommand { ...@@ -61,7 +70,7 @@ public class DevicePortsListCommand extends DevicesListCommand {
61 * @param devices collection of devices 70 * @param devices collection of devices
62 * @return JSON array 71 * @return JSON array
63 */ 72 */
64 - public static JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { 73 + public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
65 ObjectMapper mapper = new ObjectMapper(); 74 ObjectMapper mapper = new ObjectMapper();
66 ArrayNode result = mapper.createArrayNode(); 75 ArrayNode result = mapper.createArrayNode();
67 for (Device device : devices) { 76 for (Device device : devices) {
...@@ -78,24 +87,34 @@ public class DevicePortsListCommand extends DevicesListCommand { ...@@ -78,24 +87,34 @@ public class DevicePortsListCommand extends DevicesListCommand {
78 * @param device infrastructure devices 87 * @param device infrastructure devices
79 * @return JSON array 88 * @return JSON array
80 */ 89 */
81 - public static JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { 90 + public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
82 ObjectNode result = mapper.createObjectNode(); 91 ObjectNode result = mapper.createObjectNode();
83 ArrayNode ports = mapper.createArrayNode(); 92 ArrayNode ports = mapper.createArrayNode();
84 for (Port port : service.getPorts(device.id())) { 93 for (Port port : service.getPorts(device.id())) {
85 - ports.add(mapper.createObjectNode() 94 + if (isIncluded(port)) {
86 - .put("port", port.number().toString()) 95 + ports.add(mapper.createObjectNode()
87 - .put("isEnabled", port.isEnabled())); 96 + .put("port", port.number().toString())
97 + .put("isEnabled", port.isEnabled()));
98 + }
88 } 99 }
89 return result.put("device", device.id().toString()).set("ports", ports); 100 return result.put("device", device.id().toString()).set("ports", ports);
90 } 101 }
91 102
103 + // 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 +
92 @Override 109 @Override
93 protected void printDevice(DeviceService service, Device device) { 110 protected void printDevice(DeviceService service, Device device) {
94 super.printDevice(service, device); 111 super.printDevice(service, device);
95 List<Port> ports = new ArrayList<>(service.getPorts(device.id())); 112 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
96 Collections.sort(ports, Comparators.PORT_COMPARATOR); 113 Collections.sort(ports, Comparators.PORT_COMPARATOR);
97 for (Port port : ports) { 114 for (Port port : ports) {
98 - print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); 115 + if (isIncluded(port)) {
116 + print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
117 + }
99 } 118 }
100 } 119 }
101 120
......