Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstackvtap.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.TpPort; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.openstackvtap.api.OpenstackVtap; |
| 24 | import org.onosproject.openstackvtap.api.OpenstackVtapAdminService; |
| 25 | import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion; |
| 26 | |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 27 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getProtocolTypeFromString; |
| 28 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString; |
| 29 | |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 30 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 31 | * Adds a openstack vtap rule. |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 32 | */ |
| 33 | @Command(scope = "onos", name = "openstack-vtap-add", |
| 34 | description = "OpenstackVtap activate") |
| 35 | public class OpenstackVtapAddCommand extends AbstractShellCommand { |
| 36 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 37 | private final OpenstackVtapAdminService vtapService = |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 38 | get(OpenstackVtapAdminService.class); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 39 | |
| 40 | @Argument(index = 0, name = "srcIp", |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 41 | description = "source IP address CIDR (e.g., \"10.1.0.2/32\")", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 42 | required = true, multiValued = false) |
| 43 | String srcIp = ""; |
| 44 | |
| 45 | @Argument(index = 1, name = "dstIp", |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 46 | description = "destination IP address CIDR (e.g., \"10.1.0.3/32\")", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 47 | required = true, multiValued = false) |
| 48 | String dstIp = ""; |
| 49 | |
| 50 | @Argument(index = 2, name = "ipProto", |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 51 | description = "IP protocol [any|tcp|udp|icmp]", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 52 | required = false, multiValued = false) |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 53 | String ipProto = "any"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 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", |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 66 | description = "vtap type [all|rx|tx]", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 67 | required = false, multiValued = false) |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 68 | String vtapTypeStr = "all"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 69 | |
| 70 | @Override |
| 71 | protected void execute() { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 72 | DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder(); |
| 73 | if (makeCriterion(vtapCriterionBuilder)) { |
| 74 | OpenstackVtap.Type type = getVtapTypeFromString(vtapTypeStr); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 75 | if (type == null) { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 76 | print("Invalid vtap type"); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 80 | OpenstackVtap vtap = vtapService.createVtap(type, vtapCriterionBuilder.build()); |
| 81 | if (vtap != null) { |
| 82 | print("Created OpenstackVtap with id { %s }", vtap.id().toString()); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 83 | } else { |
| 84 | print("Failed to create OpenstackVtap"); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 89 | private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vtapCriterionBuilder) { |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 90 | try { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 91 | vtapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp)); |
| 92 | vtapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp)); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 93 | } catch (Exception e) { |
Jian Li | 311a9c9 | 2018-07-09 16:48:36 +0900 | [diff] [blame] | 94 | print("Inputted valid source IP & destination IP in CIDR (e.g., \"10.1.0.4/32\")"); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 98 | vtapCriterionBuilder.ipProtocol(getProtocolTypeFromString(ipProto.toLowerCase())); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 99 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame^] | 100 | vtapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort)); |
| 101 | vtapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort)); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 102 | |
| 103 | return true; |
| 104 | } |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 105 | } |