blob: 939bdc492cd841990e3a076f902b097a0391c7da [file] [log] [blame]
alessio0af65232020-09-19 17:06:37 +02001/*
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 org.slf4j.Logger;
33
34import static org.slf4j.LoggerFactory.getLogger;
35
36@Service
37@Command(scope = "onos", name = "check-internal-connectivity",
38 description = "Check if two port of a device can be connected")
39public class InternalConnectivityCommand extends AbstractShellCommand {
40
41 private static final Logger log = getLogger(BitErrorCommand.class);
42
43 @Argument(index = 0, name = "input port", description = "{DeviceID}/{PortNumber}",
44 required = true, multiValued = false)
45 @Completion(ConnectPointCompleter.class)
46 private String input = null;
47
48 @Argument(index = 1, name = "output port", description = "{DeviceID}/{PortNumber}",
49 required = true, multiValued = false)
50 @Completion(ConnectPointCompleter.class)
51 private String output = null;
52
53 @Override
54 protected void doExecute() throws Exception {
55 ConnectPoint inputConnectPoint = ConnectPoint.deviceConnectPoint(input);
56 ConnectPoint outputConnectPoint = ConnectPoint.deviceConnectPoint(output);
57
58 DeviceService deviceService = get(DeviceService.class);
59 DeviceId inputDeviceId = inputConnectPoint.deviceId();
60 DeviceId outputDeviceId = outputConnectPoint.deviceId();
61 PortNumber inputPortNumber = inputConnectPoint.port();
62 PortNumber outputPortNumber = outputConnectPoint.port();
63
64 InternalConnectivity internalConnectivityBehaviour;
65
66 if (!inputDeviceId.equals(outputDeviceId)) {
67 print("[ERROR] specified connect points should belong to the same device.");
68 return;
69 }
70
71 Device device = deviceService.getDevice(inputDeviceId);
72
73 if (device != null && device.is(InternalConnectivity.class)) {
74 internalConnectivityBehaviour = device.as(InternalConnectivity.class);
75 } else {
76 print("[ERROR] specified device %s does not support Internal Connectivity Behaviour.",
77 device.toString());
78 return;
79 }
80
81 if (internalConnectivityBehaviour.testConnectivity(inputPortNumber, outputPortNumber)) {
82 print("[CONNECTIVITY ALLOWED] device %s from input-port %s to output-port %s",
83 device.id().toString(),
84 inputPortNumber.toString(),
85 outputPortNumber.toString());
86 } else {
87 print("[CONNECTIVITY NOT ALLOWED] device %s from input-port %s to output-port %s",
88 device.id().toString(),
89 inputPortNumber.toString(),
90 outputPortNumber.toString());
91 }
92 }
93}