Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [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 | |
| 17 | package org.onosproject.segmentrouting.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Completion; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.cli.PlaceholderCompleter; |
| 25 | import org.onosproject.cli.net.DeviceIdCompleter; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.segmentrouting.phasedrecovery.api.PhasedRecoveryService; |
| 29 | |
| 30 | import java.util.Set; |
| 31 | import 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 | |
| 36 | public 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 | } |