blob: 312be78258c1a427d65715ce0a79f425a0186754 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
tom6d2a43e2014-09-08 01:50:20 -070022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
tom8350ba52014-10-16 07:22:02 -070024import org.apache.karaf.shell.commands.Option;
Ray Milkeyc7477292016-03-11 10:53:43 -080025import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.Device;
27import org.onosproject.net.Port;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.device.DeviceService;
tomff7eb7c2014-09-08 12:49:03 -070029import java.util.ArrayList;
30import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070031import java.util.List;
32
Brian O'Connorabafb502014-12-02 22:26:20 -080033import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070034
35/**
tomc290a122014-09-08 14:27:13 -070036 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070037 */
38@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070039 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070040public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070041
Yafit Hadara9a73de2015-09-06 13:52:52 +030042 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
tom6d2a43e2014-09-08 01:50:20 -070043
tom8350ba52014-10-16 07:22:02 -070044 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
45 required = false, multiValued = false)
46 private boolean enabled = false;
47
48 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
49 required = false, multiValued = false)
50 private boolean disabled = false;
51
tomc290a122014-09-08 14:27:13 -070052 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070053 required = false, multiValued = false)
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070054 protected String uri = null;
tomff7eb7c2014-09-08 12:49:03 -070055
tom6d2a43e2014-09-08 01:50:20 -070056 @Override
tom0872a172014-09-23 11:24:26 -070057 protected void execute() {
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070058 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070059 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070060 if (outputJson()) {
61 print("%s", jsonPorts(service, getSortedDevices(service)));
62 } else {
63 for (Device device : getSortedDevices(service)) {
64 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070065 printPorts(service, device);
tom32085cf2014-10-16 00:04:33 -070066 }
tomff7eb7c2014-09-08 12:49:03 -070067 }
tom32085cf2014-10-16 00:04:33 -070068
tomff7eb7c2014-09-08 12:49:03 -070069 } else {
tom9eb57fb2014-09-11 19:42:38 -070070 Device device = service.getDevice(deviceId(uri));
71 if (device == null) {
72 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070073 } else if (outputJson()) {
74 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070075 } else {
76 printDevice(service, device);
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070077 printPorts(service, device);
tom9eb57fb2014-09-11 19:42:38 -070078 }
tom6d2a43e2014-09-08 01:50:20 -070079 }
tom6d2a43e2014-09-08 01:50:20 -070080 }
tomff7eb7c2014-09-08 12:49:03 -070081
tom32085cf2014-10-16 00:04:33 -070082 /**
83 * Produces JSON array containing ports of the specified devices.
84 *
85 * @param service device service
86 * @param devices collection of devices
87 * @return JSON array
88 */
tom8350ba52014-10-16 07:22:02 -070089 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070090 ObjectMapper mapper = new ObjectMapper();
91 ArrayNode result = mapper.createArrayNode();
92 for (Device device : devices) {
93 result.add(jsonPorts(service, mapper, device));
94 }
95 return result;
96 }
97
98 /**
99 * Produces JSON array containing ports of the specified device.
100 *
101 * @param service device service
102 * @param mapper object mapper
103 * @param device infrastructure devices
104 * @return JSON array
105 */
tom8350ba52014-10-16 07:22:02 -0700106 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700107 ObjectNode result = mapper.createObjectNode();
108 ArrayNode ports = mapper.createArrayNode();
109 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700110 if (isIncluded(port)) {
111 ports.add(mapper.createObjectNode()
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700112 .put("port", port.number().toString())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700113 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700114 .put("type", port.type().toString().toLowerCase())
115 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700116 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700117 }
tom32085cf2014-10-16 00:04:33 -0700118 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700119 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700120 result.set("ports", ports);
121 return result;
tom32085cf2014-10-16 00:04:33 -0700122 }
123
tom8350ba52014-10-16 07:22:02 -0700124 // Determines if a port should be included in output.
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700125 protected boolean isIncluded(Port port) {
tom8350ba52014-10-16 07:22:02 -0700126 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
127 !enabled && !disabled;
128 }
129
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700130 protected void printPorts(DeviceService service, Device device) {
tomff7eb7c2014-09-08 12:49:03 -0700131 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700132 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700133 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300134 if (!isIncluded(port)) {
135 continue;
136 }
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700137 String portName = port.number().toString();
Yafit Hadara9a73de2015-09-06 13:52:52 +0300138 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
139 String portType = port.type().toString().toLowerCase();
140 String annotations = annotations(port.annotations());
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -0700141 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
tomff7eb7c2014-09-08 12:49:03 -0700142 }
143 }
tom6d2a43e2014-09-08 01:50:20 -0700144}