blob: 527e1331aba280389d3dc4ac5b21ed1d8401d7ab [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Andreas Papazois92e4a042016-02-24 15:29:30 +020023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.behaviour.InterfaceConfig;
26import org.onosproject.net.driver.DriverHandler;
27import org.onosproject.net.driver.DriverService;
28
29/**
Andreas Papazois34a82cf2016-04-27 09:09:13 +030030 * Removes an interface configurion from a device.
Andreas Papazois92e4a042016-02-24 15:29:30 +020031 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032@Service
Andreas Papazois92e4a042016-02-24 15:29:30 +020033@Command(scope = "onos", name = "device-remove-interface",
34 description = "Removes an interface configuration from a device")
35public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
36
Andreas Papazois34a82cf2016-04-27 09:09:13 +030037 private static final String ONE_ACTION_ALLOWED =
38 "One configuration removal allowed at a time";
Andreas Papazois827d8d02016-04-15 11:53:16 +030039 private static final String REMOVE_ACCESS_SUCCESS =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030040 "Access mode removed from device %s interface %s.";
Andreas Papazois827d8d02016-04-15 11:53:16 +030041 private static final String REMOVE_ACCESS_FAILURE =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030042 "Failed to remove access mode from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020043 private static final String REMOVE_TRUNK_SUCCESS =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030044 "Trunk mode removed from device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020045 private static final String REMOVE_TRUNK_FAILURE =
Andreas Papazois34a82cf2016-04-27 09:09:13 +030046 "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 Papazois92e4a042016-02-24 15:29:30 +020051
52 @Argument(index = 0, name = "uri", description = "Device ID",
53 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070054 @Completion(DeviceIdCompleter.class)
Andreas Papazois92e4a042016-02-24 15:29:30 +020055 private String uri = null;
56
57 @Argument(index = 1, name = "interface",
Andreas Papazois34a82cf2016-04-27 09:09:13 +030058 description = "Interface name",
59 required = true, multiValued = false)
Andreas Papazois92e4a042016-02-24 15:29:30 +020060 private String portName = null;
61
Andreas Papazois34a82cf2016-04-27 09:09:13 +030062 @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 Papazois92e4a042016-02-24 15:29:30 +020067 @Option(name = "-t", aliases = "--trunk",
Andreas Papazois59e19bb2016-04-12 13:59:58 +030068 description = "Remove trunk mode for VLAN(s)",
Andreas Papazois92e4a042016-02-24 15:29:30 +020069 required = false, multiValued = false)
70 private boolean trunkMode = false;
71
Andreas Papazois34a82cf2016-04-27 09:09:13 +030072 @Option(name = "-a", aliases = "--access",
73 description = "Remove access mode for VLAN",
74 required = false, multiValued = false)
75 private boolean accessMode = false;
76
Andreas Papazois92e4a042016-02-24 15:29:30 +020077 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 protected void doExecute() {
Andreas Papazois92e4a042016-02-24 15:29:30 +020079 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 Papazois34a82cf2016-04-27 09:09:13 +030084 if (trunkMode && !accessMode && !rateLimit) {
Andreas Papazois92e4a042016-02-24 15:29:30 +020085 // Trunk mode for VLAN to be removed.
Andreas Papazois34a82cf2016-04-27 09:09:13 +030086 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 Papazois92e4a042016-02-24 15:29:30 +020093 } else {
Andreas Papazois34a82cf2016-04-27 09:09:13 +030094 // 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 Papazois92e4a042016-02-24 15:29:30 +0200120 }
121 }
122
123}