tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 7 | import org.apache.karaf.shell.commands.Argument; |
| 8 | import org.apache.karaf.shell.commands.Command; |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 9 | import org.apache.karaf.shell.commands.Option; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 10 | import org.onlab.onos.cli.Comparators; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.Device; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.Port; |
| 13 | import org.onlab.onos.net.device.DeviceService; |
| 14 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 15 | import java.util.ArrayList; |
| 16 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 17 | import java.util.List; |
| 18 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 19 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 20 | |
| 21 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 22 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 23 | */ |
| 24 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 25 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 26 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 27 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 28 | private static final String FMT = " port=%s, state=%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 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 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 38 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 39 | required = false, multiValued = false) |
| 40 | String uri = null; |
| 41 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 42 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 43 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 44 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 45 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 46 | if (outputJson()) { |
| 47 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 48 | } else { |
| 49 | for (Device device : getSortedDevices(service)) { |
| 50 | printDevice(service, device); |
| 51 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 52 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 53 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 54 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 55 | Device device = service.getDevice(deviceId(uri)); |
| 56 | if (device == null) { |
| 57 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 58 | } else if (outputJson()) { |
| 59 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 60 | } else { |
| 61 | printDevice(service, device); |
| 62 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 63 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 64 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 65 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 66 | /** |
| 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 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 73 | public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 74 | 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 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 90 | public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 91 | ObjectNode result = mapper.createObjectNode(); |
| 92 | ArrayNode ports = mapper.createArrayNode(); |
| 93 | for (Port port : service.getPorts(device.id())) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 94 | if (isIncluded(port)) { |
| 95 | ports.add(mapper.createObjectNode() |
| 96 | .put("port", port.number().toString()) |
| 97 | .put("isEnabled", port.isEnabled())); |
| 98 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 99 | } |
| 100 | return result.put("device", device.id().toString()).set("ports", ports); |
| 101 | } |
| 102 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 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 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 109 | @Override |
| 110 | protected void printDevice(DeviceService service, Device device) { |
| 111 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 112 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 113 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 114 | for (Port port : ports) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 115 | if (isIncluded(port)) { |
| 116 | print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); |
| 117 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 121 | } |