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