blob: e05a675c84198f6b1bb39bac99b1ae97298603d3 [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)
49 private boolean enabled = false;
50
51 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
52 required = false, multiValued = false)
53 private boolean disabled = false;
54
tomc290a122014-09-08 14:27:13 -070055 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070056 required = false, multiValued = false)
Ray Milkeyc1e69e82018-10-09 15:59:22 -070057 @Completion(DeviceIdCompleter.class)
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070058 protected String uri = null;
tomff7eb7c2014-09-08 12:49:03 -070059
tom6d2a43e2014-09-08 01:50:20 -070060 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 protected void doExecute() {
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070062 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070063 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070064 if (outputJson()) {
65 print("%s", jsonPorts(service, getSortedDevices(service)));
66 } else {
67 for (Device device : getSortedDevices(service)) {
68 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070069 printPorts(service, device);
tom32085cf2014-10-16 00:04:33 -070070 }
tomff7eb7c2014-09-08 12:49:03 -070071 }
tom32085cf2014-10-16 00:04:33 -070072
tomff7eb7c2014-09-08 12:49:03 -070073 } else {
tom9eb57fb2014-09-11 19:42:38 -070074 Device device = service.getDevice(deviceId(uri));
75 if (device == null) {
76 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070077 } else if (outputJson()) {
78 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070079 } else {
80 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070081 printPorts(service, device);
tom9eb57fb2014-09-11 19:42:38 -070082 }
tom6d2a43e2014-09-08 01:50:20 -070083 }
tom6d2a43e2014-09-08 01:50:20 -070084 }
tomff7eb7c2014-09-08 12:49:03 -070085
tom32085cf2014-10-16 00:04:33 -070086 /**
87 * Produces JSON array containing ports of the specified devices.
88 *
89 * @param service device service
90 * @param devices collection of devices
91 * @return JSON array
92 */
tom8350ba52014-10-16 07:22:02 -070093 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070094 ObjectMapper mapper = new ObjectMapper();
95 ArrayNode result = mapper.createArrayNode();
96 for (Device device : devices) {
97 result.add(jsonPorts(service, mapper, device));
98 }
99 return result;
100 }
101
102 /**
103 * Produces JSON array containing ports of the specified device.
104 *
105 * @param service device service
106 * @param mapper object mapper
107 * @param device infrastructure devices
108 * @return JSON array
109 */
tom8350ba52014-10-16 07:22:02 -0700110 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700111 ObjectNode result = mapper.createObjectNode();
112 ArrayNode ports = mapper.createArrayNode();
113 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700114 if (isIncluded(port)) {
115 ports.add(mapper.createObjectNode()
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700116 .put("port", port.number().toString())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700117 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700118 .put("type", port.type().toString().toLowerCase())
119 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700120 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700121 }
tom32085cf2014-10-16 00:04:33 -0700122 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700123 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700124 result.set("ports", ports);
125 return result;
tom32085cf2014-10-16 00:04:33 -0700126 }
127
tom8350ba52014-10-16 07:22:02 -0700128 // Determines if a port should be included in output.
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700129 protected boolean isIncluded(Port port) {
tom8350ba52014-10-16 07:22:02 -0700130 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
131 !enabled && !disabled;
132 }
133
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700134 protected void printPorts(DeviceService service, Device device) {
tomff7eb7c2014-09-08 12:49:03 -0700135 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700136 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700137 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300138 if (!isIncluded(port)) {
139 continue;
140 }
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700141 String portName = port.number().toString();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300142 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
143 String portType = port.type().toString().toLowerCase();
144 String annotations = annotations(port.annotations());
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700145 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
tomff7eb7c2014-09-08 12:49:03 -0700146 }
147 }
tom6d2a43e2014-09-08 01:50:20 -0700148}