Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstackvtap.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.openstackvtap.api.OpenstackVtap; |
| 27 | import org.onosproject.openstackvtap.api.OpenstackVtapAdminService; |
| 28 | |
| 29 | import static org.onlab.packet.VlanId.UNTAGGED; |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame^] | 30 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Command line interface for set openstack vTap output. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "openstack-vtap-output", |
| 36 | description = "OpenstackVtap output setup") |
| 37 | public class OpenstackVtapOutputCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private final DeviceService deviceService = get(DeviceService.class); |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame^] | 40 | private final OpenstackVtapAdminService vTapAdminService = |
| 41 | get(OpenstackVtapAdminService.class); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 42 | |
| 43 | @Argument(index = 0, name = "deviceId", description = "device id", |
| 44 | required = true, multiValued = false) |
| 45 | String id = ""; |
| 46 | |
| 47 | @Argument(index = 1, name = "port", description = "output port number", |
| 48 | required = true, multiValued = false) |
| 49 | int port = 0; |
| 50 | |
| 51 | @Argument(index = 2, name = "vlan", description = "vlan id", |
| 52 | required = false, multiValued = false) |
| 53 | int vlan = UNTAGGED; |
| 54 | |
| 55 | @Argument(index = 3, name = "type", description = "vTap type [all|tx|rx]", |
| 56 | required = false, multiValued = false) |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame^] | 57 | String vTapTypeStr = "all"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | protected void execute() { |
| 61 | try { |
| 62 | Device device = deviceService.getDevice(DeviceId.deviceId(id)); |
| 63 | if (device != null) { |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame^] | 64 | OpenstackVtap.Type type = getVtapTypeFromString(vTapTypeStr); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 65 | |
| 66 | vTapAdminService.setVtapOutput(device.id(), type, |
| 67 | PortNumber.portNumber(port), VlanId.vlanId((short) vlan)); |
| 68 | print("Set OpenstackVtap output deviceId { %s }, port=%s, vlan=%s", |
| 69 | device.id().toString(), |
| 70 | PortNumber.portNumber(port).toString(), |
| 71 | VlanId.vlanId((short) vlan).toString()); |
| 72 | } else { |
| 73 | print("Invalid device id"); |
| 74 | } |
| 75 | } catch (Exception e) { |
| 76 | print("Invalid parameter: %s", e.toString()); |
| 77 | } |
| 78 | } |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 79 | } |