Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 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; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 25 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.Port; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 31 | import java.util.List; |
| 32 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 36 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 37 | */ |
| 38 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 39 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 40 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 42 | 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] | 43 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 44 | @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports", |
| 45 | required = false, multiValued = false) |
| 46 | private boolean enabled = false; |
| 47 | |
| 48 | @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports", |
| 49 | required = false, multiValued = false) |
| 50 | private boolean disabled = false; |
| 51 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 52 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 53 | required = false, multiValued = false) |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 54 | protected String uri = null; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 55 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 56 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 57 | protected void execute() { |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 58 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 59 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 60 | if (outputJson()) { |
| 61 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 62 | } else { |
| 63 | for (Device device : getSortedDevices(service)) { |
| 64 | printDevice(service, device); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 65 | printPorts(service, device); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 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); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 77 | printPorts(service, device); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 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() |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 112 | .put("port", port.number().toString()) |
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 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 119 | result.set("device", jsonForEntity(device, Device.class)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 120 | result.set("ports", ports); |
| 121 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 122 | } |
| 123 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 124 | // Determines if a port should be included in output. |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 125 | protected boolean isIncluded(Port port) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 126 | return enabled && port.isEnabled() || disabled && !port.isEnabled() || |
| 127 | !enabled && !disabled; |
| 128 | } |
| 129 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 130 | protected void printPorts(DeviceService service, Device device) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 131 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 132 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 133 | for (Port port : ports) { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 134 | if (!isIncluded(port)) { |
| 135 | continue; |
| 136 | } |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 137 | String portName = port.number().toString(); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 138 | Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled"; |
| 139 | String portType = port.type().toString().toLowerCase(); |
| 140 | String annotations = annotations(port.annotations()); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 141 | print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 144 | } |