blob: 643fbb42b349d6d930e55758039f22e0c3d59713 [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;
21import org.onlab.packet.VlanId;
22import 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
Andreas Papazois59e19bb2016-04-12 13:59:58 +030028import java.util.ArrayList;
29import java.util.List;
30
Andreas Papazois92e4a042016-02-24 15:29:30 +020031/**
32 * Removes configured interface from a device.
33 */
34@Command(scope = "onos", name = "device-remove-interface",
35 description = "Removes an interface configuration from a device")
36public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
37
38 private static final String REMOVE_VLAN_SUCCESS =
39 "VLAN %s removed from device %s interface %s.";
40 private static final String REMOVE_VLAN_FAILURE =
41 "Failed to remove VLAN %s from device %s interface %s.";
Andreas Papazois59e19bb2016-04-12 13:59:58 +030042 private static final String ONE_VLAN_ALLOWED =
43 "Only one VLAN allowed for access mode on device %s interface %s.";
Andreas Papazois92e4a042016-02-24 15:29:30 +020044
45 private static final String REMOVE_TRUNK_SUCCESS =
46 "Trunk mode removed for VLAN %s on device %s interface %s.";
47 private static final String REMOVE_TRUNK_FAILURE =
48 "Failed to remove trunk mode for VLAN %s on device %s interface %s.";
49
50 @Argument(index = 0, name = "uri", description = "Device ID",
51 required = true, multiValued = false)
52 private String uri = null;
53
54 @Argument(index = 1, name = "interface",
55 description = "Interface name",
56 required = true, multiValued = false)
57 private String portName = null;
58
59 @Argument(index = 2, name = "vlan",
60 description = "VLAN ID",
Andreas Papazois59e19bb2016-04-12 13:59:58 +030061 required = true, multiValued = true)
62 private String[] vlanStrings = null;
Andreas Papazois92e4a042016-02-24 15:29:30 +020063
64 @Option(name = "-t", aliases = "--trunk",
Andreas Papazois59e19bb2016-04-12 13:59:58 +030065 description = "Remove trunk mode for VLAN(s)",
Andreas Papazois92e4a042016-02-24 15:29:30 +020066 required = false, multiValued = false)
67 private boolean trunkMode = false;
68
69 @Override
70 protected void execute() {
71 DriverService service = get(DriverService.class);
72 DeviceId deviceId = DeviceId.deviceId(uri);
73 DriverHandler h = service.createHandler(deviceId);
74 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
75
Andreas Papazois59e19bb2016-04-12 13:59:58 +030076 List<VlanId> vlanIds = new ArrayList<>();
77 for (String vlanString : vlanStrings) {
78 vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
79 }
Andreas Papazois92e4a042016-02-24 15:29:30 +020080
81 if (trunkMode) {
82 // Trunk mode for VLAN to be removed.
Andreas Papazois59e19bb2016-04-12 13:59:58 +030083 if (interfaceConfig.removeTrunkInterface(deviceId, portName, vlanIds)) {
84 print(REMOVE_TRUNK_SUCCESS, vlanIds, deviceId, portName);
Andreas Papazois92e4a042016-02-24 15:29:30 +020085 } else {
Andreas Papazois59e19bb2016-04-12 13:59:58 +030086 print(REMOVE_TRUNK_FAILURE, vlanIds, deviceId, portName);
Andreas Papazois92e4a042016-02-24 15:29:30 +020087 }
88 return;
89 }
90
Andreas Papazois59e19bb2016-04-12 13:59:58 +030091 // Access mode for VLAN to be removed.
92 if (vlanIds.size() != 1) {
93 print(ONE_VLAN_ALLOWED, deviceId, portName);
94 return;
95 }
96 VlanId accessVlanId = vlanIds.get(0);
97 if (interfaceConfig.removeInterfaceFromVlan(deviceId, portName, accessVlanId)) {
98 print(REMOVE_VLAN_SUCCESS, accessVlanId, deviceId, portName);
Andreas Papazois92e4a042016-02-24 15:29:30 +020099 } else {
Andreas Papazois59e19bb2016-04-12 13:59:58 +0300100 print(REMOVE_VLAN_FAILURE, accessVlanId, deviceId, portName);
Andreas Papazois92e4a042016-02-24 15:29:30 +0200101 }
102 }
103
104}