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; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceService; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 34 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.Collections; |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 37 | import java.util.List; |
| 38 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import static org.onosproject.net.DeviceId.deviceId; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 42 | * Lists all ports or all ports of a device. |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 43 | */ |
| 44 | @Command(scope = "onos", name = "ports", |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 45 | description = "Lists all ports or all ports of a device") |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 46 | public class DevicePortsListCommand extends DevicesListCommand { |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 47 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 48 | 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"; |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 52 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 53 | @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 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 61 | @Argument(index = 0, name = "uri", description = "Device ID", |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 62 | required = false, multiValued = false) |
| 63 | String uri = null; |
| 64 | |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 65 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 66 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 67 | DeviceService service = get(DeviceService.class); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 68 | if (uri == null) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 69 | if (outputJson()) { |
| 70 | print("%s", jsonPorts(service, getSortedDevices(service))); |
| 71 | } else { |
| 72 | for (Device device : getSortedDevices(service)) { |
| 73 | printDevice(service, device); |
| 74 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 75 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 76 | |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 77 | } else { |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 78 | Device device = service.getDevice(deviceId(uri)); |
| 79 | if (device == null) { |
| 80 | error("No such device %s", uri); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 81 | } else if (outputJson()) { |
| 82 | print("%s", jsonPorts(service, new ObjectMapper(), device)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 83 | } else { |
| 84 | printDevice(service, device); |
| 85 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 86 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 87 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 88 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 89 | /** |
| 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 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 96 | public JsonNode jsonPorts(DeviceService service, Iterable<Device> devices) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 97 | 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 | */ |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 113 | public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 114 | ObjectNode result = mapper.createObjectNode(); |
| 115 | ArrayNode ports = mapper.createArrayNode(); |
| 116 | for (Port port : service.getPorts(device.id())) { |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 117 | if (isIncluded(port)) { |
| 118 | ports.add(mapper.createObjectNode() |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 119 | .put("port", portName(port.number())) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 120 | .put("isEnabled", port.isEnabled()) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 121 | .put("type", port.type().toString().toLowerCase()) |
| 122 | .put("portSpeed", port.portSpeed()) |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 123 | .set("annotations", annotations(mapper, port.annotations()))); |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 124 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 125 | } |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 126 | result.set("device", jsonForEntity(device, Device.class)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 127 | result.set("ports", ports); |
| 128 | return result; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Thomas Vachuska | caa14d6 | 2014-11-05 16:28:33 -0800 | [diff] [blame] | 131 | private String portName(PortNumber port) { |
| 132 | return port.equals(PortNumber.LOCAL) ? "local" : port.toString(); |
| 133 | } |
| 134 | |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 135 | // 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 | |
tom | c290a12 | 2014-09-08 14:27:13 -0700 | [diff] [blame] | 141 | @Override |
| 142 | protected void printDevice(DeviceService service, Device device) { |
| 143 | super.printDevice(service, device); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 144 | List<Port> ports = new ArrayList<>(service.getPorts(device.id())); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 145 | Collections.sort(ports, Comparators.PORT_COMPARATOR); |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 146 | for (Port port : ports) { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 147 | 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; |
tom | 8350ba5 | 2014-10-16 07:22:02 -0700 | [diff] [blame] | 174 | } |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
tom | 6d2a43e | 2014-09-08 01:50:20 -0700 | [diff] [blame] | 177 | } |