blob: c28e190108195c14daae39b895fa8790cf7f240a [file] [log] [blame]
Andreas Papazois1dff77c2016-02-16 16:27:33 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andreas Papazois1dff77c2016-02-16 16:27:33 +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 Papazois1dff77c2016-02-16 16:27:33 +020031/**
32 * Configures a device interface.
33 */
34@Command(scope = "onos", name = "device-add-interface",
35 description = "Configures a device interface")
36public class DeviceInterfaceAddCommand extends AbstractShellCommand {
37
Andreas Papazois34a82cf2016-04-27 09:09:13 +030038 private static final String ONE_ACTION_ALLOWED =
39 "One configuration action allowed at a time";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020040 private static final String CONFIG_VLAN_SUCCESS =
Andreas Papazois92e4a042016-02-24 15:29:30 +020041 "VLAN %s added on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020042 private static final String CONFIG_VLAN_FAILURE =
Andreas Papazois92e4a042016-02-24 15:29:30 +020043 "Failed to add VLAN %s on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020044 private static final String CONFIG_TRUNK_SUCCESS =
Andreas Papazois92e4a042016-02-24 15:29:30 +020045 "Trunk mode added for VLAN %s on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020046 private static final String CONFIG_TRUNK_FAILURE =
Andreas Papazois92e4a042016-02-24 15:29:30 +020047 "Failed to add trunk mode for VLAN %s on device %s interface %s.";
Andreas Papazois34a82cf2016-04-27 09:09:13 +030048 private static final String CONFIG_RATE_SUCCESS =
49 "Rate limit %d%% added on device %s interface %s.";
50 private static final String CONFIG_RATE_FAILURE =
51 "Failed to add rate limit %d%% on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020052
53 @Argument(index = 0, name = "uri", description = "Device ID",
54 required = true, multiValued = false)
55 private String uri = null;
56
57 @Argument(index = 1, name = "interface",
58 description = "Interface name",
59 required = true, multiValued = false)
60 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 String limitString = null;
Andreas Papazois1dff77c2016-02-16 16:27:33 +020066
67 @Option(name = "-t", aliases = "--trunk",
Andreas Papazois34a82cf2016-04-27 09:09:13 +030068 description = "VLAN(s) for trunk port (multiple values are allowed)",
69 required = false, multiValued = true)
70 private String[] trunkVlanStrings = null;
71
72 @Option(name = "-a", aliases = "--access",
73 description = "VLAN for access port",
Andreas Papazois1dff77c2016-02-16 16:27:33 +020074 required = false, multiValued = false)
Andreas Papazois34a82cf2016-04-27 09:09:13 +030075 private String accessVlanString = null;
Andreas Papazois1dff77c2016-02-16 16:27:33 +020076
77 @Override
78 protected void execute() {
79 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 (accessVlanString != null && trunkVlanStrings == null &&
85 limitString == null) {
86 // Access mode to be enabled for VLAN.
87 addAccessModeToIntf(interfaceConfig);
88 } else if (trunkVlanStrings != null && accessVlanString == null &&
89 limitString == null) {
90 // Trunk mode to be enabled for VLANs.
91 addTrunkModeToIntf(interfaceConfig);
92 } else if (limitString != null && accessVlanString == null &&
93 trunkVlanStrings == null) {
94 // Rate limit to be set on interface.
95 addRateLimitToIntf(interfaceConfig);
96 } else {
97 // Option has not been correctly set.
98 print(ONE_ACTION_ALLOWED);
99 }
100 }
101
102 private void addRateLimitToIntf(InterfaceConfig config) {
103 short rate = Short.parseShort(limitString);
104 if (config.addRateLimit(portName, rate)) {
105 print(CONFIG_RATE_SUCCESS, rate, uri, portName);
106 } else {
107 print(CONFIG_RATE_FAILURE, rate, uri, portName);
108 }
109 }
110
111 private void addTrunkModeToIntf(InterfaceConfig config) {
Andreas Papazois59e19bb2016-04-12 13:59:58 +0300112 List<VlanId> vlanIds = new ArrayList<>();
Andreas Papazois34a82cf2016-04-27 09:09:13 +0300113 for (String vlanString : trunkVlanStrings) {
Andreas Papazois59e19bb2016-04-12 13:59:58 +0300114 vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
115 }
Andreas Papazois34a82cf2016-04-27 09:09:13 +0300116 if (config.addTrunkMode(portName, vlanIds)) {
117 print(CONFIG_TRUNK_SUCCESS, vlanIds, uri, portName);
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200118 } else {
Andreas Papazois34a82cf2016-04-27 09:09:13 +0300119 print(CONFIG_TRUNK_FAILURE, vlanIds, uri, portName);
120 }
121 }
122
123 private void addAccessModeToIntf(InterfaceConfig config) {
124 VlanId accessVlanId = VlanId.vlanId(Short.parseShort(accessVlanString));
125 if (config.addAccessMode(portName, accessVlanId)) {
126 print(CONFIG_VLAN_SUCCESS, accessVlanId, uri, portName);
127 } else {
128 print(CONFIG_VLAN_FAILURE, accessVlanId, uri, portName);
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200129 }
130 }
131
132}