Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 38 | private static final String CONFIG_VLAN_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 39 | "VLAN %s added on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 40 | private static final String CONFIG_VLAN_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 41 | "Failed to add VLAN %s on device %s interface %s."; |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 42 | private static final String ONE_VLAN_ALLOWED = |
| 43 | "Only one VLAN allowed for access mode on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 44 | |
| 45 | private static final String CONFIG_TRUNK_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 46 | "Trunk mode added for 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_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 48 | "Failed to add trunk mode for VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 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 Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 61 | required = true, multiValued = true) |
| 62 | private String[] vlanStrings = null; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 63 | |
| 64 | @Option(name = "-t", aliases = "--trunk", |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 65 | description = "Configure interface as trunk for VLAN(s)", |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 66 | 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 Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 76 | List<VlanId> vlanIds = new ArrayList<>(); |
| 77 | for (String vlanString : vlanStrings) { |
| 78 | vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString))); |
| 79 | } |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 80 | |
| 81 | if (trunkMode) { |
| 82 | // Trunk mode to be enabled for VLAN. |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 83 | if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanIds)) { |
| 84 | print(CONFIG_TRUNK_SUCCESS, vlanIds, deviceId, portName); |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 85 | } else { |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 86 | print(CONFIG_TRUNK_FAILURE, vlanIds, deviceId, portName); |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 87 | } |
| 88 | return; |
| 89 | } |
| 90 | |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 91 | // Access mode to be enabled for VLAN. |
| 92 | if (vlanIds.size() != 1) { |
| 93 | print(ONE_VLAN_ALLOWED, deviceId, portName); |
| 94 | return; |
| 95 | } |
| 96 | VlanId accessVlanId = vlanIds.get(0); |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame^] | 97 | if (interfaceConfig.addAccessInterface(deviceId, portName, accessVlanId)) { |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 98 | print(CONFIG_VLAN_SUCCESS, accessVlanId, deviceId, portName); |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 99 | } else { |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 100 | print(CONFIG_VLAN_FAILURE, accessVlanId, deviceId, portName); |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | } |