blob: 527208386983cdc2a7e7bea045ed94146a9e4e75 [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/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090031 * Adds a openstack vtap rule.
Jian Lic2538102018-07-03 22:42:07 +090032 */
33@Command(scope = "onos", name = "openstack-vtap-add",
34 description = "OpenstackVtap activate")
35public class OpenstackVtapAddCommand extends AbstractShellCommand {
36
Jimo Jung14e87bf2018-09-03 16:28:13 +090037 private final OpenstackVtapAdminService vtapService =
Jian Li26ef1302018-07-04 14:37:06 +090038 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",
Jimo Jung14e87bf2018-09-03 16:28:13 +090051 description = "IP protocol [any|tcp|udp|icmp]",
Jian Lic2538102018-07-03 22:42:07 +090052 required = false, multiValued = false)
Jimo Jung14e87bf2018-09-03 16:28:13 +090053 String ipProto = "any";
Jian Lic2538102018-07-03 22:42:07 +090054
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",
Jimo Jung14e87bf2018-09-03 16:28:13 +090066 description = "vtap type [all|rx|tx]",
Jian Lic2538102018-07-03 22:42:07 +090067 required = false, multiValued = false)
Jimo Jung14e87bf2018-09-03 16:28:13 +090068 String vtapTypeStr = "all";
Jian Lic2538102018-07-03 22:42:07 +090069
70 @Override
71 protected void execute() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090072 DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder();
73 if (makeCriterion(vtapCriterionBuilder)) {
74 OpenstackVtap.Type type = getVtapTypeFromString(vtapTypeStr);
Jian Lic2538102018-07-03 22:42:07 +090075 if (type == null) {
Jimo Jung14e87bf2018-09-03 16:28:13 +090076 print("Invalid vtap type");
Jian Lic2538102018-07-03 22:42:07 +090077 return;
78 }
79
Jimo Jung14e87bf2018-09-03 16:28:13 +090080 OpenstackVtap vtap = vtapService.createVtap(type, vtapCriterionBuilder.build());
81 if (vtap != null) {
82 print("Created OpenstackVtap with id { %s }", vtap.id().toString());
Jian Lic2538102018-07-03 22:42:07 +090083 } else {
84 print("Failed to create OpenstackVtap");
85 }
86 }
87 }
88
Jimo Jung14e87bf2018-09-03 16:28:13 +090089 private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder) {
Jian Lic2538102018-07-03 22:42:07 +090090 try {
Jimo Jung14e87bf2018-09-03 16:28:13 +090091 vtapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp));
92 vtapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp));
Jian Lic2538102018-07-03 22:42:07 +090093 } catch (Exception e) {
Jian Li311a9c92018-07-09 16:48:36 +090094 print("Inputted valid source IP & destination IP in CIDR (e.g., \"10.1.0.4/32\")");
Jian Lic2538102018-07-03 22:42:07 +090095 return false;
96 }
97
Jimo Jung14e87bf2018-09-03 16:28:13 +090098 vtapCriterionBuilder.ipProtocol(getProtocolTypeFromString(ipProto.toLowerCase()));
Jian Lic2538102018-07-03 22:42:07 +090099
Jimo Jung14e87bf2018-09-03 16:28:13 +0900100 vtapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort));
101 vtapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort));
Jian Lic2538102018-07-03 22:42:07 +0900102
103 return true;
104 }
Jian Lic2538102018-07-03 22:42:07 +0900105}