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; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.TpPort; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.openstackvtap.api.OpenstackVtap; |
| 25 | import org.onosproject.openstackvtap.api.OpenstackVtapAdminService; |
| 26 | import org.onosproject.openstackvtap.impl.DefaultOpenstackVtapCriterion; |
| 27 | |
| 28 | /** |
| 29 | * Command line interface for adding openstack vTap rule. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "openstack-vtap-add", |
| 32 | description = "OpenstackVtap activate") |
| 33 | public class OpenstackVtapAddCommand extends AbstractShellCommand { |
| 34 | |
| 35 | private final OpenstackVtapAdminService vTapService = get(OpenstackVtapAdminService.class); |
| 36 | |
| 37 | @Argument(index = 0, name = "srcIp", |
| 38 | description = "source IP address CIDR (e.g., \"10.1.0.0/16\")", |
| 39 | required = true, multiValued = false) |
| 40 | String srcIp = ""; |
| 41 | |
| 42 | @Argument(index = 1, name = "dstIp", |
| 43 | description = "destination IP address CIDR (e.g., \"10.1.0.0/16\")", |
| 44 | required = true, multiValued = false) |
| 45 | String dstIp = ""; |
| 46 | |
| 47 | @Argument(index = 2, name = "ipProto", |
| 48 | description = "IP protocol [tcp|udp|icmp|none]", |
| 49 | required = false, multiValued = false) |
| 50 | String ipProto = ""; |
| 51 | |
| 52 | @Argument(index = 3, name = "srcTpPort", |
| 53 | description = "source transport layer port (0 is skip)", |
| 54 | required = false, multiValued = false) |
| 55 | int srcTpPort = 0; |
| 56 | |
| 57 | @Argument(index = 4, name = "dstTpPort", |
| 58 | description = "destination transport layer port (0 is skip)", |
| 59 | required = false, multiValued = false) |
| 60 | int dstTpPort = 0; |
| 61 | |
| 62 | @Argument(index = 5, name = "type", |
| 63 | description = "vTap type [all|tx|rx]", |
| 64 | required = false, multiValued = false) |
| 65 | String vTapTypeStr = "all"; |
| 66 | |
| 67 | @Override |
| 68 | protected void execute() { |
| 69 | DefaultOpenstackVtapCriterion.Builder |
| 70 | defaultVtapCriterionBuilder = DefaultOpenstackVtapCriterion.builder(); |
| 71 | if (makeCriterion(defaultVtapCriterionBuilder)) { |
| 72 | OpenstackVtap.Type type = getVtapType(vTapTypeStr); |
| 73 | if (type == null) { |
| 74 | print("Invalid vTap type"); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | OpenstackVtap vTap = vTapService.createVtap(type, defaultVtapCriterionBuilder.build()); |
| 79 | if (vTap != null) { |
| 80 | print("Created OpenstackVtap with id { %s }", vTap.id().toString()); |
| 81 | } else { |
| 82 | print("Failed to create OpenstackVtap"); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private static OpenstackVtap.Type getVtapType(String vTapTypeStr) { |
| 88 | switch (vTapTypeStr.toLowerCase()) { |
| 89 | case "all": |
| 90 | return OpenstackVtap.Type.VTAP_ALL; |
| 91 | case "tx": |
| 92 | return OpenstackVtap.Type.VTAP_TX; |
| 93 | case "rx": |
| 94 | return OpenstackVtap.Type.VTAP_RX; |
| 95 | default: |
| 96 | return OpenstackVtap.Type.VTAP_NONE; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private boolean makeCriterion(DefaultOpenstackVtapCriterion.Builder vTapCriterionBuilder) { |
| 101 | try { |
| 102 | vTapCriterionBuilder.srcIpPrefix(IpPrefix.valueOf(srcIp)); |
| 103 | vTapCriterionBuilder.dstIpPrefix(IpPrefix.valueOf(dstIp)); |
| 104 | } catch (Exception e) { |
| 105 | print("Inputted valid source IP & destination IP in CIDR (e.g., \"10.1.0.0/16\")"); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | switch (ipProto.toLowerCase()) { |
| 110 | case "tcp": |
| 111 | vTapCriterionBuilder.ipProtocol(IPv4.PROTOCOL_TCP); |
| 112 | break; |
| 113 | case "udp": |
| 114 | vTapCriterionBuilder.ipProtocol(IPv4.PROTOCOL_UDP); |
| 115 | break; |
| 116 | case "icmp": |
| 117 | vTapCriterionBuilder.ipProtocol(IPv4.PROTOCOL_ICMP); |
| 118 | break; |
| 119 | default: |
| 120 | log.warn("Invalid protocol type {}", ipProto); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | vTapCriterionBuilder.srcTpPort(TpPort.tpPort(srcTpPort)); |
| 125 | vTapCriterionBuilder.dstTpPort(TpPort.tpPort(dstTpPort)); |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | } |