Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 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 | package org.onosproject.cli.net; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.behaviour.InterfaceConfig; |
| 26 | import org.onosproject.net.driver.DriverHandler; |
| 27 | import org.onosproject.net.driver.DriverService; |
| 28 | |
| 29 | /** |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 30 | * Removes an interface configurion from a device. |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 31 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 32 | @Service |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 33 | @Command(scope = "onos", name = "device-remove-interface", |
| 34 | description = "Removes an interface configuration from a device") |
| 35 | public class DeviceInterfaceRemoveCommand extends AbstractShellCommand { |
| 36 | |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 37 | private static final String ONE_ACTION_ALLOWED = |
| 38 | "One configuration removal allowed at a time"; |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 39 | private static final String REMOVE_ACCESS_SUCCESS = |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 40 | "Access mode removed from device %s interface %s."; |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 41 | private static final String REMOVE_ACCESS_FAILURE = |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 42 | "Failed to remove access mode from device %s interface %s."; |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 43 | private static final String REMOVE_TRUNK_SUCCESS = |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 44 | "Trunk mode removed from device %s interface %s."; |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 45 | private static final String REMOVE_TRUNK_FAILURE = |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 46 | "Failed to remove trunk mode from device %s interface %s."; |
| 47 | private static final String REMOVE_RATE_SUCCESS = |
| 48 | "Rate limit removed from device %s interface %s."; |
| 49 | private static final String REMOVE_RATE_FAILURE = |
| 50 | "Failed to remove rate limit from device %s interface %s."; |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 51 | |
| 52 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 53 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 54 | @Completion(DeviceIdCompleter.class) |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 55 | private String uri = null; |
| 56 | |
| 57 | @Argument(index = 1, name = "interface", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 58 | description = "Interface name", |
| 59 | required = true, multiValued = false) |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 60 | private String portName = null; |
| 61 | |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 62 | @Option(name = "-r", aliases = "--rate-limit", |
| 63 | description = "Percentage for egress bandwidth limit", |
| 64 | required = false, multiValued = false) |
| 65 | private boolean rateLimit = false; |
| 66 | |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 67 | @Option(name = "-t", aliases = "--trunk", |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 68 | description = "Remove trunk mode for VLAN(s)", |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 69 | required = false, multiValued = false) |
| 70 | private boolean trunkMode = false; |
| 71 | |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 72 | @Option(name = "-a", aliases = "--access", |
| 73 | description = "Remove access mode for VLAN", |
| 74 | required = false, multiValued = false) |
| 75 | private boolean accessMode = false; |
| 76 | |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 77 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 78 | protected void doExecute() { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 79 | DriverService service = get(DriverService.class); |
| 80 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 81 | DriverHandler h = service.createHandler(deviceId); |
| 82 | InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); |
| 83 | |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 84 | if (trunkMode && !accessMode && !rateLimit) { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 85 | // Trunk mode for VLAN to be removed. |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 86 | removeTrunkModeFromIntf(interfaceConfig); |
| 87 | } else if (accessMode && !trunkMode && !rateLimit) { |
| 88 | // Access mode for VLAN to be removed. |
| 89 | removeAccessModeFromIntf(interfaceConfig); |
| 90 | } else if (rateLimit && !trunkMode && !accessMode) { |
| 91 | // Rate limit to be removed. |
| 92 | removeRateLimitFromIntf(interfaceConfig); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 93 | } else { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 94 | // Option has not been correctly set. |
| 95 | print(ONE_ACTION_ALLOWED); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private void removeAccessModeFromIntf(InterfaceConfig interfaceConfig) { |
| 100 | if (interfaceConfig.removeAccessMode(portName)) { |
| 101 | print(REMOVE_ACCESS_SUCCESS, uri, portName); |
| 102 | } else { |
| 103 | print(REMOVE_ACCESS_FAILURE, uri, portName); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private void removeTrunkModeFromIntf(InterfaceConfig interfaceConfig) { |
| 108 | if (interfaceConfig.removeTrunkMode(portName)) { |
| 109 | print(REMOVE_TRUNK_SUCCESS, uri, portName); |
| 110 | } else { |
| 111 | print(REMOVE_TRUNK_FAILURE, uri, portName); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private void removeRateLimitFromIntf(InterfaceConfig config) { |
| 116 | if (config.removeRateLimit(portName)) { |
| 117 | print(REMOVE_RATE_SUCCESS, uri, portName); |
| 118 | } else { |
| 119 | print(REMOVE_RATE_FAILURE, uri, portName); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | } |