blob: 610eb6188590349a8f94bb6a700ef2e0ebf46bb6 [file] [log] [blame]
Michael Enrico584eebd2021-07-22 08:04:13 +00001/*
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
19package org.onosproject.cli.net;
20
21import org.apache.karaf.shell.api.action.Argument;
22import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.Completion;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.behaviour.InternalConnectivity;
32import java.util.Set;
33//import java.util.HashSet;
34//import java.util.Comparator;
35//import java.util.Collections;
36import java.util.stream.Collectors;
37import java.util.List;
38import org.slf4j.Logger;
39
40import 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")
49public 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}