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; |
Ray Milkey | c1e69e8 | 2018-10-09 15:59:22 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 26 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 27 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.Port; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.device.DeviceService; |
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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 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 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | @Service |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 42 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 43 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 44 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 45 | 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] | 46 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 47 | @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports", |
| 48 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 49 | @Completion(DeviceIdCompleter.class) |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 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) |
Ray Milkey | c1e69e8 | 2018-10-09 15:59:22 -0700 | [diff] [blame] | 58 | @Completion(DeviceIdCompleter.class) |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 59 | protected String uri = null; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 60 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 61 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | protected void doExecute() { |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 63 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 64 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 65 | if (outputJson()) { |
| 66 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 67 | } else { |
| 68 | for (Device device : getSortedDevices(service)) { |
| 69 | printDevice(service, device); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 70 | printPorts(service, device); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 71 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 72 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 73 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 74 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 75 | Device device = service.getDevice(deviceId(uri)); |
| 76 | if (device == null) { |
| 77 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 78 | } else if (outputJson()) { |
| 79 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 80 | } else { |
| 81 | printDevice(service, device); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 82 | printPorts(service, device); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 83 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 84 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 85 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 86 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Produces JSON array containing ports of the specified devices. |
| 89 | * |
| 90 | * @param service device service |
| 91 | * @param devices collection of devices |
| 92 | * @return JSON array |
| 93 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 94 | public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 95 | ObjectMapper mapper = new ObjectMapper(); |
| 96 | ArrayNode result = mapper.createArrayNode(); |
| 97 | for (Device device : devices) { |
| 98 | result.add(jsonPorts(service, mapper, device)); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Produces JSON array containing ports of the specified device. |
| 105 | * |
| 106 | * @param service device service |
| 107 | * @param mapper object mapper |
| 108 | * @param device infrastructure devices |
| 109 | * @return JSON array |
| 110 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 111 | public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 112 | ObjectNode result = mapper.createObjectNode(); |
| 113 | ArrayNode ports = mapper.createArrayNode(); |
| 114 | for (Port port : service.getPorts(device.id())) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 115 | if (isIncluded(port)) { |
| 116 | ports.add(mapper.createObjectNode() |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 117 | .put("port", port.number().toString()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 118 | .put("isEnabled", port.isEnabled()) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 119 | .put("type", port.type().toString().toLowerCase()) |
| 120 | .put("portSpeed", port.portSpeed()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 121 | .set("annotations", annotations(mapper, port.annotations()))); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 122 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 123 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 124 | result.set("device", jsonForEntity(device, Device.class)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 125 | result.set("ports", ports); |
| 126 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 127 | } |
| 128 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 129 | // Determines if a port should be included in output. |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 130 | protected boolean isIncluded(Port port) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 131 | return enabled && port.isEnabled() || disabled && !port.isEnabled() || |
| 132 | !enabled && !disabled; |
| 133 | } |
| 134 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 135 | protected void printPorts(DeviceService service, Device device) { |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 136 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 137 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 138 | for (Port port : ports) { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 139 | if (!isIncluded(port)) { |
| 140 | continue; |
| 141 | } |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 142 | String portName = port.number().toString(); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 143 | Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled"; |
| 144 | String portType = port.type().toString().toLowerCase(); |
| 145 | String annotations = annotations(port.annotations()); |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 146 | print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 149 | } |