blob: 9480346fcf18f4a232ea3de23b935d827b679f5c [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/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090032 * Adds a openstack vtap rule.
Jian Lic2538102018-07-03 22:42:07 +090033 */
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
Jimo Jung14e87bf2018-09-03 16:28:13 +090039 private final OpenstackVtapAdminService vtapService =
Jian Li26ef1302018-07-04 14:37:06 +090040 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",
Jimo Jung14e87bf2018-09-03 16:28:13 +090053 description = "IP protocol [any|tcp|udp|icmp]",
Jian Lic2538102018-07-03 22:42:07 +090054 required = false, multiValued = false)
Jimo Jung14e87bf2018-09-03 16:28:13 +090055 String ipProto = "any";
Jian Lic2538102018-07-03 22:42:07 +090056
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",
Jimo Jung14e87bf2018-09-03 16:28:13 +090068 description = "vtap type [all|rx|tx]",
Jian Lic2538102018-07-03 22:42:07 +090069 required = false, multiValued = false)
Jimo Jung14e87bf2018-09-03 16:28:13 +090070 String vtapTypeStr = "all";
Jian Lic2538102018-07-03 22:42:07 +090071
72 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070073 protected void doExecute() {
Jimo Jung14e87bf2018-09-03 16:28:13 +090074 DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder();
75 if (makeCriterion(vtapCriterionBuilder)) {
76 OpenstackVtap.Type type = getVtapTypeFromString(vtapTypeStr);
Ray Milkeyd5425682018-10-23 10:21:33 -070077
Jian Lic2538102018-07-03 22:42:07 +090078 if (type == null) {
Jimo Jung14e87bf2018-09-03 16:28:13 +090079 print("Invalid vtap type");
Jian Lic2538102018-07-03 22:42:07 +090080 return;
81 }
82
Jimo Jung14e87bf2018-09-03 16:28:13 +090083 OpenstackVtap vtap = vtapService.createVtap(type, vtapCriterionBuilder.build());
84 if (vtap != null) {
85 print("Created OpenstackVtap with id { %s }", vtap.id().toString());
Jian Lic2538102018-07-03 22:42:07 +090086 } else {
87 print("Failed to create OpenstackVtap");
88 }
89 }
90 }
91
Jimo Jung14e87bf2018-09-03 16:28:13 +090092 private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder) {
Jian Lic2538102018-07-03 22:42:07 +090093 try {
Jimo Jung14e87bf2018-09-03 16:28:13 +090094 vtapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp));
95 vtapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp));
Jian Lic2538102018-07-03 22:42:07 +090096 } 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
Jimo Jung14e87bf2018-09-03 16:28:13 +0900101 vtapCriterionBuilder.ipProtocol(getProtocolTypeFromString(ipProto.toLowerCase()));
Jian Lic2538102018-07-03 22:42:07 +0900102
Jimo Jung14e87bf2018-09-03 16:28:13 +0900103 vtapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort));
104 vtapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort));
Jian Lic2538102018-07-03 22:42:07 +0900105
106 return true;
107 }
Jian Lic2538102018-07-03 22:42:07 +0900108}