blob: 37a6b9cbc98d772ae5a3c8ea163a19d94e23f776 [file] [log] [blame]
Daniel Park0e1c7b52018-07-07 01:00:14 +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.openstacknetworking.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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Daniel Park0e1c7b52018-07-07 01:00:14 +090021import org.onlab.packet.Ethernet;
22import org.onlab.packet.IPv4;
Daniel Park0e1c7b52018-07-07 01:00:14 +090023import org.onlab.packet.IpAddress;
24import org.onlab.packet.TpPort;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.openstacknetworking.api.InstancePort;
33import org.onosproject.openstacknetworking.api.InstancePortService;
34import org.onosproject.openstacknetworking.api.OpenstackFlowRuleService;
35
36import java.util.Optional;
37
38import static org.onosproject.openstacknetworking.api.Constants.DHCP_ARP_TABLE;
39import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID;
40import static org.onosproject.openstacknetworking.api.Constants.PRIORITY_FORCED_ACL_RULE;
41
Ray Milkey7a2dee52018-09-28 10:58:28 -070042@Service
Daniel Park0e1c7b52018-07-07 01:00:14 +090043@Command(scope = "onos", name = "openstack-remove-acl",
Daniel Parkcd91a072018-07-09 16:00:40 +090044 description = "Remove acl rules to VM")
Daniel Park0e1c7b52018-07-07 01:00:14 +090045public class OpenstackRemoveAclCommand extends AbstractShellCommand {
46 @Argument(index = 0, name = "src ip", description = "src ip address", required = true)
Daniel Parkcd91a072018-07-09 16:00:40 +090047 private String srcIpStr = null;
Daniel Park0e1c7b52018-07-07 01:00:14 +090048
Daniel Parkcd91a072018-07-09 16:00:40 +090049 @Argument(index = 1, name = "src ip", description = "src tcp port", required = true)
50 private int srcPort = 0;
51
52 @Argument(index = 2, name = "dst ip", description = "dst ip address", required = true)
53 private String dstIpStr = null;
54
55 @Argument(index = 3, name = "dst port", description = "dst tcp port", required = true)
56 private int dstPort = 0;
Daniel Park0e1c7b52018-07-07 01:00:14 +090057
58 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070059 protected void doExecute() {
Daniel Park0e1c7b52018-07-07 01:00:14 +090060
61 OpenstackFlowRuleService flowRuleService = AbstractShellCommand.get(OpenstackFlowRuleService.class);
62 CoreService coreService = AbstractShellCommand.get(CoreService.class);
63
64 ApplicationId appId = coreService.getAppId(OPENSTACK_NETWORKING_APP_ID);
65
66 InstancePortService instancePortService = AbstractShellCommand.get(InstancePortService.class);
67
Daniel Parkcd91a072018-07-09 16:00:40 +090068 IpAddress srcIpAddress = null;
69
70 IpAddress dstIpAddress = null;
71
Daniel Park0e1c7b52018-07-07 01:00:14 +090072 try {
Daniel Parkcd91a072018-07-09 16:00:40 +090073 srcIpAddress = IpAddress.valueOf(srcIpStr);
Daniel Park0e1c7b52018-07-07 01:00:14 +090074
Daniel Parkcd91a072018-07-09 16:00:40 +090075 dstIpAddress = IpAddress.valueOf(dstIpStr);
Daniel Park0e1c7b52018-07-07 01:00:14 +090076 } catch (IllegalArgumentException e) {
77 log.error("IllegalArgumentException occurred because of {}", e.toString());
Ray Milkeyf6911bd2018-07-10 08:50:10 -070078 return;
Daniel Park0e1c7b52018-07-07 01:00:14 +090079 }
Daniel Parkcd91a072018-07-09 16:00:40 +090080
81 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder()
82 .matchEthType(Ethernet.TYPE_IPV4)
83 .matchIPSrc(srcIpAddress.toIpPrefix())
84 .matchIPDst(dstIpAddress.toIpPrefix());
85
86 TrafficTreatment treatment = DefaultTrafficTreatment.builder().
87 drop().build();
88
89 if (srcPort != 0 || dstPort != 0) {
90 sBuilder.matchIPProtocol(IPv4.PROTOCOL_TCP);
91 if (srcPort != 0) {
92 sBuilder.matchTcpSrc(TpPort.tpPort(srcPort));
93 }
94
95 if (dstPort != 0) {
96 sBuilder.matchTcpDst(TpPort.tpPort(dstPort));
97 }
98 }
99
100 log.info("Deny the packet from srcIp: {}, dstPort: {} to dstIp: {}, dstPort: {}",
101 srcIpAddress.toString(),
102 srcPort,
103 dstIpAddress.toString(),
104 dstPort);
105
106 Optional<InstancePort> instancePort = instancePortService.instancePorts().stream()
107 .filter(port -> port.ipAddress().toString().equals(dstIpStr))
108 .findAny();
109
110 if (!instancePort.isPresent()) {
111 log.info("Instance port that matches with the given dst ip address isn't present {}");
112 return;
113 }
114
115 flowRuleService.setRule(
116 appId,
117 instancePort.get().deviceId(),
118 sBuilder.build(),
119 treatment,
120 PRIORITY_FORCED_ACL_RULE,
121 DHCP_ARP_TABLE,
122 false);
Daniel Park0e1c7b52018-07-07 01:00:14 +0900123 }
124}