blob: d1dc559025f93ed20714790c20a037d1c483c4c9 [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;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020031import org.onosproject.net.OtuPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.device.DeviceService;
tom6d2a43e2014-09-08 01:50:20 -070035
tomff7eb7c2014-09-08 12:49:03 -070036import java.util.ArrayList;
37import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070038import java.util.List;
39
Brian O'Connorabafb502014-12-02 22:26:20 -080040import static org.onosproject.net.DeviceId.deviceId;
tom6d2a43e2014-09-08 01:50:20 -070041
42/**
tomc290a122014-09-08 14:27:13 -070043 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070044 */
45@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070046 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070047public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070048
Yafit Hadara9a73de2015-09-06 13:52:52 +030049 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
50 private static final String FMT_OCH = " port=%s, state=%s, type=%s, signalType=%s, isTunable=%s %s";
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020051 private static final String FMT_ODUCLT_OTU = " port=%s, state=%s, type=%s, signalType=%s %s";
Yafit Hadara9a73de2015-09-06 13:52:52 +030052 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 -070053
tom8350ba52014-10-16 07:22:02 -070054 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
55 required = false, multiValued = false)
56 private boolean enabled = false;
57
58 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
59 required = false, multiValued = false)
60 private boolean disabled = false;
61
tomc290a122014-09-08 14:27:13 -070062 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070063 required = false, multiValued = false)
64 String uri = null;
65
tom6d2a43e2014-09-08 01:50:20 -070066 @Override
tom0872a172014-09-23 11:24:26 -070067 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070068 DeviceService service = get(DeviceService.class);
tomff7eb7c2014-09-08 12:49:03 -070069 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070070 if (outputJson()) {
71 print("%s", jsonPorts(service, getSortedDevices(service)));
72 } else {
73 for (Device device : getSortedDevices(service)) {
74 printDevice(service, device);
75 }
tomff7eb7c2014-09-08 12:49:03 -070076 }
tom32085cf2014-10-16 00:04:33 -070077
tomff7eb7c2014-09-08 12:49:03 -070078 } else {
tom9eb57fb2014-09-11 19:42:38 -070079 Device device = service.getDevice(deviceId(uri));
80 if (device == null) {
81 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070082 } else if (outputJson()) {
83 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070084 } else {
85 printDevice(service, device);
86 }
tom6d2a43e2014-09-08 01:50:20 -070087 }
tom6d2a43e2014-09-08 01:50:20 -070088 }
tomff7eb7c2014-09-08 12:49:03 -070089
tom32085cf2014-10-16 00:04:33 -070090 /**
91 * Produces JSON array containing ports of the specified devices.
92 *
93 * @param service device service
94 * @param devices collection of devices
95 * @return JSON array
96 */
tom8350ba52014-10-16 07:22:02 -070097 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -070098 ObjectMapper mapper = new ObjectMapper();
99 ArrayNode result = mapper.createArrayNode();
100 for (Device device : devices) {
101 result.add(jsonPorts(service, mapper, device));
102 }
103 return result;
104 }
105
106 /**
107 * Produces JSON array containing ports of the specified device.
108 *
109 * @param service device service
110 * @param mapper object mapper
111 * @param device infrastructure devices
112 * @return JSON array
113 */
tom8350ba52014-10-16 07:22:02 -0700114 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700115 ObjectNode result = mapper.createObjectNode();
116 ArrayNode ports = mapper.createArrayNode();
117 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700118 if (isIncluded(port)) {
119 ports.add(mapper.createObjectNode()
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800120 .put("port", portName(port.number()))
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700121 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700122 .put("type", port.type().toString().toLowerCase())
123 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700124 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700125 }
tom32085cf2014-10-16 00:04:33 -0700126 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700127 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700128 result.set("ports", ports);
129 return result;
tom32085cf2014-10-16 00:04:33 -0700130 }
131
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800132 private String portName(PortNumber port) {
133 return port.equals(PortNumber.LOCAL) ? "local" : port.toString();
134 }
135
tom8350ba52014-10-16 07:22:02 -0700136 // Determines if a port should be included in output.
137 private boolean isIncluded(Port port) {
138 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
139 !enabled && !disabled;
140 }
141
tomc290a122014-09-08 14:27:13 -0700142 @Override
143 protected void printDevice(DeviceService service, Device device) {
144 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -0700145 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700146 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700147 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300148 if (!isIncluded(port)) {
149 continue;
150 }
151 String portName = portName(port.number());
152 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
153 String portType = port.type().toString().toLowerCase();
154 String annotations = annotations(port.annotations());
155 switch (port.type()) {
156 case OCH:
157 print(FMT_OCH, portName, portIsEnabled, portType,
158 ((OchPort) port).signalType().toString(),
159 ((OchPort) port).isTunable() ? "yes" : "no", annotations);
160 break;
161 case ODUCLT:
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200162 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
Yafit Hadara9a73de2015-09-06 13:52:52 +0300163 ((OduCltPort) port).signalType().toString(), annotations);
164 break;
165 case OMS:
166 print(FMT_OMS, portName, portIsEnabled, portType,
167 ((OmsPort) port).minFrequency().asHz() / Frequency.ofGHz(1).asHz(),
168 ((OmsPort) port).maxFrequency().asHz() / Frequency.ofGHz(1).asHz(),
169 ((OmsPort) port).grid().asHz() / Frequency.ofGHz(1).asHz(),
170 ((OmsPort) port).totalChannels(), annotations);
171 break;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200172 case OTU:
173 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
174 ((OtuPort) port).signalType().toString(), annotations);
175 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300176 default:
177 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
178 break;
tom8350ba52014-10-16 07:22:02 -0700179 }
tomff7eb7c2014-09-08 12:49:03 -0700180 }
181 }
tom6d2a43e2014-09-08 01:50:20 -0700182}