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