blob: 013ac89e5448a41d20ab48ba1657206b2e59693d [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;
Jian Lidb521c12018-11-19 17:42:35 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Lic2538102018-07-03 22:42:07 +090022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.TpPort;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.openstackvtap.api.OpenstackVtap;
26import org.onosproject.openstackvtap.api.OpenstackVtapAdminService;
27import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion;
28
Jian Li26ef1302018-07-04 14:37:06 +090029import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolTypeFromString;
30import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString;
31
Jian Lic2538102018-07-03 22:42:07 +090032/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090033 * Adds a openstack vtap rule.
Jian Lic2538102018-07-03 22:42:07 +090034 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070035@Service
Jian Lic2538102018-07-03 22:42:07 +090036@Command(scope = "onos", name = "openstack-vtap-add",
37 description = "OpenstackVtap activate")
38public class OpenstackVtapAddCommand extends AbstractShellCommand {
39
Jimo Jung14e87bf2018-09-03 16:28:13 +090040 private final OpenstackVtapAdminService vtapService =
Jian Li26ef1302018-07-04 14:37:06 +090041 get(OpenstackVtapAdminService.class);
Jian Lic2538102018-07-03 22:42:07 +090042
43 @Argument(index = 0, name = "srcIp",
Jian Li26ef1302018-07-04 14:37:06 +090044 description = "source IP address CIDR (e.g., \"10.1.0.2/32\")",
Jian Lic2538102018-07-03 22:42:07 +090045 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090046 @Completion(VmIpCompleter.class)
Jian Lic2538102018-07-03 22:42:07 +090047 String srcIp = "";
48
49 @Argument(index = 1, name = "dstIp",
Jian Li26ef1302018-07-04 14:37:06 +090050 description = "destination IP address CIDR (e.g., \"10.1.0.3/32\")",
Jian Lic2538102018-07-03 22:42:07 +090051 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090052 @Completion(VmIpCompleter.class)
Jian Lic2538102018-07-03 22:42:07 +090053 String dstIp = "";
54
55 @Argument(index = 2, name = "ipProto",
Jimo Jung14e87bf2018-09-03 16:28:13 +090056 description = "IP protocol [any|tcp|udp|icmp]",
Jian Lic2538102018-07-03 22:42:07 +090057 required = false, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090058 @Completion(ProtocolTypeCompleter.class)
Jimo Jung14e87bf2018-09-03 16:28:13 +090059 String ipProto = "any";
Jian Lic2538102018-07-03 22:42:07 +090060
61 @Argument(index = 3, name = "srcTpPort",
62 description = "source transport layer port (0 is skip)",
63 required = false, multiValued = false)
64 int srcTpPort = 0;
65
66 @Argument(index = 4, name = "dstTpPort",
67 description = "destination transport layer port (0 is skip)",
68 required = false, multiValued = false)
69 int dstTpPort = 0;
70
71 @Argument(index = 5, name = "type",
Jimo Jung14e87bf2018-09-03 16:28:13 +090072 description = "vtap type [all|rx|tx]",
Jian Lic2538102018-07-03 22:42:07 +090073 required = false, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090074 @Completion(VtapTypeCompleter.class)
Jimo Jung14e87bf2018-09-03 16:28:13 +090075 String vtapTypeStr = "all";
Jian Lic2538102018-07-03 22:42:07 +090076
77 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070078 protected void doExecute() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090079 DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder();
80 if (makeCriterion(vtapCriterionBuilder)) {
81 OpenstackVtap.Type type = getVtapTypeFromString(vtapTypeStr);
Ray Milkeyd5425682018-10-23 10:21:33 -070082
Jian Lic2538102018-07-03 22:42:07 +090083 if (type == null) {
Jimo Jung14e87bf2018-09-03 16:28:13 +090084 print("Invalid vtap type");
Jian Lic2538102018-07-03 22:42:07 +090085 return;
86 }
87
Jimo Jung14e87bf2018-09-03 16:28:13 +090088 OpenstackVtap vtap = vtapService.createVtap(type, vtapCriterionBuilder.build());
89 if (vtap != null) {
90 print("Created OpenstackVtap with id { %s }", vtap.id().toString());
Jian Lic2538102018-07-03 22:42:07 +090091 } else {
92 print("Failed to create OpenstackVtap");
93 }
94 }
95 }
96
Jimo Jung14e87bf2018-09-03 16:28:13 +090097 private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder) {
Jian Lic2538102018-07-03 22:42:07 +090098 try {
Jimo Jung14e87bf2018-09-03 16:28:13 +090099 vtapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp));
100 vtapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp));
Jian Lic2538102018-07-03 22:42:07 +0900101 } catch (Exception e) {
Jian Li311a9c92018-07-09 16:48:36 +0900102 print("Inputted valid source IP & destination IP in CIDR (e.g., \"10.1.0.4/32\")");
Jian Lic2538102018-07-03 22:42:07 +0900103 return false;
104 }
105
Jimo Jung14e87bf2018-09-03 16:28:13 +0900106 vtapCriterionBuilder.ipProtocol(getProtocolTypeFromString(ipProto.toLowerCase()));
Jian Lic2538102018-07-03 22:42:07 +0900107
Jimo Jung14e87bf2018-09-03 16:28:13 +0900108 vtapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort));
109 vtapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort));
Jian Lic2538102018-07-03 22:42:07 +0900110
111 return true;
112 }
Jian Lic2538102018-07-03 22:42:07 +0900113}