blob: f24d28f3d4d76cc45313f174bf1daeaa8f1de33b [file] [log] [blame]
Charles Chan12a8a842020-02-14 13:23:57 -08001/*
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
17package org.onosproject.segmentrouting.cli;
18
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Completion;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cli.PlaceholderCompleter;
25import org.onosproject.cli.net.DeviceIdCompleter;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.PortNumber;
28import org.onosproject.segmentrouting.phasedrecovery.api.PhasedRecoveryService;
29
30import java.util.Set;
31import java.util.stream.Collectors;
32
33@Service
34@Command(scope = "onos", name = "sr-ports", description = "Enable/Disable group of ports on a specific device")
35
36public class PortsCommand extends AbstractShellCommand {
37 @Argument(index = 0, name = "deviceId",
38 description = "Device ID",
39 required = true, multiValued = false)
40 @Completion(DeviceIdCompleter.class)
41 private String deviceIdStr;
42
43 @Argument(index = 1, name = "ports",
44 description = "Ports to be enabled/disabled: ALL, PAIR, INFRA, EDGE",
45 required = true, multiValued = false)
46 @Completion(PlaceholderCompleter.class)
47 private String portsStr;
48
49 @Argument(index = 2, name = "action",
50 description = "Action: ENABLE, DISABLE",
51 required = true, multiValued = false)
52 @Completion(PlaceholderCompleter.class)
53 private String actionStr;
54
55 @Override
56 protected void doExecute() {
57 PhasedRecoveryService prService = get(PhasedRecoveryService.class);
58
59 DeviceId deviceId = DeviceId.deviceId(deviceIdStr);
60
61 boolean enabled;
62 switch (actionStr.toUpperCase()) {
63 case "ENABLE":
64 enabled = true;
65 break;
66 case "DISABLE":
67 enabled = false;
68 break;
69 default:
70 print("Action should be either ENABLE or DISABLE");
71 return;
72 }
73
74 Set<PortNumber> portsChanged;
75 switch (portsStr.toUpperCase()) {
76 case "ALL":
77 portsChanged = prService.changeAllPorts(deviceId, enabled);
78 break;
79 case "PAIR":
80 portsChanged = prService.changePairPort(deviceId, enabled);
81 break;
82 case "INFRA":
83 portsChanged = prService.changeInfraPorts(deviceId, enabled);
84 break;
85 case "EDGE":
86 portsChanged = prService.changeEdgePorts(deviceId, enabled);
87 break;
88 default:
89 print("Ports should be ALL, PAIR, INFRA, EDGE");
90 return;
91 }
92 print("Ports set to %s: %s",
93 enabled ? "enabled" : "disabled",
94 portsChanged.stream().map(PortNumber::toLong).collect(Collectors.toSet()));
95 }
96}