Merge "Fix bugs related to wrong exception handling"
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / StatelessIngressAclServiceImpl.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.aclservice;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.genius.mdsalutil.ActionInfo;
17 import org.opendaylight.genius.mdsalutil.InstructionInfo;
18 import org.opendaylight.genius.mdsalutil.MatchFieldType;
19 import org.opendaylight.genius.mdsalutil.MatchInfo;
20 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
23 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
24 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
25 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
26 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
27 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.Matches;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.AceType;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Provides the stateless implementation for ingress (w.r.t VM) ACL service.
40  *
41  * <p>
42  * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
43  * and vice versa.
44  */
45 public class StatelessIngressAclServiceImpl extends AbstractIngressAclServiceImpl {
46
47     private static final Logger LOG = LoggerFactory.getLogger(StatelessIngressAclServiceImpl.class);
48
49     public StatelessIngressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
50         super(dataBroker, mdsalManager);
51     }
52
53     @Override
54     protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
55             List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
56     }
57
58     @Override
59     protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
60             Map<String, List<MatchInfoBase>> flowMap, String flowName) {
61         // Not in use here. programAceRule function is overridden.
62         return null;
63     }
64
65     @Override
66     protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
67             List<AllowedAddressPairs> syncAllowedAddresses) {
68         SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
69         if (!aceAttr.getDirection().equals(DirectionIngress.class)) {
70             return;
71         }
72         Matches matches = ace.getMatches();
73         AceType aceType = matches.getAceType();
74         Map<String, List<MatchInfoBase>> flowMap = null;
75         Short protocol = null;
76
77         if (aceType instanceof AceIp) {
78             protocol = ((AceIp)aceType).getProtocol();
79             flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
80         }
81         if (null == flowMap) {
82             LOG.error("Failed to apply ACL {} lPortTag {}", ace.getKey(), lportTag);
83             return;
84         }
85         for (Map.Entry<String, List<MatchInfoBase>> flow : flowMap.entrySet()) {
86             List<MatchInfoBase> flowMatches = flow.getValue();
87             boolean hasTcpDstMatch = AclServiceUtils.containsMatchFieldType(flowMatches,
88                     NxMatchFieldType.nx_tcp_dst_with_mask);
89             if (hasTcpDstMatch || protocol == null) {
90                 String flowName = flow.getKey() + "Ingress" + lportTag + ace.getKey().getRuleName();
91                 flowMatches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
92                 programAllowSynRules(dpId, flowName, flowMatches, addOrRemove);
93             }
94         }
95     }
96
97     private void programAllowSynRules(BigInteger dpId, String origFlowName,
98             List<MatchInfoBase> origFlowMatches, int addOrRemove) {
99         List<MatchInfoBase> flowMatches = new ArrayList<>();
100         flowMatches.addAll(origFlowMatches);
101         flowMatches.add(new MatchInfo(MatchFieldType.tcp_flags, new long[] { AclConstants.TCP_FLAG_SYN }));
102         List<ActionInfo> actionsInfos = new ArrayList<>();
103         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
104
105         String flowName = "SYN_" + origFlowName;
106         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_SYN_ALLOW_PRIORITY,
107                 "ACL_SYN_", 0, 0, AclConstants.COOKIE_ACL_BASE, flowMatches, instructions, addOrRemove);
108         String oper = getOperAsString(addOrRemove);
109         LOG.debug("{} allow syn packet flow {}", oper, flowName);
110     }
111
112 }