Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 2 | * Copyright 2015 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 25 | import org.onlab.util.Frequency; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.Comparators; |
| 27 | import org.onosproject.net.Device; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 28 | import org.onosproject.net.OchPort; |
| 29 | import org.onosproject.net.OduCltPort; |
| 30 | import org.onosproject.net.OmsPort; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 31 | import org.onosproject.net.OtuPort; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Port; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.device.DeviceService; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 35 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 43 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 44 | */ |
| 45 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 46 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 47 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 48 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 49 | 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 Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 51 | private static final String FMT_ODUCLT_OTU = " port=%s, state=%s, type=%s, signalType=%s %s"; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 52 | private static final String FMT_OMS = " port=%s, state=%s, type=%s, Freqs= %s / %s / %s GHz, totalChannels=%s %s"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 53 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 54 | @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 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 62 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 63 | required = false, multiValued = false) |
| 64 | String uri = null; |
| 65 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 66 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 67 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 68 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 69 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 70 | if (outputJson()) { |
| 71 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 72 | } else { |
| 73 | for (Device device : getSortedDevices(service)) { |
| 74 | printDevice(service, device); |
| 75 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 76 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 77 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 78 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 79 | Device device = service.getDevice(deviceId(uri)); |
| 80 | if (device == null) { |
| 81 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 82 | } else if (outputJson()) { |
| 83 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 84 | } else { |
| 85 | printDevice(service, device); |
| 86 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 87 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 88 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 89 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 90 | /** |
| 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 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 97 | public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 98 | 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 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 114 | public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 115 | ObjectNode result = mapper.createObjectNode(); |
| 116 | ArrayNode ports = mapper.createArrayNode(); |
| 117 | for (Port port : service.getPorts(device.id())) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 118 | if (isIncluded(port)) { |
| 119 | ports.add(mapper.createObjectNode() |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 120 | .put("port", portName(port.number())) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 121 | .put("isEnabled", port.isEnabled()) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 122 | .put("type", port.type().toString().toLowerCase()) |
| 123 | .put("portSpeed", port.portSpeed()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 124 | .set("annotations", annotations(mapper, port.annotations()))); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 125 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 126 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 127 | result.set("device", jsonForEntity(device, Device.class)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 128 | result.set("ports", ports); |
| 129 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 132 | private String portName(PortNumber port) { |
| 133 | return port.equals(PortNumber.LOCAL) ? "local" : port.toString(); |
| 134 | } |
| 135 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 136 | // 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 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 142 | @Override |
| 143 | protected void printDevice(DeviceService service, Device device) { |
| 144 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 145 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 146 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 147 | for (Port port : ports) { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 148 | 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 Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 162 | print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType, |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 163 | ((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 Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 172 | case OTU: |
| 173 | print(FMT_ODUCLT_OTU, portName, portIsEnabled, portType, |
| 174 | ((OtuPort) port).signalType().toString(), annotations); |
| 175 | break; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 176 | default: |
| 177 | print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations); |
| 178 | break; |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 179 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 182 | } |