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