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 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.apache.karaf.shell.commands.Option; |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.behaviour.InterfaceConfig; |
| 25 | import org.onosproject.net.driver.DriverHandler; |
| 26 | import org.onosproject.net.driver.DriverService; |
| 27 | |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.List; |
| 30 | |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 31 | /** |
| 32 | * Configures a device interface. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "device-add-interface", |
| 35 | description = "Configures a device interface") |
| 36 | public class DeviceInterfaceAddCommand extends AbstractShellCommand { |
| 37 | |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 38 | private static final String ONE_ACTION_ALLOWED = |
| 39 | "One configuration action allowed at a time"; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 40 | private static final String CONFIG_VLAN_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 41 | "VLAN %s added on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 42 | private static final String CONFIG_VLAN_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 43 | "Failed to add VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 44 | private static final String CONFIG_TRUNK_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 45 | "Trunk mode added for VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 46 | private static final String CONFIG_TRUNK_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 47 | "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] | 48 | 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 Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 52 | |
| 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 Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 62 | @Option(name = "-r", aliases = "--rate-limit", |
| 63 | description = "Percentage for egress bandwidth limit", |
| 64 | required = false, multiValued = false) |
| 65 | private String limitString = null; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 66 | |
| 67 | @Option(name = "-t", aliases = "--trunk", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 68 | 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 Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 74 | required = false, multiValued = false) |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 75 | private String accessVlanString = null; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 76 | |
| 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 Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 84 | 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 Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 112 | List<VlanId> vlanIds = new ArrayList<>(); |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 113 | for (String vlanString : trunkVlanStrings) { |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 114 | vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString))); |
| 115 | } |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 116 | if (config.addTrunkMode(portName, vlanIds)) { |
| 117 | print(CONFIG_TRUNK_SUCCESS, vlanIds, uri, portName); |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 118 | } else { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame] | 119 | 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 Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | } |