blob: 3daa77f14a34828d5f1e295a6d7f8c6233ec40af [file] [log] [blame]
Andreas Papazois92e4a042016-02-24 15:29:30 +02001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Andreas Papazois92e4a042016-02-24 15:29:30 +02003 *
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
Andreas Papazois92e4a042016-02-24 15:29:30 +020021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.behaviour.InterfaceConfig;
24import org.onosproject.net.driver.DriverHandler;
25import org.onosproject.net.driver.DriverService;
26
27/**
Andreas Papazois34a82cf2016-04-27 09:09:13 +030028 * Removes an interface configurion from a device.
Andreas Papazois92e4a042016-02-24 15:29:30 +020029 */
30@Command(scope = "onos", name = "device-remove-interface",
31 description = "Removes an interface configuration from a device")
32public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
33
Andreas Papazois34a82cf2016-04-27 09:09:13 +030034 private static final String ONE_ACTION_ALLOWED =
35 "One configuration removal allowed at a time";
Andreas Papazois827d8d02016-04-15 11:53:16 +030036 private static final String REMOVE_ACCESS_SUCCESS =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030037 "Access mode removed from device %s interface %s.";
Andreas Papazois827d8d02016-04-15 11:53:16 +030038 private static final String REMOVE_ACCESS_FAILURE =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030039 "Failed to remove access mode from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020040 private static final String REMOVE_TRUNK_SUCCESS =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030041 "Trunk mode removed from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020042 private static final String REMOVE_TRUNK_FAILURE =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030043 "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 Papazois92e4a042016-02-24 15:29:30 +020048
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 Papazois34a82cf2016-04-27 09:09:13 +030054 description = "Interface name",
55 required = true, multiValued = false)
Andreas Papazois92e4a042016-02-24 15:29:30 +020056 private String portName = null;
57
Andreas Papazois34a82cf2016-04-27 09:09:13 +030058 @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 Papazois92e4a042016-02-24 15:29:30 +020063 @Option(name = "-t", aliases = "--trunk",
Andreas Papazois59e19bb2016-04-12 13:59:58 +030064 description = "Remove trunk mode for VLAN(s)",
Andreas Papazois92e4a042016-02-24 15:29:30 +020065 required = false, multiValued = false)
66 private boolean trunkMode = false;
67
Andreas Papazois34a82cf2016-04-27 09:09:13 +030068 @Option(name = "-a", aliases = "--access",
69 description = "Remove access mode for VLAN",
70 required = false, multiValued = false)
71 private boolean accessMode = false;
72
Andreas Papazois92e4a042016-02-24 15:29:30 +020073 @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 Papazois34a82cf2016-04-27 09:09:13 +030080 if (trunkMode && !accessMode && !rateLimit) {
Andreas Papazois92e4a042016-02-24 15:29:30 +020081 // Trunk mode for VLAN to be removed.
Andreas Papazois34a82cf2016-04-27 09:09:13 +030082 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 Papazois92e4a042016-02-24 15:29:30 +020089 } else {
Andreas Papazois34a82cf2016-04-27 09:09:13 +030090 // 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 Papazois92e4a042016-02-24 15:29:30 +0200116 }
117 }
118
119}