Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 21 | import org.onlab.util.Frequency; |
| 22 | import org.onosproject.cli.net.DevicePortsListCommand; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.Port.Type; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.optical.OchPort; |
| 28 | import org.onosproject.net.optical.OduCltPort; |
| 29 | import org.onosproject.net.optical.OmsPort; |
| 30 | import org.onosproject.net.optical.OtuPort; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.EnumSet; |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.onosproject.net.DeviceId.deviceId; |
| 37 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
| 38 | |
| 39 | /** |
| 40 | * Lists all ports or all ports of a device. |
| 41 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 42 | @Service |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 43 | @Command(scope = "onos", name = "optical-ports", |
| 44 | description = "Lists all optical ports or all optical ports of a device") |
| 45 | public 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | protected void doExecute() { |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 56 | 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 | } |