Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | |
| 28 | /** |
| 29 | * Configures a device interface. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "device-add-interface", |
| 32 | description = "Configures a device interface") |
| 33 | public class DeviceInterfaceAddCommand extends AbstractShellCommand { |
| 34 | |
| 35 | private static final String CONFIG_VLAN_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 36 | "VLAN %s added on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 37 | private static final String CONFIG_VLAN_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 38 | "Failed to add VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 39 | |
| 40 | private static final String CONFIG_TRUNK_SUCCESS = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 41 | "Trunk mode added for VLAN %s on device %s interface %s."; |
Andreas Papazois | 1dff77c | 2016-02-16 16:27:33 +0200 | [diff] [blame] | 42 | private static final String CONFIG_TRUNK_FAILURE = |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 43 | "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] | 44 | |
| 45 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 46 | required = true, multiValued = false) |
| 47 | private String uri = null; |
| 48 | |
| 49 | @Argument(index = 1, name = "interface", |
| 50 | description = "Interface name", |
| 51 | required = true, multiValued = false) |
| 52 | private String portName = null; |
| 53 | |
| 54 | @Argument(index = 2, name = "vlan", |
| 55 | description = "VLAN ID", |
| 56 | required = true, multiValued = false) |
| 57 | private String vlanString = null; |
| 58 | |
| 59 | @Option(name = "-t", aliases = "--trunk", |
| 60 | description = "Configure interface as trunk for VLAN", |
| 61 | required = false, multiValued = false) |
| 62 | private boolean trunkMode = false; |
| 63 | |
| 64 | @Override |
| 65 | protected void execute() { |
| 66 | DriverService service = get(DriverService.class); |
| 67 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 68 | DriverHandler h = service.createHandler(deviceId); |
| 69 | InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); |
| 70 | |
| 71 | VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString)); |
| 72 | |
| 73 | if (trunkMode) { |
| 74 | // Trunk mode to be enabled for VLAN. |
| 75 | if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanId)) { |
| 76 | print(CONFIG_TRUNK_SUCCESS, vlanId, deviceId, portName); |
| 77 | } else { |
| 78 | print(CONFIG_TRUNK_FAILURE, vlanId, deviceId, portName); |
| 79 | } |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // VLAN to be added to interface. |
| 84 | if (interfaceConfig.addInterfaceToVlan(deviceId, portName, vlanId)) { |
| 85 | print(CONFIG_VLAN_SUCCESS, vlanId, deviceId, portName); |
| 86 | } else { |
| 87 | print(CONFIG_VLAN_FAILURE, vlanId, deviceId, portName); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | } |