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.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.openstackvtap.api.OpenstackVtap; |
| 24 | import org.onosproject.openstackvtap.api.OpenstackVtapAdminService; |
Jian Li | b45ffda | 2019-10-23 14:05:32 +0900 | [diff] [blame^] | 25 | import org.onosproject.openstackvtap.api.OpenstackVtapCriterion; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 26 | |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 27 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.getVtapTypeFromString; |
Jian Li | b45ffda | 2019-10-23 14:05:32 +0900 | [diff] [blame^] | 28 | import static org.onosproject.openstackvtap.util.OpenstackVtapUtil.makeVtapCriterion; |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 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 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 33 | @Service |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 34 | @Command(scope = "onos", name = "openstack-vtap-add", |
| 35 | description = "OpenstackVtap activate") |
| 36 | public class OpenstackVtapAddCommand extends AbstractShellCommand { |
| 37 | |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 38 | private final OpenstackVtapAdminService vtapService = |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 39 | get(OpenstackVtapAdminService.class); |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 40 | |
| 41 | @Argument(index = 0, name = "srcIp", |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 42 | description = "source IP address CIDR (e.g., \"10.1.0.2/32\")", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 43 | required = true, multiValued = false) |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 44 | @Completion(VmIpCompleter.class) |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 45 | String srcIp = ""; |
| 46 | |
| 47 | @Argument(index = 1, name = "dstIp", |
Jian Li | 26ef130 | 2018-07-04 14:37:06 +0900 | [diff] [blame] | 48 | description = "destination IP address CIDR (e.g., \"10.1.0.3/32\")", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 49 | required = true, multiValued = false) |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 50 | @Completion(VmIpCompleter.class) |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 51 | String dstIp = ""; |
| 52 | |
| 53 | @Argument(index = 2, name = "ipProto", |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 54 | description = "IP protocol [any|tcp|udp|icmp]", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 55 | required = false, multiValued = false) |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 56 | @Completion(ProtocolTypeCompleter.class) |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 57 | String ipProto = "any"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 58 | |
| 59 | @Argument(index = 3, name = "srcTpPort", |
| 60 | description = "source transport layer port (0 is skip)", |
| 61 | required = false, multiValued = false) |
| 62 | int srcTpPort = 0; |
| 63 | |
| 64 | @Argument(index = 4, name = "dstTpPort", |
| 65 | description = "destination transport layer port (0 is skip)", |
| 66 | required = false, multiValued = false) |
| 67 | int dstTpPort = 0; |
| 68 | |
| 69 | @Argument(index = 5, name = "type", |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 70 | description = "vtap type [all|rx|tx]", |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 71 | required = false, multiValued = false) |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 72 | @Completion(VtapTypeCompleter.class) |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 73 | String vtapTypeStr = "all"; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 74 | |
| 75 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 76 | protected void doExecute() { |
Jian Li | b45ffda | 2019-10-23 14:05:32 +0900 | [diff] [blame^] | 77 | OpenstackVtapCriterion criterion = |
| 78 | makeVtapCriterion(srcIp, dstIp, ipProto, srcTpPort, dstTpPort); |
| 79 | OpenstackVtap.Type type = getVtapTypeFromString(vtapTypeStr); |
| 80 | if (type == null) { |
| 81 | print("Invalid vtap type"); |
| 82 | return; |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Jian Li | b45ffda | 2019-10-23 14:05:32 +0900 | [diff] [blame^] | 85 | OpenstackVtap vtap = vtapService.createVtap(type, criterion); |
| 86 | if (vtap != null) { |
| 87 | print("Created OpenstackVtap with id { %s }", vtap.id().toString()); |
| 88 | } else { |
| 89 | print("Failed to create OpenstackVtap"); |
| 90 | } |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 91 | } |
Jian Li | c253810 | 2018-07-03 22:42:07 +0900 | [diff] [blame] | 92 | } |