blob: 8cdd7b445ffe3d0af2f3ed11019a8daade899f20 [file] [log] [blame]
Andreas Papazois92e4a042016-02-24 15:29:30 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
21import org.apache.karaf.shell.api.action.Option;
Andreas Papazois92e4a042016-02-24 15:29:30 +020022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.behaviour.InterfaceConfig;
25import org.onosproject.net.driver.DriverHandler;
26import org.onosproject.net.driver.DriverService;
27
28/**
Andreas Papazois34a82cf2016-04-27 09:09:13 +030029 * Removes an interface configurion from a device.
Andreas Papazois92e4a042016-02-24 15:29:30 +020030 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031@Service
Andreas Papazois92e4a042016-02-24 15:29:30 +020032@Command(scope = "onos", name = "device-remove-interface",
33 description = "Removes an interface configuration from a device")
34public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
35
Andreas Papazois34a82cf2016-04-27 09:09:13 +030036 private static final String ONE_ACTION_ALLOWED =
37 "One configuration removal allowed at a time";
Andreas Papazois827d8d02016-04-15 11:53:16 +030038 private static final String REMOVE_ACCESS_SUCCESS =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030039 "Access mode removed from device %s interface %s.";
Andreas Papazois827d8d02016-04-15 11:53:16 +030040 private static final String REMOVE_ACCESS_FAILURE =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030041 "Failed to remove access mode from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020042 private static final String REMOVE_TRUNK_SUCCESS =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030043 "Trunk mode removed from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020044 private static final String REMOVE_TRUNK_FAILURE =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030045 "Failed to remove trunk mode from device %s interface %s.";
46 private static final String REMOVE_RATE_SUCCESS =
47 "Rate limit removed from device %s interface %s.";
48 private static final String REMOVE_RATE_FAILURE =
49 "Failed to remove rate limit from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020050
51 @Argument(index = 0, name = "uri", description = "Device ID",
52 required = true, multiValued = false)
53 private String uri = null;
54
55 @Argument(index = 1, name = "interface",
Andreas Papazois34a82cf2016-04-27 09:09:13 +030056 description = "Interface name",
57 required = true, multiValued = false)
Andreas Papazois92e4a042016-02-24 15:29:30 +020058 private String portName = null;
59
Andreas Papazois34a82cf2016-04-27 09:09:13 +030060 @Option(name = "-r", aliases = "--rate-limit",
61 description = "Percentage for egress bandwidth limit",
62 required = false, multiValued = false)
63 private boolean rateLimit = false;
64
Andreas Papazois92e4a042016-02-24 15:29:30 +020065 @Option(name = "-t", aliases = "--trunk",
Andreas Papazois59e19bb2016-04-12 13:59:58 +030066 description = "Remove trunk mode for VLAN(s)",
Andreas Papazois92e4a042016-02-24 15:29:30 +020067 required = false, multiValued = false)
68 private boolean trunkMode = false;
69
Andreas Papazois34a82cf2016-04-27 09:09:13 +030070 @Option(name = "-a", aliases = "--access",
71 description = "Remove access mode for VLAN",
72 required = false, multiValued = false)
73 private boolean accessMode = false;
74
Andreas Papazois92e4a042016-02-24 15:29:30 +020075 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 protected void doExecute() {
Andreas Papazois92e4a042016-02-24 15:29:30 +020077 DriverService service = get(DriverService.class);
78 DeviceId deviceId = DeviceId.deviceId(uri);
79 DriverHandler h = service.createHandler(deviceId);
80 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
81
Andreas Papazois34a82cf2016-04-27 09:09:13 +030082 if (trunkMode && !accessMode && !rateLimit) {
Andreas Papazois92e4a042016-02-24 15:29:30 +020083 // Trunk mode for VLAN to be removed.
Andreas Papazois34a82cf2016-04-27 09:09:13 +030084 removeTrunkModeFromIntf(interfaceConfig);
85 } else if (accessMode && !trunkMode && !rateLimit) {
86 // Access mode for VLAN to be removed.
87 removeAccessModeFromIntf(interfaceConfig);
88 } else if (rateLimit && !trunkMode && !accessMode) {
89 // Rate limit to be removed.
90 removeRateLimitFromIntf(interfaceConfig);
Andreas Papazois92e4a042016-02-24 15:29:30 +020091 } else {
Andreas Papazois34a82cf2016-04-27 09:09:13 +030092 // Option has not been correctly set.
93 print(ONE_ACTION_ALLOWED);
94 }
95 }
96
97 private void removeAccessModeFromIntf(InterfaceConfig interfaceConfig) {
98 if (interfaceConfig.removeAccessMode(portName)) {
99 print(REMOVE_ACCESS_SUCCESS, uri, portName);
100 } else {
101 print(REMOVE_ACCESS_FAILURE, uri, portName);
102 }
103 }
104
105 private void removeTrunkModeFromIntf(InterfaceConfig interfaceConfig) {
106 if (interfaceConfig.removeTrunkMode(portName)) {
107 print(REMOVE_TRUNK_SUCCESS, uri, portName);
108 } else {
109 print(REMOVE_TRUNK_FAILURE, uri, portName);
110 }
111 }
112
113 private void removeRateLimitFromIntf(InterfaceConfig config) {
114 if (config.removeRateLimit(portName)) {
115 print(REMOVE_RATE_SUCCESS, uri, portName);
116 } else {
117 print(REMOVE_RATE_FAILURE, uri, portName);
Andreas Papazois92e4a042016-02-24 15:29:30 +0200118 }
119 }
120
121}