De-static-ify aclservice utility classes methods and fields
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / StatelessEgressAclServiceImpl.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.AclDataUtil;
27 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
28 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
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;
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;
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.AceType;
32 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;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Provides the stateless implementation for egress (w.r.t VM) ACL service.
41  *
42  * <p>
43  * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
44  * and vice versa.
45  */
46 public class StatelessEgressAclServiceImpl extends AbstractEgressAclServiceImpl {
47
48     private static final Logger LOG = LoggerFactory.getLogger(StatelessEgressAclServiceImpl.class);
49
50     public StatelessEgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
51             AclServiceUtils aclServiceUtils) {
52         super(dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
53     }
54
55     @Override
56     protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
57             List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
58     }
59
60     @Override
61     protected String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
62             Map<String, List<MatchInfoBase>> flowMap, String flowName) {
63         // Not in use here. programAceRule function is overridden.
64         return null;
65     }
66
67     @Override
68     protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
69             List<AllowedAddressPairs> syncAllowedAddresses) {
70         SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
71         if (!aceAttr.getDirection().equals(DirectionEgress.class)) {
72             return;
73         }
74         Matches matches = ace.getMatches();
75         AceType aceType = matches.getAceType();
76         Map<String, List<MatchInfoBase>> flowMap = null;
77         Short protocol = null;
78
79         if (aceType instanceof AceIp) {
80             protocol = ((AceIp)aceType).getProtocol();
81             flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
82         }
83         if (null == flowMap) {
84             LOG.error("Failed to apply ACL {}", ace.getKey());
85             return;
86         }
87         for (Map.Entry<String, List<MatchInfoBase>> flow : flowMap.entrySet()) {
88             String flowName = flow.getKey();
89             List<MatchInfoBase> flowMatches = flow.getValue();
90             boolean hasTcpMatch = AclServiceUtils.containsMatchFieldType(flowMatches,
91                     NxMatchFieldType.nx_tcp_dst_with_mask) || AclServiceUtils.containsMatchFieldType(flowMatches,
92                             NxMatchFieldType.nx_tcp_src_with_mask);
93             if (hasTcpMatch || protocol == null) {
94                 flowName += "Egress" + lportTag + ace.getKey().getRuleName();
95                 flowMatches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
96
97                 programAllowSynRules(dpId, flowName, flowMatches, addOrRemove, protocol);
98             }
99         }
100     }
101
102     private void programAllowSynRules(BigInteger dpId, String origFlowName,
103             List<MatchInfoBase> origFlowMatches, int addFlow, Short protocol) {
104         List<MatchInfoBase> flowMatches = new ArrayList<>();
105         flowMatches.addAll(origFlowMatches);
106         if (new Short((short) NwConstants.IP_PROT_TCP).equals(protocol)) {
107             flowMatches.add(new MatchInfo(MatchFieldType.tcp_flags, new long[] { AclConstants.TCP_FLAG_SYN }));
108         }
109
110         List<ActionInfo> actionsInfos = new ArrayList<>();
111         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
112
113         String flowName = "SYN_" + origFlowName;
114         syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_SYN_ALLOW_PRIORITY,
115                 "ACL_SYN_", 0, 0, AclConstants.COOKIE_ACL_BASE, flowMatches, instructions, addFlow);
116         String oper = getOperAsString(addFlow);
117         LOG.debug("{} allow syn packet flow {}", oper, flowName);
118     }
119
120 }