Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | * This work was partially supported by EC H2020 project METRO-HAUL (761727). |
| 17 | */ |
| 18 | |
| 19 | package org.onosproject.cli.net; |
| 20 | |
| 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.Completion; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.behaviour.InternalConnectivity; |
| 32 | import java.util.Set; |
| 33 | //import java.util.HashSet; |
| 34 | //import java.util.Comparator; |
| 35 | //import java.util.Collections; |
| 36 | import java.util.stream.Collectors; |
| 37 | import java.util.List; |
| 38 | import org.slf4j.Logger; |
| 39 | |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Lists possible output ports to which a given input port can be internally connected on a single device. |
| 44 | */ |
| 45 | @Service |
| 46 | @Command(scope = "onos", name = "get-output-ports", |
| 47 | description = "Lists possible output ports to which a given input port can be internally connected " + |
| 48 | "on a single device") |
| 49 | public class GetInternalConnectivityOutputPortsCommand extends AbstractShellCommand { |
| 50 | |
| 51 | private static final Logger log = getLogger(BitErrorCommand.class); |
| 52 | |
| 53 | @Argument(index = 0, name = "input port", description = "{DeviceID}/{PortNumber}", |
| 54 | required = true, multiValued = false) |
| 55 | @Completion(ConnectPointCompleter.class) |
| 56 | private String input = null; |
| 57 | |
| 58 | @Override |
| 59 | protected void doExecute() throws Exception { |
| 60 | ConnectPoint inputConnectPoint = ConnectPoint.deviceConnectPoint(input); |
| 61 | |
| 62 | DeviceService deviceService = get(DeviceService.class); |
| 63 | DeviceId inputDeviceId = inputConnectPoint.deviceId(); |
| 64 | PortNumber inputPortNumber = inputConnectPoint.port(); |
| 65 | |
| 66 | InternalConnectivity internalConnectivityBehaviour; |
| 67 | |
| 68 | Device device = deviceService.getDevice(inputDeviceId); |
| 69 | |
| 70 | if (device != null && device.is(InternalConnectivity.class)) { |
| 71 | internalConnectivityBehaviour = device.as(InternalConnectivity.class); |
| 72 | } else { |
| 73 | print("[ERROR] specified device %s does not support Internal Connectivity Behaviour.", |
| 74 | device.toString()); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | Set<PortNumber> outputPorts = internalConnectivityBehaviour.getOutputPorts(inputPortNumber); |
| 79 | List<PortNumber> outputPortsList = outputPorts.stream().sorted((a, b) |
| 80 | -> (int) (a.toLong() - b.toLong())).collect(Collectors.toList()); |
| 81 | |
| 82 | if (outputPorts.isEmpty()) { |
| 83 | print("[POSSIBLE OUTPUT PORTS] None!"); |
| 84 | } else { |
| 85 | print("[POSSIBLE OUTPUT PORTS] " + outputPortsList.toString()); |
| 86 | } |
| 87 | } |
| 88 | } |