Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.behaviour.InterfaceConfig; |
| 27 | import org.onosproject.net.driver.DriverHandler; |
| 28 | import org.onosproject.net.driver.DriverService; |
| 29 | |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 33 | /** |
| 34 | * Configures a device interface. |
| 35 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Service |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 37 | @Command(scope = "onos", name = "device-add-interface", |
| 38 | description = "Configures a device interface") |
| 39 | public class DeviceInterfaceAddCommand extends AbstractShellCommand { |
| 40 | |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 41 | private static final String ONE_ACTION_ALLOWED = |
| 42 | "One configuration action allowed at a time"; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 43 | private static final String CONFIG_VLAN_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 44 | "VLAN %s added on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 45 | private static final String CONFIG_VLAN_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 46 | "Failed to add VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 47 | private static final String CONFIG_TRUNK_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 48 | "Trunk mode added for VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 49 | private static final String CONFIG_TRUNK_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 50 | "Failed to add trunk mode for VLAN %s on device %s interface %s."; |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 51 | 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 Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 55 | |
| 56 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 57 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 58 | @Completion(DeviceIdCompleter.class) |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 59 | 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 Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 66 | @Option(name = "-r", aliases = "--rate-limit", |
| 67 | description = "Percentage for egress bandwidth limit", |
| 68 | required = false, multiValued = false) |
| 69 | private String limitString = null; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 70 | |
| 71 | @Option(name = "-t", aliases = "--trunk", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 72 | 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 Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 78 | required = false, multiValued = false) |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 79 | private String accessVlanString = null; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 80 | |
| 81 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 82 | protected void doExecute() { |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 83 | 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 Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 88 | 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 Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 116 | List<VlanId> vlanIds = new ArrayList<>(); |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 117 | for (String vlanString : trunkVlanStrings) { |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 118 | vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString))); |
| 119 | } |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 120 | if (config.addTrunkMode(portName, vlanIds)) { |
| 121 | print(CONFIG_TRUNK_SUCCESS, vlanIds, uri, portName); |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 122 | } else { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 123 | 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 Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | } |