blob: 1d32aa57cd2c0fe01e241150c4ca3cb59bbe3f9a [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
tom6d2a43e2014-09-08 01:50:20 -070016package org.onlab.onos.cli.net;
17
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;
tom91c7bd02014-09-25 22:50:44 -070025import org.onlab.onos.cli.Comparators;
tomff7eb7c2014-09-08 12:49:03 -070026import org.onlab.onos.net.Device;
tom6d2a43e2014-09-08 01:50:20 -070027import org.onlab.onos.net.Port;
28import org.onlab.onos.net.device.DeviceService;
29
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
tom6d2a43e2014-09-08 01:50:20 -070034import static org.onlab.onos.net.DeviceId.deviceId;
35
36/**
tomc290a122014-09-08 14:27:13 -070037 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070038 */
39@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070040 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070041public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070042
Thomas Vachuskad16ce182014-10-29 17:25:29 -070043 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s%s";
tom6d2a43e2014-09-08 01:50:20 -070044
tom8350ba52014-10-16 07:22:02 -070045 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
46 required = false, multiValued = false)
47 private boolean enabled = false;
48
49 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
50 required = false, multiValued = false)
51 private boolean disabled = false;
52
tomc290a122014-09-08 14:27:13 -070053 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070054 required = false, multiValued = false)
55 String uri = null;
56
tom6d2a43e2014-09-08 01:50:20 -070057 @Override
tom0872a172014-09-23 11:24:26 -070058 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070059 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070060 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070061 if (outputJson()) {
62 print("%s", jsonPorts(service, getSortedDevices(service)));
63 } else {
64 for (Device device : getSortedDevices(service)) {
65 printDevice(service, device);
66 }
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);
77 }
tom6d2a43e2014-09-08 01:50:20 -070078 }
tom6d2a43e2014-09-08 01:50:20 -070079 }
tomff7eb7c2014-09-08 12:49:03 -070080
tom32085cf2014-10-16 00:04:33 -070081 /**
82 * Produces JSON array containing ports of the specified devices.
83 *
84 * @param service device service
85 * @param devices collection of devices
86 * @return JSON array
87 */
tom8350ba52014-10-16 07:22:02 -070088 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070089 ObjectMapper mapper = new ObjectMapper();
90 ArrayNode result = mapper.createArrayNode();
91 for (Device device : devices) {
92 result.add(jsonPorts(service, mapper, device));
93 }
94 return result;
95 }
96
97 /**
98 * Produces JSON array containing ports of the specified device.
99 *
100 * @param service device service
101 * @param mapper object mapper
102 * @param device infrastructure devices
103 * @return JSON array
104 */
tom8350ba52014-10-16 07:22:02 -0700105 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700106 ObjectNode result = mapper.createObjectNode();
107 ArrayNode ports = mapper.createArrayNode();
108 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700109 if (isIncluded(port)) {
110 ports.add(mapper.createObjectNode()
111 .put("port", port.number().toString())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700112 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700113 .put("type", port.type().toString().toLowerCase())
114 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700115 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700116 }
tom32085cf2014-10-16 00:04:33 -0700117 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700118 result.set("device", json(service, mapper, device));
119 result.set("ports", ports);
120 return result;
tom32085cf2014-10-16 00:04:33 -0700121 }
122
tom8350ba52014-10-16 07:22:02 -0700123 // Determines if a port should be included in output.
124 private boolean isIncluded(Port port) {
125 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
126 !enabled && !disabled;
127 }
128
tomc290a122014-09-08 14:27:13 -0700129 @Override
130 protected void printDevice(DeviceService service, Device device) {
131 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -0700132 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700133 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700134 for (Port port : ports) {
tom8350ba52014-10-16 07:22:02 -0700135 if (isIncluded(port)) {
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700136 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled",
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700137 port.type().toString().toLowerCase(), port.portSpeed(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700138 annotations(port.annotations()));
tom8350ba52014-10-16 07:22:02 -0700139 }
tomff7eb7c2014-09-08 12:49:03 -0700140 }
141 }
142
tom6d2a43e2014-09-08 01:50:20 -0700143}