blob: 61049ae971ffad37cf81f4c25f3d262dc81ae638 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Lic2538102018-07-03 22:42:07 +090021import org.onlab.packet.IpPrefix;
22import org.onlab.packet.TpPort;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstackvtap.api.OpenstackVtap;
25import org.onosproject.openstackvtap.api.OpenstackVtapAdminService;
26import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion;
27
Jian Li26ef1302018-07-04 14:37:06 +090028import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolTypeFromString;
29import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString;
30
Jian Lic2538102018-07-03 22:42:07 +090031/**
32 * Command line interface for adding openstack vTap rule.
33 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070034@Service
Jian Lic2538102018-07-03 22:42:07 +090035@Command(scope = "onos", name = "openstack-vtap-add",
36 description = "OpenstackVtap activate")
37public class OpenstackVtapAddCommand extends AbstractShellCommand {
38
Jian Li26ef1302018-07-04 14:37:06 +090039 private final OpenstackVtapAdminService vTapService =
40 get(OpenstackVtapAdminService.class);
Jian Lic2538102018-07-03 22:42:07 +090041
42 @Argument(index = 0, name = "srcIp",
Jian Li26ef1302018-07-04 14:37:06 +090043 description = "source IP address CIDR (e.g., \"10.1.0.2/32\")",
Jian Lic2538102018-07-03 22:42:07 +090044 required = true, multiValued = false)
45 String srcIp = "";
46
47 @Argument(index = 1, name = "dstIp",
Jian Li26ef1302018-07-04 14:37:06 +090048 description = "destination IP address CIDR (e.g., \"10.1.0.3/32\")",
Jian Lic2538102018-07-03 22:42:07 +090049 required = true, multiValued = false)
50 String dstIp = "";
51
52 @Argument(index = 2, name = "ipProto",
53 description = "IP protocol [tcp|udp|icmp|none]",
54 required = false, multiValued = false)
55 String ipProto = "";
56
57 @Argument(index = 3, name = "srcTpPort",
58 description = "source transport layer port (0 is skip)",
59 required = false, multiValued = false)
60 int srcTpPort = 0;
61
62 @Argument(index = 4, name = "dstTpPort",
63 description = "destination transport layer port (0 is skip)",
64 required = false, multiValued = false)
65 int dstTpPort = 0;
66
67 @Argument(index = 5, name = "type",
68 description = "vTap type [all|tx|rx]",
69 required = false, multiValued = false)
70 String vTapTypeStr = "all";
71
72 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070073 protected void doExecute() {
Jian Lic2538102018-07-03 22:42:07 +090074 DefaultOpenstackVtapCriterion.Builder
75 defaultVtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder();
76 if (makeCriterion(defaultVtapCriterionBuilder)) {
Jian Li26ef1302018-07-04 14:37:06 +090077 OpenstackVtap.Type type = getVtapTypeFromString(vTapTypeStr);
Jian Lic2538102018-07-03 22:42:07 +090078 if (type == null) {
79 print("Invalid vTap type");
80 return;
81 }
82
83 OpenstackVtap vTap = vTapService.createVtap(type, defaultVtapCriterionBuilder.build());
84 if (vTap != null) {
85 print("Created OpenstackVtap with id { %s }", vTap.id().toString());
86 } else {
87 print("Failed to create OpenstackVtap");
88 }
89 }
90 }
91
Jian Lic2538102018-07-03 22:42:07 +090092 private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vTapCriterionBuilder) {
93 try {
94 vTapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp));
95 vTapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp));
96 } catch (Exception e) {
Jian Li311a9c92018-07-09 16:48:36 +090097 print("Inputted valid source IP & destination IP in CIDR (e.g., \"10.1.0.4/32\")");
Jian Lic2538102018-07-03 22:42:07 +090098 return false;
99 }
100
Jian Li26ef1302018-07-04 14:37:06 +0900101 vTapCriterionBuilder.ipProtocol(getProtocolTypeFromString(ipProto.toLowerCase()));
Jian Lic2538102018-07-03 22:42:07 +0900102
103 vTapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort));
104 vTapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort));
105
106 return true;
107 }
Jian Lic2538102018-07-03 22:42:07 +0900108}