blob: e2d3e6d68b79958f7a155c6fb69e280609aac977 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Yafit Hadara9a73de2015-09-06 13:52:52 +03002 * Copyright 2015 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;
Yafit Hadara9a73de2015-09-06 13:52:52 +030025import org.onlab.util.Frequency;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.Comparators;
27import org.onosproject.net.Device;
Yafit Hadara9a73de2015-09-06 13:52:52 +030028import org.onosproject.net.OchPort;
29import org.onosproject.net.OduCltPort;
30import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Port;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceService;
tom6d2a43e2014-09-08 01:50:20 -070034
tomff7eb7c2014-09-08 12:49:03 -070035import java.util.ArrayList;
36import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070037import java.util.List;
38
Brian O'Connorabafb502014-12-02 22:26:20 -080039import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070040
41/**
tomc290a122014-09-08 14:27:13 -070042 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070043 */
44@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070045 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070046public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070047
Yafit Hadara9a73de2015-09-06 13:52:52 +030048 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
49 private static final String FMT_OCH = " port=%s, state=%s, type=%s, signalType=%s, isTunable=%s %s";
50 private static final String FMT_ODUCLT = " port=%s, state=%s, type=%s, signalType=%s %s";
51 private static final String FMT_OMS = " port=%s, state=%s, type=%s, Freqs= %s / %s / %s GHz, totalChannels=%s %s";
tom6d2a43e2014-09-08 01:50:20 -070052
tom8350ba52014-10-16 07:22:02 -070053 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
54 required = false, multiValued = false)
55 private boolean enabled = false;
56
57 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
58 required = false, multiValued = false)
59 private boolean disabled = false;
60
tomc290a122014-09-08 14:27:13 -070061 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070062 required = false, multiValued = false)
63 String uri = null;
64
tom6d2a43e2014-09-08 01:50:20 -070065 @Override
tom0872a172014-09-23 11:24:26 -070066 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070067 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070068 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070069 if (outputJson()) {
70 print("%s", jsonPorts(service, getSortedDevices(service)));
71 } else {
72 for (Device device : getSortedDevices(service)) {
73 printDevice(service, device);
74 }
tomff7eb7c2014-09-08 12:49:03 -070075 }
tom32085cf2014-10-16 00:04:33 -070076
tomff7eb7c2014-09-08 12:49:03 -070077 } else {
tom9eb57fb2014-09-11 19:42:38 -070078 Device device = service.getDevice(deviceId(uri));
79 if (device == null) {
80 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070081 } else if (outputJson()) {
82 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070083 } else {
84 printDevice(service, device);
85 }
tom6d2a43e2014-09-08 01:50:20 -070086 }
tom6d2a43e2014-09-08 01:50:20 -070087 }
tomff7eb7c2014-09-08 12:49:03 -070088
tom32085cf2014-10-16 00:04:33 -070089 /**
90 * Produces JSON array containing ports of the specified devices.
91 *
92 * @param service device service
93 * @param devices collection of devices
94 * @return JSON array
95 */
tom8350ba52014-10-16 07:22:02 -070096 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070097 ObjectMapper mapper = new ObjectMapper();
98 ArrayNode result = mapper.createArrayNode();
99 for (Device device : devices) {
100 result.add(jsonPorts(service, mapper, device));
101 }
102 return result;
103 }
104
105 /**
106 * Produces JSON array containing ports of the specified device.
107 *
108 * @param service device service
109 * @param mapper object mapper
110 * @param device infrastructure devices
111 * @return JSON array
112 */
tom8350ba52014-10-16 07:22:02 -0700113 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700114 ObjectNode result = mapper.createObjectNode();
115 ArrayNode ports = mapper.createArrayNode();
116 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700117 if (isIncluded(port)) {
118 ports.add(mapper.createObjectNode()
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800119 .put("port", portName(port.number()))
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700120 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700121 .put("type", port.type().toString().toLowerCase())
122 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700123 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700124 }
tom32085cf2014-10-16 00:04:33 -0700125 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700126 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700127 result.set("ports", ports);
128 return result;
tom32085cf2014-10-16 00:04:33 -0700129 }
130
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800131 private String portName(PortNumber port) {
132 return port.equals(PortNumber.LOCAL) ? "local" : port.toString();
133 }
134
tom8350ba52014-10-16 07:22:02 -0700135 // Determines if a port should be included in output.
136 private boolean isIncluded(Port port) {
137 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
138 !enabled && !disabled;
139 }
140
tomc290a122014-09-08 14:27:13 -0700141 @Override
142 protected void printDevice(DeviceService service, Device device) {
143 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -0700144 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700145 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700146 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300147 if (!isIncluded(port)) {
148 continue;
149 }
150 String portName = portName(port.number());
151 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
152 String portType = port.type().toString().toLowerCase();
153 String annotations = annotations(port.annotations());
154 switch (port.type()) {
155 case OCH:
156 print(FMT_OCH, portName, portIsEnabled, portType,
157 ((OchPort) port).signalType().toString(),
158 ((OchPort) port).isTunable() ? "yes" : "no", annotations);
159 break;
160 case ODUCLT:
161 print(FMT_ODUCLT, portName, portIsEnabled, portType,
162 ((OduCltPort) port).signalType().toString(), annotations);
163 break;
164 case OMS:
165 print(FMT_OMS, portName, portIsEnabled, portType,
166 ((OmsPort) port).minFrequency().asHz() / Frequency.ofGHz(1).asHz(),
167 ((OmsPort) port).maxFrequency().asHz() / Frequency.ofGHz(1).asHz(),
168 ((OmsPort) port).grid().asHz() / Frequency.ofGHz(1).asHz(),
169 ((OmsPort) port).totalChannels(), annotations);
170 break;
171 default:
172 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
173 break;
tom8350ba52014-10-16 07:22:02 -0700174 }
tomff7eb7c2014-09-08 12:49:03 -0700175 }
176 }
tom6d2a43e2014-09-08 01:50:20 -0700177}