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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Argument; |
| 23 | import org.apache.karaf.shell.api.action.Command; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 26 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.Port; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.device.DeviceService; |
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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 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 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Service |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 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 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [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) |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 56 | protected String uri = null; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 57 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 58 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | protected void doExecute() { |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -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); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 67 | printPorts(service, device); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 68 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 69 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 70 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 71 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 72 | Device device = service.getDevice(deviceId(uri)); |
| 73 | if (device == null) { |
| 74 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 75 | } else if (outputJson()) { |
| 76 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 77 | } else { |
| 78 | printDevice(service, device); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 79 | printPorts(service, device); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 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() |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 114 | .put("port", port.number().toString()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 115 | .put("isEnabled", port.isEnabled()) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 116 | .put("type", port.type().toString().toLowerCase()) |
| 117 | .put("portSpeed", port.portSpeed()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 118 | .set("annotations", annotations(mapper, port.annotations()))); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 119 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 120 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 121 | result.set("device", jsonForEntity(device, Device.class)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 122 | result.set("ports", ports); |
| 123 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 124 | } |
| 125 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 126 | // Determines if a port should be included in output. |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 127 | protected boolean isIncluded(Port port) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 128 | return enabled && port.isEnabled() || disabled && !port.isEnabled() || |
| 129 | !enabled && !disabled; |
| 130 | } |
| 131 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 132 | protected void printPorts(DeviceService service, Device device) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 133 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 134 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 135 | for (Port port : ports) { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 136 | if (!isIncluded(port)) { |
| 137 | continue; |
| 138 | } |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 139 | String portName = port.number().toString(); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 140 | Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled"; |
| 141 | String portType = port.type().toString().toLowerCase(); |
| 142 | String annotations = annotations(port.annotations()); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 143 | print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 146 | } |