blob: 293b62e603760407e690a41fb4468ddae4b5a50f [file] [log] [blame]
Jian Lic2538102018-07-03 22:42:07 +09001/*
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 */
16package org.onosproject.openstackvtap.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.packet.VlanId;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.openstackvtap.api.OpenstackVtap;
27import org.onosproject.openstackvtap.api.OpenstackVtapAdminService;
28
29import static org.onlab.packet.VlanId.UNTAGGED;
30
31/**
32 * Command line interface for set openstack vTap output.
33 */
34@Command(scope = "onos", name = "openstack-vtap-output",
35 description = "OpenstackVtap output setup")
36public class OpenstackVtapOutputCommand extends AbstractShellCommand {
37
38 private final DeviceService deviceService = get(DeviceService.class);
39 private final OpenstackVtapAdminService vTapAdminService = get(OpenstackVtapAdminService.class);
40
41 @Argument(index = 0, name = "deviceId", description = "device id",
42 required = true, multiValued = false)
43 String id = "";
44
45 @Argument(index = 1, name = "port", description = "output port number",
46 required = true, multiValued = false)
47 int port = 0;
48
49 @Argument(index = 2, name = "vlan", description = "vlan id",
50 required = false, multiValued = false)
51 int vlan = UNTAGGED;
52
53 @Argument(index = 3, name = "type", description = "vTap type [all|tx|rx]",
54 required = false, multiValued = false)
55 String vTapType = "all";
56
57 @Override
58 protected void execute() {
59 try {
60 Device device = deviceService.getDevice(DeviceId.deviceId(id));
61 if (device != null) {
62 OpenstackVtap.Type type = getVtapType(vTapType);
63 if (type == null) {
64 print("Invalid vTap type");
65 return;
66 }
67
68 vTapAdminService.setVtapOutput(device.id(), type,
69 PortNumber.portNumber(port), VlanId.vlanId((short) vlan));
70 print("Set OpenstackVtap output deviceId { %s }, port=%s, vlan=%s",
71 device.id().toString(),
72 PortNumber.portNumber(port).toString(),
73 VlanId.vlanId((short) vlan).toString());
74 } else {
75 print("Invalid device id");
76 }
77 } catch (Exception e) {
78 print("Invalid parameter: %s", e.toString());
79 }
80 }
81
82 private static OpenstackVtap.Type getVtapType(String str) {
83 switch (str) {
84 case "all":
85 return OpenstackVtap.Type.VTAP_ALL;
86 case "tx":
87 return OpenstackVtap.Type.VTAP_TX;
88 case "rx":
89 return OpenstackVtap.Type.VTAP_RX;
90 default:
91 return OpenstackVtap.Type.VTAP_NONE;
92 }
93 }
94}