blob: 29452f7676e6a8b5d7deedadedee1c6224825ca0 [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;
Thomas Vachuskacaa14d62014-11-05 16:28:33 -080028import org.onlab.onos.net.PortNumber;
tom6d2a43e2014-09-08 01:50:20 -070029import org.onlab.onos.net.device.DeviceService;
30
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
tom6d2a43e2014-09-08 01:50:20 -070035import static org.onlab.onos.net.DeviceId.deviceId;
36
37/**
tomc290a122014-09-08 14:27:13 -070038 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070039 */
40@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
Thomas Vachuskad16ce182014-10-29 17:25:29 -070044 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)
56 String uri = null;
57
tom6d2a43e2014-09-08 01:50:20 -070058 @Override
tom0872a172014-09-23 11:24:26 -070059 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -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);
67 }
tomff7eb7c2014-09-08 12:49:03 -070068 }
tom32085cf2014-10-16 00:04:33 -070069
tomff7eb7c2014-09-08 12:49:03 -070070 } else {
tom9eb57fb2014-09-11 19:42:38 -070071 Device device = service.getDevice(deviceId(uri));
72 if (device == null) {
73 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070074 } else if (outputJson()) {
75 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070076 } else {
77 printDevice(service, device);
78 }
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()
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800112 .put("port", portName(port.number()))
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 }
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700119 result.set("device", json(service, mapper, device));
120 result.set("ports", ports);
121 return result;
tom32085cf2014-10-16 00:04:33 -0700122 }
123
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800124 private String portName(PortNumber port) {
125 return port.equals(PortNumber.LOCAL) ? "local" : port.toString();
126 }
127
tom8350ba52014-10-16 07:22:02 -0700128 // Determines if a port should be included in output.
129 private boolean isIncluded(Port port) {
130 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
131 !enabled && !disabled;
132 }
133
tomc290a122014-09-08 14:27:13 -0700134 @Override
135 protected void printDevice(DeviceService service, Device device) {
136 super.printDevice(service, 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) {
tom8350ba52014-10-16 07:22:02 -0700140 if (isIncluded(port)) {
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800141 print(FMT, portName(port.number()),
142 port.isEnabled() ? "enabled" : "disabled",
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700143 port.type().toString().toLowerCase(), port.portSpeed(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700144 annotations(port.annotations()));
tom8350ba52014-10-16 07:22:02 -0700145 }
tomff7eb7c2014-09-08 12:49:03 -0700146 }
147 }
148
tom6d2a43e2014-09-08 01:50:20 -0700149}