blob: d61c8bd57f9873350e2f94ab07747381009632c2 [file] [log] [blame]
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -07003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.net.optical.cli;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070021import org.onlab.util.Frequency;
22import org.onosproject.cli.net.DevicePortsListCommand;
23import org.onosproject.net.Device;
24import org.onosproject.net.Port;
25import org.onosproject.net.Port.Type;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.optical.OchPort;
28import org.onosproject.net.optical.OduCltPort;
29import org.onosproject.net.optical.OmsPort;
30import org.onosproject.net.optical.OtuPort;
31
32import java.util.ArrayList;
33import java.util.EnumSet;
34import java.util.List;
35
36import static org.onosproject.net.DeviceId.deviceId;
37import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
38
39/**
40 * Lists all ports or all ports of a device.
41 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070042@Service
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070043@Command(scope = "onos", name = "optical-ports",
44 description = "Lists all optical ports or all optical ports of a device")
45public class OpticalPortsListCommand extends DevicePortsListCommand {
46
47 private static final String FMT = " port=%s, state=%s, type=%s, speed=%s %s";
48 private static final String FMT_OCH = " port=%s, state=%s, type=%s, signalType=%s, isTunable=%s %s";
49 private static final String FMT_ODUCLT_OTU = " port=%s, state=%s, type=%s, signalType=%s %s";
50 private static final String FMT_OMS = " port=%s, state=%s, type=%s, freqs=%s / %s / %s GHz, totalChannels=%s %s";
51
52 private static final EnumSet<Port.Type> OPTICAL = EnumSet.of(Type.OCH, Type.ODUCLT, Type.OMS, Type.OTU);
53
54 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 protected void doExecute() {
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070056 DeviceService service = opticalView(get(DeviceService.class));
57 if (uri == null) {
58 if (outputJson()) {
59 print("%s", jsonPorts(service, getSortedDevices(service)));
60 } else {
61 for (Device device : getSortedDevices(service)) {
62 printDevice(service, device);
63 printPorts(service, device);
64 }
65 }
66
67 } else {
68 Device device = service.getDevice(deviceId(uri));
69 if (device == null) {
70 error("No such device %s", uri);
71 } else if (outputJson()) {
72 print("%s", jsonPorts(service, new ObjectMapper(), device));
73 } else {
74 printDevice(service, device);
75 printPorts(service, device);
76 }
77 }
78 }
79
80 // Determines if a port should be included in output.
81 @Override
82 protected boolean isIncluded(Port port) {
83 return OPTICAL.contains(port.type()) &&
84 super.isIncluded(port);
85 }
86
87 @Override
88 protected void printPorts(DeviceService service, Device device) {
89 List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
90 ports.sort((p1, p2) ->
91 Long.signum(p1.number().toLong() - p2.number().toLong())
92 );
93 for (Port port : ports) {
94 if (!isIncluded(port)) {
95 continue;
96 }
97 String portName = port.number().toString();
98 String portIsEnabled = port.isEnabled() ? "enabled" : "disabled";
99 String portType = port.type().toString().toLowerCase();
100 switch (port.type()) {
101 case OCH:
102 if (port instanceof OchPort) {
103 OchPort och = (OchPort) port;
104 print(FMT_OCH, portName, portIsEnabled, portType,
105 och.signalType().toString(),
106 och.isTunable() ? "yes" : "no",
107 annotations(och.unhandledAnnotations()));
108 break;
109 }
110 print("WARN: OchPort but not on OpticalDevice or ill-formed");
111 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations(port.annotations()));
112 break;
113 case ODUCLT:
114 if (port instanceof OduCltPort) {
115 OduCltPort oduCltPort = (OduCltPort) port;
116 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
117 oduCltPort.signalType().toString(),
118 annotations(oduCltPort.unhandledAnnotations()));
119 break;
120 }
121 print("WARN: OduCltPort but not on OpticalDevice or ill-formed");
122 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations(port.annotations()));
123 break;
124 case OMS:
125 if (port instanceof OmsPort) {
126 OmsPort oms = (OmsPort) port;
127 print(FMT_OMS, portName, portIsEnabled, portType,
128 oms.minFrequency().asHz() / Frequency.ofGHz(1).asHz(),
129 oms.maxFrequency().asHz() / Frequency.ofGHz(1).asHz(),
130 oms.grid().asHz() / Frequency.ofGHz(1).asHz(),
131 oms.totalChannels(),
132 annotations(oms.unhandledAnnotations()));
133 break;
134 }
135 print("WARN: OmsPort but not on OpticalDevice or ill-formed");
136 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations(port.annotations()));
137 break;
138 case OTU:
139 if (port instanceof OtuPort) {
140 OtuPort otuPort = (OtuPort) port;
141 print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType,
142 otuPort.signalType().toString(),
143 annotations(otuPort.unhandledAnnotations()));
144 break;
145 }
146 print("WARN: OtuPort but not on OpticalDevice or ill-formed");
147 print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations(port.annotations()));
148 break;
149 default:
150 // do not print non-optical ports
151 break;
152 }
153 }
154 }
155}