blob: 8d9955fd37930dcc211d0a383d1272588b8e8c17 [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;
Jian Lic2538102018-07-03 22:42:07 +090020import org.onlab.packet.IpPrefix;
21import org.onlab.packet.TpPort;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstackvtap.api.OpenstackVtap;
24import org.onosproject.openstackvtap.api.OpenstackVtapAdminService;
25import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion;
26
Jian Li26ef1302018-07-04 14:37:06 +090027import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolTypeFromString;
28import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString;
29
Jian Lic2538102018-07-03 22:42:07 +090030/**
31 * Command line interface for adding openstack vTap rule.
32 */
33@Command(scope = "onos", name = "openstack-vtap-add",
34 description = "OpenstackVtap activate")
35public class OpenstackVtapAddCommand extends AbstractShellCommand {
36
Jian Li26ef1302018-07-04 14:37:06 +090037 private final OpenstackVtapAdminService vTapService =
38 get(OpenstackVtapAdminService.class);
Jian Lic2538102018-07-03 22:42:07 +090039
40 @Argument(index = 0, name = "srcIp",
Jian Li26ef1302018-07-04 14:37:06 +090041 description = "source IP address CIDR (e.g., \"10.1.0.2/32\")",
Jian Lic2538102018-07-03 22:42:07 +090042 required = true, multiValued = false)
43 String srcIp = "";
44
45 @Argument(index = 1, name = "dstIp",
Jian Li26ef1302018-07-04 14:37:06 +090046 description = "destination IP address CIDR (e.g., \"10.1.0.3/32\")",
Jian Lic2538102018-07-03 22:42:07 +090047 required = true, multiValued = false)
48 String dstIp = "";
49
50 @Argument(index = 2, name = "ipProto",
51 description = "IP protocol [tcp|udp|icmp|none]",
52 required = false, multiValued = false)
53 String ipProto = "";
54
55 @Argument(index = 3, name = "srcTpPort",
56 description = "source transport layer port (0 is skip)",
57 required = false, multiValued = false)
58 int srcTpPort = 0;
59
60 @Argument(index = 4, name = "dstTpPort",
61 description = "destination transport layer port (0 is skip)",
62 required = false, multiValued = false)
63 int dstTpPort = 0;
64
65 @Argument(index = 5, name = "type",
66 description = "vTap type [all|tx|rx]",
67 required = false, multiValued = false)
68 String vTapTypeStr = "all";
69
70 @Override
71 protected void execute() {
72 DefaultOpenstackVtapCriterion.Builder
73 defaultVtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder();
74 if (makeCriterion(defaultVtapCriterionBuilder)) {
Jian Li26ef1302018-07-04 14:37:06 +090075 OpenstackVtap.Type type = getVtapTypeFromString(vTapTypeStr);
Jian Lic2538102018-07-03 22:42:07 +090076 if (type == null) {
77 print("Invalid vTap type");
78 return;
79 }
80
81 OpenstackVtap vTap = vTapService.createVtap(type, defaultVtapCriterionBuilder.build());
82 if (vTap != null) {
83 print("Created OpenstackVtap with id { %s }", vTap.id().toString());
84 } else {
85 print("Failed to create OpenstackVtap");
86 }
87 }
88 }
89
Jian Lic2538102018-07-03 22:42:07 +090090 private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vTapCriterionBuilder) {
91 try {
92 vTapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp));
93 vTapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp));
94 } catch (Exception e) {
Jian Li311a9c92018-07-09 16:48:36 +090095 print("Inputted valid source IP & destination IP in CIDR (e.g., \"10.1.0.4/32\")");
Jian Lic2538102018-07-03 22:42:07 +090096 return false;
97 }
98
Jian Li26ef1302018-07-04 14:37:06 +090099 vTapCriterionBuilder.ipProtocol(getProtocolTypeFromString(ipProto.toLowerCase()));
Jian Lic2538102018-07-03 22:42:07 +0900100
101 vTapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort));
102 vTapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort));
103
104 return true;
105 }
Jian Lic2538102018-07-03 22:42:07 +0900106}