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