Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 25 | import org.onlab.onos.cli.Comparators; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.Device; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.Port; |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 28 | import org.onlab.onos.net.PortNumber; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.device.DeviceService; |
| 30 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 33 | import java.util.List; |
| 34 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 35 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 36 | |
| 37 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 38 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 39 | */ |
| 40 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 41 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 42 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 43 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 44 | private static final String FMT = " port=%s, state=%s, type=%s, speed=%s%s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 45 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 46 | @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports", |
| 47 | required = false, multiValued = false) |
| 48 | private boolean enabled = false; |
| 49 | |
| 50 | @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports", |
| 51 | required = false, multiValued = false) |
| 52 | private boolean disabled = false; |
| 53 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 54 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 55 | required = false, multiValued = false) |
| 56 | String uri = null; |
| 57 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 58 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 59 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 60 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 61 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 62 | if (outputJson()) { |
| 63 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 64 | } else { |
| 65 | for (Device device : getSortedDevices(service)) { |
| 66 | printDevice(service, device); |
| 67 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 68 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 69 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 70 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 71 | Device device = service.getDevice(deviceId(uri)); |
| 72 | if (device == null) { |
| 73 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 74 | } else if (outputJson()) { |
| 75 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 76 | } else { |
| 77 | printDevice(service, device); |
| 78 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 79 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 80 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 81 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Produces JSON array containing ports of the specified devices. |
| 84 | * |
| 85 | * @param service device service |
| 86 | * @param devices collection of devices |
| 87 | * @return JSON array |
| 88 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 89 | public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 90 | ObjectMapper mapper = new ObjectMapper(); |
| 91 | ArrayNode result = mapper.createArrayNode(); |
| 92 | for (Device device : devices) { |
| 93 | result.add(jsonPorts(service, mapper, device)); |
| 94 | } |
| 95 | return result; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Produces JSON array containing ports of the specified device. |
| 100 | * |
| 101 | * @param service device service |
| 102 | * @param mapper object mapper |
| 103 | * @param device infrastructure devices |
| 104 | * @return JSON array |
| 105 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 106 | public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 107 | ObjectNode result = mapper.createObjectNode(); |
| 108 | ArrayNode ports = mapper.createArrayNode(); |
| 109 | for (Port port : service.getPorts(device.id())) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 110 | if (isIncluded(port)) { |
| 111 | ports.add(mapper.createObjectNode() |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 112 | .put("port", portName(port.number())) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 113 | .put("isEnabled", port.isEnabled()) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 114 | .put("type", port.type().toString().toLowerCase()) |
| 115 | .put("portSpeed", port.portSpeed()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 116 | .set("annotations", annotations(mapper, port.annotations()))); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 117 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 118 | } |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 119 | result.set("device", json(service, mapper, device)); |
| 120 | result.set("ports", ports); |
| 121 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 124 | private String portName(PortNumber port) { |
| 125 | return port.equals(PortNumber.LOCAL) ? "local" : port.toString(); |
| 126 | } |
| 127 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 128 | // Determines if a port should be included in output. |
| 129 | private boolean isIncluded(Port port) { |
| 130 | return enabled && port.isEnabled() || disabled && !port.isEnabled() || |
| 131 | !enabled && !disabled; |
| 132 | } |
| 133 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 134 | @Override |
| 135 | protected void printDevice(DeviceService service, Device device) { |
| 136 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 137 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 138 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 139 | for (Port port : ports) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 140 | if (isIncluded(port)) { |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 141 | print(FMT, portName(port.number()), |
| 142 | port.isEnabled() ? "enabled" : "disabled", |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 143 | port.type().toString().toLowerCase(), port.portSpeed(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 144 | annotations(port.annotations())); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 145 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 149 | } |