blob: f3d63bdfeac9cdf4ea08b8334e818040766bfc2d [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom6d2a43e2014-09-08 01:50:20 -070017
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
Ray Milkeyc1e69e82018-10-09 15:59:22 -070024import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
26import org.apache.karaf.shell.api.action.Option;
Ray Milkeyc7477292016-03-11 10:53:43 -080027import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Device;
29import org.onosproject.net.Port;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.device.DeviceService;
tomff7eb7c2014-09-08 12:49:03 -070031import java.util.ArrayList;
32import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070033import java.util.List;
34
Brian O'Connorabafb502014-12-02 22:26:20 -080035import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070036
37/**
tomc290a122014-09-08 14:27:13 -070038 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070039 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
tom6d2a43e2014-09-08 01:50:20 -070041@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070042 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070043public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070044
Yafit Hadara9a73de2015-09-06 13:52:52 +030045 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
tom6d2a43e2014-09-08 01:50:20 -070046
tom8350ba52014-10-16 07:22:02 -070047 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
48 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070049 @Completion(DeviceIdCompleter.class)
tom8350ba52014-10-16 07:22:02 -070050 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
tomc290a122014-09-08 14:27:13 -070056 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070057 required = false, multiValued = false)
Ray Milkeyc1e69e82018-10-09 15:59:22 -070058 @Completion(DeviceIdCompleter.class)
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070059 protected String uri = null;
tomff7eb7c2014-09-08 12:49:03 -070060
tom6d2a43e2014-09-08 01:50:20 -070061 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 protected void doExecute() {
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070063 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070064 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070065 if (outputJson()) {
66 print("%s", jsonPorts(service, getSortedDevices(service)));
67 } else {
68 for (Device device : getSortedDevices(service)) {
69 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070070 printPorts(service, device);
tom32085cf2014-10-16 00:04:33 -070071 }
tomff7eb7c2014-09-08 12:49:03 -070072 }
tom32085cf2014-10-16 00:04:33 -070073
tomff7eb7c2014-09-08 12:49:03 -070074 } else {
tom9eb57fb2014-09-11 19:42:38 -070075 Device device = service.getDevice(deviceId(uri));
76 if (device == null) {
77 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070078 } else if (outputJson()) {
79 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070080 } else {
81 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070082 printPorts(service, device);
tom9eb57fb2014-09-11 19:42:38 -070083 }
tom6d2a43e2014-09-08 01:50:20 -070084 }
tom6d2a43e2014-09-08 01:50:20 -070085 }
tomff7eb7c2014-09-08 12:49:03 -070086
tom32085cf2014-10-16 00:04:33 -070087 /**
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 */
tom8350ba52014-10-16 07:22:02 -070094 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070095 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 */
tom8350ba52014-10-16 07:22:02 -0700111 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700112 ObjectNode result = mapper.createObjectNode();
113 ArrayNode ports = mapper.createArrayNode();
114 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700115 if (isIncluded(port)) {
116 ports.add(mapper.createObjectNode()
Seyeon Jeong8d3cad22020-02-28 01:17:34 -0800117 .put("element", device.id().toString())
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700118 .put("port", port.number().toString())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700119 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700120 .put("type", port.type().toString().toLowerCase())
121 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700122 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700123 }
tom32085cf2014-10-16 00:04:33 -0700124 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700125 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700126 result.set("ports", ports);
127 return result;
tom32085cf2014-10-16 00:04:33 -0700128 }
129
tom8350ba52014-10-16 07:22:02 -0700130 // Determines if a port should be included in output.
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700131 protected boolean isIncluded(Port port) {
tom8350ba52014-10-16 07:22:02 -0700132 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
133 !enabled && !disabled;
134 }
135
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700136 protected void printPorts(DeviceService service, Device device) {
tomff7eb7c2014-09-08 12:49:03 -0700137 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700138 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700139 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300140 if (!isIncluded(port)) {
141 continue;
142 }
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700143 String portName = port.number().toString();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300144 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
145 String portType = port.type().toString().toLowerCase();
146 String annotations = annotations(port.annotations());
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700147 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
tomff7eb7c2014-09-08 12:49:03 -0700148 }
149 }
tom6d2a43e2014-09-08 01:50:20 -0700150}