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; |
| 28 | import org.onlab.onos.net.device.DeviceService; |
| 29 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 34 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 35 | |
| 36 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 37 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 38 | */ |
| 39 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 40 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 41 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 42 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 43 | 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] | 44 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 45 | @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports", |
| 46 | required = false, multiValued = false) |
| 47 | private boolean enabled = false; |
| 48 | |
| 49 | @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports", |
| 50 | required = false, multiValued = false) |
| 51 | private boolean disabled = false; |
| 52 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 53 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 54 | required = false, multiValued = false) |
| 55 | String uri = null; |
| 56 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 57 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 58 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 59 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 60 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 61 | if (outputJson()) { |
| 62 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 63 | } else { |
| 64 | for (Device device : getSortedDevices(service)) { |
| 65 | printDevice(service, device); |
| 66 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 67 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 68 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 69 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 70 | Device device = service.getDevice(deviceId(uri)); |
| 71 | if (device == null) { |
| 72 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 73 | } else if (outputJson()) { |
| 74 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 75 | } else { |
| 76 | printDevice(service, device); |
| 77 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 78 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 79 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 80 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Produces JSON array containing ports of the specified devices. |
| 83 | * |
| 84 | * @param service device service |
| 85 | * @param devices collection of devices |
| 86 | * @return JSON array |
| 87 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 88 | public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 89 | ObjectMapper mapper = new ObjectMapper(); |
| 90 | ArrayNode result = mapper.createArrayNode(); |
| 91 | for (Device device : devices) { |
| 92 | result.add(jsonPorts(service, mapper, device)); |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Produces JSON array containing ports of the specified device. |
| 99 | * |
| 100 | * @param service device service |
| 101 | * @param mapper object mapper |
| 102 | * @param device infrastructure devices |
| 103 | * @return JSON array |
| 104 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 105 | public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 106 | ObjectNode result = mapper.createObjectNode(); |
| 107 | ArrayNode ports = mapper.createArrayNode(); |
| 108 | for (Port port : service.getPorts(device.id())) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 109 | if (isIncluded(port)) { |
| 110 | ports.add(mapper.createObjectNode() |
| 111 | .put("port", port.number().toString()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 112 | .put("isEnabled", port.isEnabled()) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 113 | .put("type", port.type().toString().toLowerCase()) |
| 114 | .put("portSpeed", port.portSpeed()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 115 | .set("annotations", annotations(mapper, port.annotations()))); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 116 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 117 | } |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 118 | result.set("device", json(service, mapper, device)); |
| 119 | result.set("ports", ports); |
| 120 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 121 | } |
| 122 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 123 | // Determines if a port should be included in output. |
| 124 | private boolean isIncluded(Port port) { |
| 125 | return enabled && port.isEnabled() || disabled && !port.isEnabled() || |
| 126 | !enabled && !disabled; |
| 127 | } |
| 128 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 129 | @Override |
| 130 | protected void printDevice(DeviceService service, Device device) { |
| 131 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 132 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 133 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 134 | for (Port port : ports) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 135 | if (isIncluded(port)) { |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 136 | print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled", |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 137 | port.type().toString().toLowerCase(), port.portSpeed(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 138 | annotations(port.annotations())); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 139 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 143 | } |