blob: e2a250332b1776944ee2dba358667ea9c6265377 [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
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 Papazois1dff77c2016-02-16 16:27:33 +020023import org.onlab.packet.VlanId;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.behaviour.InterfaceConfig;
27import org.onosproject.net.driver.DriverHandler;
28import org.onosproject.net.driver.DriverService;
29
Andreas Papazois59e19bb2016-04-12 13:59:58 +030030import java.util.ArrayList;
31import java.util.List;
32
Andreas Papazois1dff77c2016-02-16 16:27:33 +020033/**
34 * Configures a device interface.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
Andreas Papazois1dff77c2016-02-16 16:27:33 +020037@Command(scope = "onos", name = "device-add-interface",
38 description = "Configures a device interface")
39public class DeviceInterfaceAddCommand extends AbstractShellCommand {
40
Andreas Papazois34a82cf2016-04-27 09:09:13 +030041 private static final String ONE_ACTION_ALLOWED =
42 "One configuration action allowed at a time";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020043 private static final String CONFIG_VLAN_SUCCESS =
Andreas Papazois92e4a042016-02-24 15:29:30 +020044 "VLAN %s added on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020045 private static final String CONFIG_VLAN_FAILURE =
Andreas Papazois92e4a042016-02-24 15:29:30 +020046 "Failed to add VLAN %s on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020047 private static final String CONFIG_TRUNK_SUCCESS =
Andreas Papazois92e4a042016-02-24 15:29:30 +020048 "Trunk mode added for VLAN %s on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020049 private static final String CONFIG_TRUNK_FAILURE =
Andreas Papazois92e4a042016-02-24 15:29:30 +020050 "Failed to add trunk mode for VLAN %s on device %s interface %s.";
Andreas Papazois34a82cf2016-04-27 09:09:13 +030051 private static final String CONFIG_RATE_SUCCESS =
52 "Rate limit %d%% added on device %s interface %s.";
53 private static final String CONFIG_RATE_FAILURE =
54 "Failed to add rate limit %d%% on device %s interface %s.";
Andreas Papazois1dff77c2016-02-16 16:27:33 +020055
56 @Argument(index = 0, name = "uri", description = "Device ID",
57 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070058 @Completion(DeviceIdCompleter.class)
Andreas Papazois1dff77c2016-02-16 16:27:33 +020059 private String uri = null;
60
61 @Argument(index = 1, name = "interface",
62 description = "Interface name",
63 required = true, multiValued = false)
64 private String portName = null;
65
Andreas Papazois34a82cf2016-04-27 09:09:13 +030066 @Option(name = "-r", aliases = "--rate-limit",
67 description = "Percentage for egress bandwidth limit",
68 required = false, multiValued = false)
69 private String limitString = null;
Andreas Papazois1dff77c2016-02-16 16:27:33 +020070
71 @Option(name = "-t", aliases = "--trunk",
Andreas Papazois34a82cf2016-04-27 09:09:13 +030072 description = "VLAN(s) for trunk port (multiple values are allowed)",
73 required = false, multiValued = true)
74 private String[] trunkVlanStrings = null;
75
76 @Option(name = "-a", aliases = "--access",
77 description = "VLAN for access port",
Andreas Papazois1dff77c2016-02-16 16:27:33 +020078 required = false, multiValued = false)
Andreas Papazois34a82cf2016-04-27 09:09:13 +030079 private String accessVlanString = null;
Andreas Papazois1dff77c2016-02-16 16:27:33 +020080
81 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 protected void doExecute() {
Andreas Papazois1dff77c2016-02-16 16:27:33 +020083 DriverService service = get(DriverService.class);
84 DeviceId deviceId = DeviceId.deviceId(uri);
85 DriverHandler h = service.createHandler(deviceId);
86 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
87
Andreas Papazois34a82cf2016-04-27 09:09:13 +030088 if (accessVlanString != null && trunkVlanStrings == null &&
89 limitString == null) {
90 // Access mode to be enabled for VLAN.
91 addAccessModeToIntf(interfaceConfig);
92 } else if (trunkVlanStrings != null && accessVlanString == null &&
93 limitString == null) {
94 // Trunk mode to be enabled for VLANs.
95 addTrunkModeToIntf(interfaceConfig);
96 } else if (limitString != null && accessVlanString == null &&
97 trunkVlanStrings == null) {
98 // Rate limit to be set on interface.
99 addRateLimitToIntf(interfaceConfig);
100 } else {
101 // Option has not been correctly set.
102 print(ONE_ACTION_ALLOWED);
103 }
104 }
105
106 private void addRateLimitToIntf(InterfaceConfig config) {
107 short rate = Short.parseShort(limitString);
108 if (config.addRateLimit(portName, rate)) {
109 print(CONFIG_RATE_SUCCESS, rate, uri, portName);
110 } else {
111 print(CONFIG_RATE_FAILURE, rate, uri, portName);
112 }
113 }
114
115 private void addTrunkModeToIntf(InterfaceConfig config) {
Andreas Papazois59e19bb2016-04-12 13:59:58 +0300116 List<VlanId> vlanIds = new ArrayList<>();
Andreas Papazois34a82cf2016-04-27 09:09:13 +0300117 for (String vlanString : trunkVlanStrings) {
Andreas Papazois59e19bb2016-04-12 13:59:58 +0300118 vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
119 }
Andreas Papazois34a82cf2016-04-27 09:09:13 +0300120 if (config.addTrunkMode(portName, vlanIds)) {
121 print(CONFIG_TRUNK_SUCCESS, vlanIds, uri, portName);
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200122 } else {
Andreas Papazois34a82cf2016-04-27 09:09:13 +0300123 print(CONFIG_TRUNK_FAILURE, vlanIds, uri, portName);
124 }
125 }
126
127 private void addAccessModeToIntf(InterfaceConfig config) {
128 VlanId accessVlanId = VlanId.vlanId(Short.parseShort(accessVlanString));
129 if (config.addAccessMode(portName, accessVlanId)) {
130 print(CONFIG_VLAN_SUCCESS, accessVlanId, uri, portName);
131 } else {
132 print(CONFIG_VLAN_FAILURE, accessVlanId, uri, portName);
Andreas Papazois1dff77c2016-02-16 16:27:33 +0200133 }
134 }
135
136}