blob: 5c994d131f4341dcf85a2362ad49e8e6ec5563c5 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Ray Milkeyc7477292016-03-11 10:53:43 -080026import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.Device;
28import org.onosproject.net.Port;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.device.DeviceService;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080031import org.onosproject.net.optical.OchPort;
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -070032import org.onosproject.net.optical.OduCltPort;
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -070033import org.onosproject.net.optical.OmsPort;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080034import org.onosproject.net.optical.OpticalDevice;
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -070035import org.onosproject.net.optical.OtuPort;
36
tomff7eb7c2014-09-08 12:49:03 -070037import java.util.ArrayList;
38import java.util.Collections;
tomff7eb7c2014-09-08 12:49:03 -070039import java.util.List;
40
Brian O'Connorabafb502014-12-02 22:26:20 -080041import static org.onosproject.net.DeviceId.deviceId;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080042import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
tom6d2a43e2014-09-08 01:50:20 -070043
44/**
tomc290a122014-09-08 14:27:13 -070045 * Lists all ports or all ports of a device.
tom6d2a43e2014-09-08 01:50:20 -070046 */
47@Command(scope = "onos", name = "ports",
tomc290a122014-09-08 14:27:13 -070048 description = "Lists all ports or all ports of a device")
tomff7eb7c2014-09-08 12:49:03 -070049public class DevicePortsListCommand extends DevicesListCommand {
tom6d2a43e2014-09-08 01:50:20 -070050
Yafit Hadara9a73de2015-09-06 13:52:52 +030051 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
52 private static final String FMT_OCH = " port=%s, state=%s, type=%s, signalType=%s, isTunable=%s %s";
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020053 private static final String FMT_ODUCLT_OTU = " port=%s, state=%s, type=%s, signalType=%s %s";
Yafit Hadara9a73de2015-09-06 13:52:52 +030054 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 -070055
tom8350ba52014-10-16 07:22:02 -070056 @Option(name = "-e", aliases = "--enabled", description = "Show only enabled ports",
57 required = false, multiValued = false)
58 private boolean enabled = false;
59
60 @Option(name = "-d", aliases = "--disabled", description = "Show only disabled ports",
61 required = false, multiValued = false)
62 private boolean disabled = false;
63
tomc290a122014-09-08 14:27:13 -070064 @Argument(index = 0, name = "uri", description = "Device ID",
tomff7eb7c2014-09-08 12:49:03 -070065 required = false, multiValued = false)
66 String uri = null;
67
tom6d2a43e2014-09-08 01:50:20 -070068 @Override
tom0872a172014-09-23 11:24:26 -070069 protected void execute() {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080070 DeviceService service = opticalView(get(DeviceService.class));
tomff7eb7c2014-09-08 12:49:03 -070071 if (uri == null) {
tom32085cf2014-10-16 00:04:33 -070072 if (outputJson()) {
73 print("%s", jsonPorts(service, getSortedDevices(service)));
74 } else {
75 for (Device device : getSortedDevices(service)) {
76 printDevice(service, device);
77 }
tomff7eb7c2014-09-08 12:49:03 -070078 }
tom32085cf2014-10-16 00:04:33 -070079
tomff7eb7c2014-09-08 12:49:03 -070080 } else {
tom9eb57fb2014-09-11 19:42:38 -070081 Device device = service.getDevice(deviceId(uri));
82 if (device == null) {
83 error("No such device %s", uri);
tom32085cf2014-10-16 00:04:33 -070084 } else if (outputJson()) {
85 print("%s", jsonPorts(service, new ObjectMapper(), device));
tom9eb57fb2014-09-11 19:42:38 -070086 } else {
87 printDevice(service, device);
88 }
tom6d2a43e2014-09-08 01:50:20 -070089 }
tom6d2a43e2014-09-08 01:50:20 -070090 }
tomff7eb7c2014-09-08 12:49:03 -070091
tom32085cf2014-10-16 00:04:33 -070092 /**
93 * Produces JSON array containing ports of the specified devices.
94 *
95 * @param service device service
96 * @param devices collection of devices
97 * @return JSON array
98 */
tom8350ba52014-10-16 07:22:02 -070099 public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) {
tom32085cf2014-10-16 00:04:33 -0700100 ObjectMapper mapper = new ObjectMapper();
101 ArrayNode result = mapper.createArrayNode();
102 for (Device device : devices) {
103 result.add(jsonPorts(service, mapper, device));
104 }
105 return result;
106 }
107
108 /**
109 * Produces JSON array containing ports of the specified device.
110 *
111 * @param service device service
112 * @param mapper object mapper
113 * @param device infrastructure devices
114 * @return JSON array
115 */
tom8350ba52014-10-16 07:22:02 -0700116 public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) {
tom32085cf2014-10-16 00:04:33 -0700117 ObjectNode result = mapper.createObjectNode();
118 ArrayNode ports = mapper.createArrayNode();
119 for (Port port : service.getPorts(device.id())) {
tom8350ba52014-10-16 07:22:02 -0700120 if (isIncluded(port)) {
121 ports.add(mapper.createObjectNode()
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800122 .put("port", portName(port.number()))
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700123 .put("isEnabled", port.isEnabled())
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700124 .put("type", port.type().toString().toLowerCase())
125 .put("portSpeed", port.portSpeed())
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700126 .set("annotations", annotations(mapper, port.annotations())));
tom8350ba52014-10-16 07:22:02 -0700127 }
tom32085cf2014-10-16 00:04:33 -0700128 }
Ray Milkey3078fc02015-05-06 16:14:14 -0700129 result.set("device", jsonForEntity(device, Device.class));
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700130 result.set("ports", ports);
131 return result;
tom32085cf2014-10-16 00:04:33 -0700132 }
133
Thomas Vachuskacaa14d62014-11-05 16:28:33 -0800134 private String portName(PortNumber port) {
135 return port.equals(PortNumber.LOCAL) ? "local" : port.toString();
136 }
137
tom8350ba52014-10-16 07:22:02 -0700138 // Determines if a port should be included in output.
139 private boolean isIncluded(Port port) {
140 return enabled && port.isEnabled() || disabled && !port.isEnabled() ||
141 !enabled && !disabled;
142 }
143
tomc290a122014-09-08 14:27:13 -0700144 @Override
145 protected void printDevice(DeviceService service, Device device) {
146 super.printDevice(service, device);
tomff7eb7c2014-09-08 12:49:03 -0700147 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
tom1380eee2014-09-24 09:22:02 -0700148 Collections.sort(ports, Comparators.PORT_COMPARATOR);
tomff7eb7c2014-09-08 12:49:03 -0700149 for (Port port : ports) {
Yafit Hadara9a73de2015-09-06 13:52:52 +0300150 if (!isIncluded(port)) {
151 continue;
152 }
153 String portName = portName(port.number());
154 Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
155 String portType = port.type().toString().toLowerCase();
156 String annotations = annotations(port.annotations());
157 switch (port.type()) {
158 case OCH:
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800159 if (port instanceof org.onosproject.net.OchPort) {
160 // old OchPort model
161 org.onosproject.net.OchPort oPort = (org.onosproject.net.OchPort) port;
162 print("WARN: OchPort in old model");
163 print(FMT_OCH, portName, portIsEnabled, portType,
164 oPort.signalType().toString(),
165 oPort.isTunable() ? "yes" : "no", annotations);
166 break;
167 }
168 if (port instanceof OchPort) {
169 OchPort och = (OchPort) port;
170 print(FMT_OCH, portName, portIsEnabled, portType,
171 och.signalType().toString(),
172 och.isTunable() ? "yes" : "no", annotations);
173 break;
174 } else if (port.element().is(OpticalDevice.class)) {
175 // Note: should never reach here, but
176 // leaving it here as an example to
177 // manually translate to specific port.
178 OpticalDevice optDevice = port.element().as(OpticalDevice.class);
179 if (optDevice.portIs(port, OchPort.class)) {
180 OchPort och = optDevice.portAs(port, OchPort.class).get();
181 print(FMT_OCH, portName, portIsEnabled, portType,
182 och.signalType().toString(),
183 och.isTunable() ? "yes" : "no", annotations);
184 break;
185 }
186 }
187 print("WARN: OchPort but not on OpticalDevice or ill-formed");
188 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
189 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300190 case ODUCLT:
HIGUCHI Yuta4c0ef6b2016-05-02 19:45:41 -0700191 if (port instanceof org.onosproject.net.OduCltPort) {
192 // old OduCltPort model
193 org.onosproject.net.OduCltPort oPort = (org.onosproject.net.OduCltPort) port;
194 print("WARN: OduCltPort in old model");
195 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
196 oPort.signalType().toString(), annotations);
197 break;
198 }
199 if (port instanceof OduCltPort) {
200 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
201 ((OduCltPort) port).signalType().toString(), annotations);
202 break;
203 }
204 print("WARN: OduCltPort but not on OpticalDevice or ill-formed");
205 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
206 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300207 case OMS:
HIGUCHI Yuta95d83e82016-04-26 12:13:48 -0700208 if (port instanceof org.onosproject.net.OmsPort) {
209 org.onosproject.net.OmsPort oms = (org.onosproject.net.OmsPort) port;
210 print("WARN: OmsPort in old model");
211 print(FMT_OMS, portName, portIsEnabled, portType,
212 oms.minFrequency().asHz() / Frequency.ofGHz(1).asHz(),
213 oms.maxFrequency().asHz() / Frequency.ofGHz(1).asHz(),
214 oms.grid().asHz() / Frequency.ofGHz(1).asHz(),
215 oms.totalChannels(), annotations);
216 break;
217 }
218 if (port instanceof OmsPort) {
219 OmsPort oms = (OmsPort) port;
220 print(FMT_OMS, portName, portIsEnabled, portType,
221 oms.minFrequency().asHz() / Frequency.ofGHz(1).asHz(),
222 oms.maxFrequency().asHz() / Frequency.ofGHz(1).asHz(),
223 oms.grid().asHz() / Frequency.ofGHz(1).asHz(),
224 oms.totalChannels(), annotations);
225 break;
226 }
227 print("WARN: OmsPort but not on OpticalDevice or ill-formed");
228 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
Yafit Hadara9a73de2015-09-06 13:52:52 +0300229 break;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200230 case OTU:
HIGUCHI Yuta5be3e822016-05-03 13:51:42 -0700231 if (port instanceof org.onosproject.net.OtuPort) {
232 org.onosproject.net.OtuPort otu = (org.onosproject.net.OtuPort) port;
233 print("WARN: OtuPort in old model");
234 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
235 otu.signalType().toString(), annotations);
236 break;
237 }
238 if (port instanceof OtuPort) {
239 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
240 ((OtuPort) port).signalType().toString(), annotations);
241 break;
242 }
243 print("WARN: OtuPort but not on OpticalDevice or ill-formed");
244 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200245 break;
Yafit Hadara9a73de2015-09-06 13:52:52 +0300246 default:
247 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations);
248 break;
tom8350ba52014-10-16 07:22:02 -0700249 }
tomff7eb7c2014-09-08 12:49:03 -0700250 }
251 }
tom6d2a43e2014-09-08 01:50:20 -0700252}