blob: b32146cfbc023399ba57af8bc07d382ca7271294 [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;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Ray Milkeyc7477292016-03-11 10:53:43 -080026import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Device;
28import org.onosproject.net.Port;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.device.DeviceService;
tomff7eb7c2014-09-08 12:49:03 -070030import java.util.ArrayList;
31import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070032import java.util.List;
33
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070035
36/**
tomc290a122014-09-08 14:27:13 -070037 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070038 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
tom6d2a43e2014-09-08 01:50:20 -070040@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070041 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070042public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070043
Yafit Hadara9a73de2015-09-06 13:52:52 +030044 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
tom6d2a43e2014-09-08 01:50:20 -070045
tom8350ba52014-10-16 07:22:02 -070046 @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
tomc290a122014-09-08 14:27:13 -070054 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070055 required = false, multiValued = false)
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070056 protected String uri = null;
tomff7eb7c2014-09-08 12:49:03 -070057
tom6d2a43e2014-09-08 01:50:20 -070058 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059 protected void doExecute() {
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070060 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070061 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070062 if (outputJson()) {
63 print("%s", jsonPorts(service, getSortedDevices(service)));
64 } else {
65 for (Device device : getSortedDevices(service)) {
66 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070067 printPorts(service, device);
tom32085cf2014-10-16 00:04:33 -070068 }
tomff7eb7c2014-09-08 12:49:03 -070069 }
tom32085cf2014-10-16 00:04:33 -070070
tomff7eb7c2014-09-08 12:49:03 -070071 } else {
tom9eb57fb2014-09-11 19:42:38 -070072 Device device = service.getDevice(deviceId(uri));
73 if (device == null) {
74 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070075 } else if (outputJson()) {
76 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070077 } else {
78 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070079 printPorts(service, device);
tom9eb57fb2014-09-11 19:42:38 -070080 }
tom6d2a43e2014-09-08 01:50:20 -070081 }
tom6d2a43e2014-09-08 01:50:20 -070082 }
tomff7eb7c2014-09-08 12:49:03 -070083
tom32085cf2014-10-16 00:04:33 -070084 /**
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 */
tom8350ba52014-10-16 07:22:02 -070091 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070092 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 */
tom8350ba52014-10-16 07:22:02 -0700108 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700109 ObjectNode result = mapper.createObjectNode();
110 ArrayNode ports = mapper.createArrayNode();
111 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700112 if (isIncluded(port)) {
113 ports.add(mapper.createObjectNode()
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700114 .put("port", port.number().toString())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700115 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700116 .put("type", port.type().toString().toLowerCase())
117 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700118 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700119 }
tom32085cf2014-10-16 00:04:33 -0700120 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700121 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700122 result.set("ports", ports);
123 return result;
tom32085cf2014-10-16 00:04:33 -0700124 }
125
tom8350ba52014-10-16 07:22:02 -0700126 // Determines if a port should be included in output.
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700127 protected boolean isIncluded(Port port) {
tom8350ba52014-10-16 07:22:02 -0700128 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
129 !enabled && !disabled;
130 }
131
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700132 protected void printPorts(DeviceService service, Device device) {
tomff7eb7c2014-09-08 12:49:03 -0700133 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700134 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700135 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300136 if (!isIncluded(port)) {
137 continue;
138 }
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700139 String portName = port.number().toString();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300140 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
141 String portType = port.type().toString().toLowerCase();
142 String annotations = annotations(port.annotations());
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700143 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
tomff7eb7c2014-09-08 12:49:03 -0700144 }
145 }
tom6d2a43e2014-09-08 01:50:20 -0700146}