blob: f2c280ef148877e3e2e7b684dcb7aa0b2b90f569 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom6d2a43e2014-09-08 01:50:20 -070019package org.onlab.onos.cli.net;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
tom6d2a43e2014-09-08 01:50:20 -070025import org.apache.karaf.shell.commands.Argument;
26import org.apache.karaf.shell.commands.Command;
tom8350ba52014-10-16 07:22:02 -070027import org.apache.karaf.shell.commands.Option;
tom91c7bd02014-09-25 22:50:44 -070028import org.onlab.onos.cli.Comparators;
tomff7eb7c2014-09-08 12:49:03 -070029import org.onlab.onos.net.Device;
tom6d2a43e2014-09-08 01:50:20 -070030import org.onlab.onos.net.Port;
31import org.onlab.onos.net.device.DeviceService;
32
tomff7eb7c2014-09-08 12:49:03 -070033import java.util.ArrayList;
34import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070035import java.util.List;
36
tom6d2a43e2014-09-08 01:50:20 -070037import static org.onlab.onos.net.DeviceId.deviceId;
38
39/**
tomc290a122014-09-08 14:27:13 -070040 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070041 */
42@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070043 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070044public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070045
tomff7eb7c2014-09-08 12:49:03 -070046 private static final String FMT = " port=%s, state=%s";
tom6d2a43e2014-09-08 01:50:20 -070047
tom8350ba52014-10-16 07:22:02 -070048 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
49 required = false, multiValued = false)
50 private boolean enabled = false;
51
52 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
53 required = false, multiValued = false)
54 private boolean disabled = false;
55
tomc290a122014-09-08 14:27:13 -070056 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070057 required = false, multiValued = false)
58 String uri = null;
59
tom6d2a43e2014-09-08 01:50:20 -070060 @Override
tom0872a172014-09-23 11:24:26 -070061 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -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);
69 }
tomff7eb7c2014-09-08 12:49:03 -070070 }
tom32085cf2014-10-16 00:04:33 -070071
tomff7eb7c2014-09-08 12:49:03 -070072 } else {
tom9eb57fb2014-09-11 19:42:38 -070073 Device device = service.getDevice(deviceId(uri));
74 if (device == null) {
75 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070076 } else if (outputJson()) {
77 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070078 } else {
79 printDevice(service, device);
80 }
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()
114 .put("port", port.number().toString())
115 .put("isEnabled", port.isEnabled()));
116 }
tom32085cf2014-10-16 00:04:33 -0700117 }
118 return result.put("device", device.id().toString()).set("ports", ports);
119 }
120
tom8350ba52014-10-16 07:22:02 -0700121 // Determines if a port should be included in output.
122 private boolean isIncluded(Port port) {
123 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
124 !enabled && !disabled;
125 }
126
tomc290a122014-09-08 14:27:13 -0700127 @Override
128 protected void printDevice(DeviceService service, Device device) {
129 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -0700130 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700131 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700132 for (Port port : ports) {
tom8350ba52014-10-16 07:22:02 -0700133 if (isIncluded(port)) {
134 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
135 }
tomff7eb7c2014-09-08 12:49:03 -0700136 }
137 }
138
tom6d2a43e2014-09-08 01:50:20 -0700139}