Missed Table 246 flow
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / AbstractAclServiceImpl.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 static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
11
12 import com.google.common.collect.Lists;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23 import java.util.SortedSet;
24 import java.util.concurrent.ConcurrentMap;
25 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.genius.mdsalutil.ActionInfo;
31 import org.opendaylight.genius.mdsalutil.FlowEntity;
32 import org.opendaylight.genius.mdsalutil.InstructionInfo;
33 import org.opendaylight.genius.mdsalutil.MDSALUtil;
34 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
35 import org.opendaylight.genius.mdsalutil.NwConstants;
36 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack;
37 import org.opendaylight.genius.mdsalutil.actions.ActionNxConntrack.NxCtAction;
38 import org.opendaylight.genius.mdsalutil.actions.ActionNxCtClear;
39 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
40 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
41 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
42 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
43 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
44 import org.opendaylight.genius.mdsalutil.nxmatches.NxMatchCtState;
45 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
46 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
47 import org.opendaylight.netvirt.aclservice.api.AclServiceListener;
48 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
49 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
50 import org.opendaylight.netvirt.aclservice.utils.AclConntrackClassifierType;
51 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
52 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
53 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
54 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
57 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;
58 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;
59 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.ace.ip.ace.ip.version.AceIpv4;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeBase;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionBase;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl.InterfaceType;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.SubnetInfo;
70 import org.opendaylight.yangtools.yang.common.Uint64;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 public abstract class AbstractAclServiceImpl implements AclServiceListener {
75
76     private static final Logger LOG = LoggerFactory.getLogger(AbstractAclServiceImpl.class);
77
78     protected final IMdsalApiManager mdsalManager;
79     protected final ManagedNewTransactionRunner txRunner;
80     protected final Class<? extends ServiceModeBase> serviceMode;
81     protected final AclDataUtil aclDataUtil;
82     protected final AclServiceUtils aclServiceUtils;
83     protected final JobCoordinator jobCoordinator;
84     protected final AclInterfaceCache aclInterfaceCache;
85
86     protected final Class<? extends DirectionBase> direction;
87     protected final String directionString;
88
89     /**
90      * Initialize the member variables.
91      *
92      * @param serviceMode the service mode
93      * @param dataBroker the data broker instance.
94      * @param mdsalManager the mdsal manager instance.
95      * @param aclDataUtil the acl data util.
96      * @param aclServiceUtils the acl service util.
97      * @param jobCoordinator the job coordinator
98      * @param aclInterfaceCache the acl interface cache
99      */
100     public AbstractAclServiceImpl(Class<? extends ServiceModeBase> serviceMode, DataBroker dataBroker,
101             IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil, AclServiceUtils aclServiceUtils,
102             JobCoordinator jobCoordinator, AclInterfaceCache aclInterfaceCache) {
103         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
104         this.mdsalManager = mdsalManager;
105         this.serviceMode = serviceMode;
106         this.aclDataUtil = aclDataUtil;
107         this.aclServiceUtils = aclServiceUtils;
108         this.jobCoordinator = jobCoordinator;
109         this.aclInterfaceCache = aclInterfaceCache;
110
111         this.direction =
112                 this.serviceMode.equals(ServiceModeEgress.class) ? DirectionIngress.class : DirectionEgress.class;
113         this.directionString = this.direction.equals(DirectionEgress.class) ? "Egress" : "Ingress";
114     }
115
116     @Override
117     public boolean applyAcl(AclInterface port) {
118         if (port == null) {
119             LOG.error("port cannot be null");
120             return false;
121         }
122         BigInteger dpId = port.getDpId();
123         String portId = port.getInterfaceId();
124         if (dpId == null || port.getLPortTag() == null) {
125             LOG.error("Unable to find DpId from ACL interface with id {}", portId);
126             return false;
127         }
128
129         LOG.debug("Applying ACL on port {} with DpId {}", port, dpId);
130         List<FlowEntity> flowEntries = new ArrayList<>();
131         if (port.getInterfaceType() == InterfaceType.DhcpService) {
132             programDhcpService(flowEntries, port, Action.ADD, NwConstants.ADD_FLOW);
133         } else {
134             if (port.getSecurityGroups() == null) {
135                 LOG.info("Port {} without SGs", portId);
136                 return false;
137             }
138             programAclWithAllowedAddress(flowEntries, port, port.getAllowedAddressPairs(),
139                     Action.ADD, NwConstants.ADD_FLOW);
140             updateRemoteAclFilterTable(port, NwConstants.ADD_FLOW);
141         }
142         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portId, flowEntries, NwConstants.ADD_FLOW);
143         return true;
144     }
145
146     @Override
147     public boolean bindAcl(AclInterface port) {
148         if (port == null) {
149             LOG.error("Port cannot be null for binding ACL service");
150             return false;
151         }
152         bindService(port);
153         return true;
154     }
155
156     @Override
157     public boolean unbindAcl(AclInterface port) {
158         if (port == null) {
159             LOG.error("Port cannot be null for unbinding ACL service");
160             return false;
161         }
162         if (port.getDpId() != null) {
163             unbindService(port);
164         }
165         return true;
166     }
167
168     @Override
169     public boolean updateAcl(AclInterface portBefore, AclInterface portAfter) {
170         // this check is to avoid situations of port update coming before interface state is up
171         if (portAfter.getDpId() == null || portAfter.getLPortTag() == null) {
172             LOG.debug("Unable to find DpId from ACL interface with id {} and lport {}", portAfter.getInterfaceId(),
173                     portAfter.getLPortTag());
174             return false;
175         }
176         boolean result = true;
177         boolean isPortSecurityEnable = portAfter.isPortSecurityEnabled();
178         boolean isPortSecurityEnableBefore = portBefore.isPortSecurityEnabled();
179         // if port security is changed, apply/remove Acls
180         if (isPortSecurityEnableBefore != isPortSecurityEnable) {
181             LOG.debug("On ACL update, Port security is {} for {}", isPortSecurityEnable ? "Enabled" :
182                     "Disabled", portAfter.getInterfaceId());
183             if (isPortSecurityEnable) {
184                 result = applyAcl(portAfter);
185             } else {
186                 result = removeAcl(portBefore);
187             }
188         } else if (isPortSecurityEnable) {
189             // Acls has been updated, find added/removed Acls and act accordingly.
190             processInterfaceUpdate(portBefore, portAfter);
191             LOG.debug("On ACL update, ACL has been updated for {}", portAfter.getInterfaceId());
192         } else if (portAfter.getInterfaceType() == InterfaceType.DhcpService) {
193             processDhcpServiceInterfaceUpdate(portBefore, portAfter);
194         }
195
196         return result;
197     }
198
199     private void processDhcpServiceInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
200         List<FlowEntity> addFlowEntries = new ArrayList<>();
201         List<FlowEntity> deleteFlowEntries = new ArrayList<>();
202         List<AllowedAddressPairs> addedAaps = AclServiceUtils
203                 .getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(), portBefore.getAllowedAddressPairs());
204         List<AllowedAddressPairs> deletedAaps = AclServiceUtils
205                 .getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(), portAfter.getAllowedAddressPairs());
206         if (!deletedAaps.isEmpty()) {
207             processDhcpServiceUpdate(deleteFlowEntries, portBefore, deletedAaps, NwConstants.DEL_FLOW);
208         }
209         if (!addedAaps.isEmpty()) {
210             processDhcpServiceUpdate(addFlowEntries, portAfter, addedAaps, NwConstants.ADD_FLOW);
211         }
212         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), deleteFlowEntries,
213                 NwConstants.DEL_FLOW);
214         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), addFlowEntries,
215                 NwConstants.ADD_FLOW);
216     }
217
218     private void processInterfaceUpdate(AclInterface portBefore, AclInterface portAfter) {
219         List<FlowEntity> addFlowEntries = new ArrayList<>();
220         List<FlowEntity> deleteFlowEntries = new ArrayList<>();
221         List<AllowedAddressPairs> addedAaps = AclServiceUtils
222                 .getUpdatedAllowedAddressPairs(portAfter.getAllowedAddressPairs(), portBefore.getAllowedAddressPairs());
223         List<AllowedAddressPairs> deletedAaps = AclServiceUtils
224                 .getUpdatedAllowedAddressPairs(portBefore.getAllowedAddressPairs(), portAfter.getAllowedAddressPairs());
225         if (!deletedAaps.isEmpty()) {
226             programAclWithAllowedAddress(deleteFlowEntries, portBefore, deletedAaps, Action.UPDATE,
227                     NwConstants.DEL_FLOW);
228             updateRemoteAclFilterTable(portBefore, portBefore.getSecurityGroups(), deletedAaps, NwConstants.DEL_FLOW);
229         }
230         if (!addedAaps.isEmpty()) {
231             programAclWithAllowedAddress(addFlowEntries, portAfter, addedAaps, Action.UPDATE, NwConstants.ADD_FLOW);
232             updateRemoteAclFilterTable(portAfter, portAfter.getSecurityGroups(), addedAaps, NwConstants.ADD_FLOW);
233         }
234         if (portAfter.getSubnetInfo() != null && portBefore.getSubnetInfo() == null) {
235             programBroadcastRules(addFlowEntries, portAfter, Action.UPDATE, NwConstants.ADD_FLOW);
236         }
237         handleSubnetChange(portBefore, portAfter, addFlowEntries, deleteFlowEntries);
238
239         List<Uuid> addedAcls = AclServiceUtils.getUpdatedAclList(portAfter.getSecurityGroups(),
240                 portBefore.getSecurityGroups());
241         List<Uuid> deletedAcls = AclServiceUtils.getUpdatedAclList(portBefore.getSecurityGroups(),
242                 portAfter.getSecurityGroups());
243         if (!deletedAcls.isEmpty() || !addedAcls.isEmpty()) {
244             handleAclChange(deleteFlowEntries, portBefore, deletedAcls, NwConstants.DEL_FLOW);
245             handleAclChange(addFlowEntries, portAfter, addedAcls, NwConstants.ADD_FLOW);
246         }
247
248         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), deleteFlowEntries,
249                 NwConstants.DEL_FLOW);
250         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + portAfter.getInterfaceId(), addFlowEntries,
251                 NwConstants.ADD_FLOW);
252     }
253
254     private void handleSubnetChange(AclInterface portBefore, AclInterface portAfter,
255             List<FlowEntity> addFlowEntries, List<FlowEntity> deleteFlowEntries) {
256         List<SubnetInfo> deletedSubnets =
257                 AclServiceUtils.getSubnetDiff(portBefore.getSubnetInfo(), portAfter.getSubnetInfo());
258         List<SubnetInfo> addedSubnets =
259                 AclServiceUtils.getSubnetDiff(portAfter.getSubnetInfo(), portBefore.getSubnetInfo());
260
261         if (deletedSubnets != null && !deletedSubnets.isEmpty()) {
262             programIcmpv6RARule(deleteFlowEntries, portAfter, deletedSubnets, NwConstants.DEL_FLOW);
263             programSubnetBroadcastRules(deleteFlowEntries, portAfter, deletedSubnets, NwConstants.DEL_FLOW);
264         }
265         if (addedSubnets != null && !addedSubnets.isEmpty()) {
266             programIcmpv6RARule(addFlowEntries, portAfter, addedSubnets, NwConstants.ADD_FLOW);
267             programSubnetBroadcastRules(addFlowEntries, portAfter, addedSubnets, NwConstants.ADD_FLOW);
268         }
269     }
270
271     private void handleAclChange(List<FlowEntity> flowEntries, AclInterface port, List<Uuid> aclList,
272             int addOrRemove) {
273         int operationForAclRules = addOrRemove == NwConstants.DEL_FLOW ? NwConstants.MOD_FLOW : addOrRemove;
274         programAclRules(flowEntries, port, aclList, operationForAclRules);
275         updateRemoteAclFilterTable(port, aclList, port.getAllowedAddressPairs(), addOrRemove);
276         programAclDispatcherTable(flowEntries, port, addOrRemove);
277     }
278
279     protected SortedSet<Integer> getRemoteAclTags(AclInterface port) {
280         return this.direction == DirectionIngress.class ? port.getIngressRemoteAclTags()
281                 : port.getEgressRemoteAclTags();
282     }
283
284     protected void programAclDispatcherTable(List<FlowEntity> flowEntries, AclInterface port, int addOrRemove) {
285         SortedSet<Integer> remoteAclTags = getRemoteAclTags(port);
286         if (remoteAclTags.isEmpty()) {
287             LOG.debug("No {} rules with remote group id for port={}", this.directionString, port.getInterfaceId());
288             return;
289         }
290         Integer firstRemoteAclTag = remoteAclTags.first();
291         Integer lastRemoteAclTag = remoteAclTags.last();
292
293         programFirstRemoteAclEntryInDispatcherTable(flowEntries, port, firstRemoteAclTag, addOrRemove);
294         programLastRemoteAclEntryInDispatcherTable(flowEntries, port, lastRemoteAclTag, addOrRemove);
295
296         Integer previousRemoteAclTag = firstRemoteAclTag;
297         for (Integer remoteAclTag : remoteAclTags) {
298             if (remoteAclTag.equals(firstRemoteAclTag)) {
299                 continue;
300             }
301             List<MatchInfoBase> matches = new ArrayList<>();
302             matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndRemoteAclTag(port.getLPortTag(),
303                     previousRemoteAclTag, serviceMode));
304             String flowId = this.directionString + "_ACL_Dispatcher_" + port.getDpId() + "_" + port.getLPortTag() + "_"
305                     + remoteAclTag;
306
307             List<InstructionInfo> instructions =
308                     AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclRuleBasedFilterTable());
309             instructions.add(AclServiceUtils.getWriteMetadataForRemoteAclTag(remoteAclTag));
310             addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(), flowId,
311                     AclConstants.ACE_GOTO_NEXT_REMOTE_ACL_PRIORITY, 0, 0, AclConstants.COOKIE_ACL_BASE, matches,
312                     instructions, addOrRemove);
313
314             previousRemoteAclTag = remoteAclTag;
315         }
316     }
317
318     protected void programFirstRemoteAclEntryInDispatcherTable(List<FlowEntity> flowEntries, AclInterface port,
319             Integer firstRemoteAclTag, int addOrRemove) {
320         List<MatchInfoBase> matches = new ArrayList<>();
321         matches.add(AclServiceUtils.buildLPortTagMatch(port.getLPortTag(), serviceMode));
322         String flowId = this.directionString + "_ACL_Dispatcher_First_" + port.getDpId() + "_" + port.getLPortTag()
323                 + "_" + firstRemoteAclTag;
324
325         List<InstructionInfo> instructions =
326                 AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclRuleBasedFilterTable());
327         instructions.add(AclServiceUtils.getWriteMetadataForRemoteAclTag(firstRemoteAclTag));
328         addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(), flowId,
329                 AclConstants.ACE_FIRST_REMOTE_ACL_PRIORITY, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions,
330                 addOrRemove);
331     }
332
333     protected void programLastRemoteAclEntryInDispatcherTable(List<FlowEntity> flowEntries, AclInterface port,
334             Integer lastRemoteAclTag, int addOrRemove) {
335         List<MatchInfoBase> matches = new ArrayList<>();
336         matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndRemoteAclTag(port.getLPortTag(), lastRemoteAclTag,
337                 serviceMode));
338         String flowId = this.directionString + "_ACL_Dispatcher_Last_" + port.getDpId() + "_" + port.getLPortTag() + "_"
339                 + lastRemoteAclTag;
340
341         List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
342         addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(), flowId,
343                 AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY, 0, 0, AclServiceUtils.getDropFlowCookie(port.getLPortTag()),
344                 matches, instructions, addOrRemove);
345     }
346
347     private void programAclWithAllowedAddress(List<FlowEntity> flowEntries, AclInterface port,
348             List<AllowedAddressPairs> allowedAddresses, Action action, int addOrRemove) {
349         Uint64 dpId = Uint64.valueOf(port.getDpId());
350         int lportTag = port.getLPortTag();
351         LOG.debug("Applying ACL Allowed Address on DpId {}, lportTag {}, Action {}", dpId, lportTag, action);
352         String portId = port.getInterfaceId();
353         programAntiSpoofingRules(flowEntries, port, allowedAddresses, action, addOrRemove);
354         programAclPortSpecificFixedRules(flowEntries, dpId, allowedAddresses, lportTag, portId, action, addOrRemove);
355         if (action == Action.ADD || action == Action.REMOVE) {
356             programAclRules(flowEntries, port, port.getSecurityGroups(), addOrRemove);
357             programAclDispatcherTable(flowEntries, port, addOrRemove);
358         }
359     }
360
361     /**
362      * Programs the acl custom rules.
363      *
364      * @param flowEntries the flow entries
365      * @param port acl interface
366      * @param aclUuidList the list of acl uuid to be applied
367      * @param addOrRemove whether to delete or add flow
368      * @return program succeeded
369      */
370     protected boolean programAclRules(List<FlowEntity> flowEntries, AclInterface port, List<Uuid> aclUuidList,
371             int addOrRemove) {
372         BigInteger dpId = port.getDpId();
373         LOG.debug("Applying custom rules on DpId {}, lportTag {}", dpId, port.getLPortTag());
374         if (aclUuidList == null || dpId == null) {
375             LOG.warn("{} ACL parameters can not be null. dpId={}, aclUuidList={}", this.directionString, dpId,
376                     aclUuidList);
377             return false;
378         }
379         for (Uuid aclUuid : aclUuidList) {
380             Acl acl = this.aclDataUtil.getAcl(aclUuid.getValue());
381             if (null == acl) {
382                 LOG.warn("The ACL {} not found in cache", aclUuid.getValue());
383                 continue;
384             }
385             for (Ace ace : AclServiceUtils.aceList(acl)) {
386                 programAceRule(flowEntries, port, aclUuid.getValue(), ace, addOrRemove);
387             }
388         }
389         return true;
390     }
391
392     /**
393      * Programs the ace specific rule.
394      *
395      * @param flowEntries flow entries
396      * @param port acl interface
397      * @param aclName the acl name
398      * @param ace rule to be program
399      * @param addOrRemove whether to delete or add flow
400      */
401     protected void programAceRule(List<FlowEntity> flowEntries, AclInterface port, String aclName, Ace ace,
402             int addOrRemove) {
403         SecurityRuleAttr aceAttr = AclServiceUtils.getAccessListAttributes(ace);
404         if (aceAttr == null) {
405             LOG.error("Ace {} of Acl {} is either null or not having SecurityRuleAttr",
406                     ace == null ? null : ace.getRuleName(), aclName);
407             return;
408         }
409         if (addOrRemove == NwConstants.ADD_FLOW && aceAttr.isDeleted()) {
410             LOG.trace("Ignoring {} rule which is already deleted", ace.getRuleName());
411             return;
412         }
413         if (!isValidDirection(aceAttr.getDirection())) {
414             LOG.trace("Ignoring {} direction while processing for {} ACE Rule {}", aceAttr.getDirection(),
415                     this.directionString, ace.getRuleName());
416             return;
417         }
418         LOG.debug("Program {} ACE rule for dpId={}, lportTag={}, addOrRemove={}, ace={}, portId={}",
419                 this.directionString, port.getDpId(), port.getLPortTag(), addOrRemove, ace.getRuleName(),
420                 port.getInterfaceId());
421
422         Matches matches = ace.getMatches();
423         if (matches != null && matches.getAceType() instanceof AceIp) {
424             Map<String, List<MatchInfoBase>> flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
425             if (!AclServiceUtils.doesAceHaveRemoteGroupId(aceAttr)) {
426                 // programming for ACE which doesn't have any remote group Id
427                 programForAceNotHavingRemoteAclId(flowEntries, port, aclName, ace, flowMap, addOrRemove);
428             } else {
429                 Uuid remoteAclId = aceAttr.getRemoteGroupId();
430                 // programming for ACE which have remote group Id
431                 programAceSpecificFlows(flowEntries, port, aclName, ace, flowMap, remoteAclId, addOrRemove);
432             }
433         }
434     }
435
436     protected void programForAceNotHavingRemoteAclId(List<FlowEntity> flowEntries, AclInterface port, String aclName,
437             Ace ace, @Nullable Map<String, List<MatchInfoBase>> flowMap, int addOrRemove) {
438         if (null == flowMap) {
439             return;
440         }
441         MatchInfoBase lportTagMatch = AclServiceUtils.buildLPortTagMatch(port.getLPortTag(), serviceMode);
442         List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclCommitterTable());
443         Integer flowPriority = this.aclServiceUtils.getAceFlowPriority(aclName);
444
445         for (Entry<String, List<MatchInfoBase>> entry : flowMap.entrySet()) {
446             String flowName = entry.getKey();
447             List<MatchInfoBase> matches = entry.getValue();
448             matches.add(lportTagMatch);
449             String flowId = flowName + this.directionString + "_" + port.getDpId() + "_" + port.getLPortTag() + "_"
450                     + ace.key().getRuleName();
451
452             int operation = addOrRemove == NwConstants.MOD_FLOW ? NwConstants.DEL_FLOW : addOrRemove;
453             addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclFilterCumDispatcherTable(),
454                     flowId, flowPriority, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, operation);
455
456             if (addOrRemove != NwConstants.DEL_FLOW) {
457                 programAclForExistingTrafficTable(port, ace, addOrRemove, flowName, matches, flowPriority);
458             }
459         }
460     }
461
462     protected void programAceSpecificFlows(List<FlowEntity> flowEntries, AclInterface port, String aclName, Ace ace,
463             @Nullable Map<String, List<MatchInfoBase>> flowMap, Uuid remoteAclId, int addOrRemove) {
464         if (null == flowMap) {
465             return;
466         }
467         Integer remoteAclTag = this.aclServiceUtils.getAclTag(remoteAclId);
468         if (remoteAclTag == null || remoteAclTag == AclConstants.INVALID_ACL_TAG) {
469             LOG.error("remoteAclTag={} is null or invalid for remoteAclId={}", remoteAclTag, remoteAclId);
470             return;
471         }
472         List<MatchInfoBase> lportAndAclMatches =
473                 AclServiceUtils.buildMatchesForLPortTagAndRemoteAclTag(port.getLPortTag(), remoteAclTag, serviceMode);
474         List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getGotoInstructionInfo(getAclRemoteAclTable());
475         Integer flowPriority = this.aclServiceUtils.getAceFlowPriority(aclName);
476
477         for (Entry<String, List<MatchInfoBase>> entry : flowMap.entrySet()) {
478             String flowName = entry.getKey();
479             List<MatchInfoBase> matches = entry.getValue();
480             matches.addAll(lportAndAclMatches);
481             String flowId = flowName + this.directionString + "_" + port.getDpId() + "_" + port.getLPortTag() + "_"
482                     + ace.key().getRuleName();
483
484             int operation = addOrRemove == NwConstants.MOD_FLOW ? NwConstants.DEL_FLOW : addOrRemove;
485             addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclRuleBasedFilterTable(), flowId,
486                     flowPriority, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, operation);
487
488             if (addOrRemove != NwConstants.DEL_FLOW) {
489                 programAclForExistingTrafficTable(port, ace, addOrRemove, flowName, matches, flowPriority);
490             }
491         }
492     }
493
494     private void programAclForExistingTrafficTable(AclInterface port, Ace ace, int addOrRemove, String flowName,
495             List<MatchInfoBase> matches, Integer priority) {
496         if (port == null || port.getElanId() == null) {
497             LOG.debug("Acl interface or elan id is null, No need to update traffic flow table.");
498             return;
499         }
500
501         AceIp acl = (AceIp) ace.getMatches().getAceType();
502         final String newFlowName = flowName + this.directionString + "_" + port.getDpId() + "_" + port.getLPortTag()
503                 + "_" + (acl.getAceIpVersion() instanceof AceIpv4 ? "_IPv4" : "_IPv6") + "_FlowAfterRuleDeleted";
504
505         final List<MatchInfoBase> newMatches =
506                 matches.stream().filter(obj -> !(obj instanceof NxMatchCtState || obj instanceof MatchMetadata))
507                         .collect(Collectors.toList());
508         newMatches.add(AclServiceUtils.buildLPortTagMatch(port.getLPortTag(), serviceMode));
509         newMatches.add(new NxMatchCtState(AclConstants.TRACKED_RPL_CT_STATE, AclConstants.TRACKED_RPL_CT_STATE_MASK));
510
511         List<InstructionInfo> instructions =
512                 AclServiceUtils.createCtMarkInstructionForNewState(getAclFilterCumDispatcherTable(), port.getElanId());
513         // Reversing the flow add/delete operation for this table.
514         List<FlowEntity> flowEntries = new ArrayList<>();
515         int operation = addOrRemove == NwConstants.ADD_FLOW ? NwConstants.DEL_FLOW : NwConstants.ADD_FLOW;
516         addFlowEntryToList(flowEntries, Uint64.valueOf(port.getDpId()), getAclForExistingTrafficTable(), newFlowName,
517                 priority, 0, AclServiceUtils.getHardTimoutForApplyStatefulChangeOnExistingTraffic(ace, aclServiceUtils),
518                 AclConstants.COOKIE_ACL_BASE, newMatches, instructions, operation);
519         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, operation);
520     }
521
522     @Override
523     public boolean removeAcl(AclInterface port) {
524         if (port.getDpId() == null) {
525             LOG.warn("Unable to find DP Id from ACL interface with id {}", port.getInterfaceId());
526             return false;
527         }
528         List<FlowEntity> flowEntries = new ArrayList<>();
529         if (port.getInterfaceType() == InterfaceType.DhcpService) {
530             programDhcpService(flowEntries, port, Action.REMOVE, NwConstants.DEL_FLOW);
531         } else {
532             programAclWithAllowedAddress(flowEntries, port, port.getAllowedAddressPairs(),
533                     Action.REMOVE, NwConstants.DEL_FLOW);
534             updateRemoteAclFilterTable(port, NwConstants.DEL_FLOW);
535         }
536         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, NwConstants.DEL_FLOW);
537         return true;
538     }
539
540     @Override
541     public boolean applyAce(AclInterface port, String aclName, Ace ace) {
542         if (!port.isPortSecurityEnabled() || port.getDpId() == null) {
543             return false;
544         }
545         List<FlowEntity> flowEntries = new ArrayList<>();
546         programAceRule(flowEntries, port, aclName, ace, NwConstants.ADD_FLOW);
547         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, NwConstants.ADD_FLOW);
548         return true;
549     }
550
551     @Override
552     public boolean removeAce(AclInterface port, String aclName, Ace ace) {
553         if (!port.isPortSecurityEnabled() || port.getDpId() == null) {
554             return false;
555         }
556         List<FlowEntity> flowEntries = new ArrayList<>();
557         programAceRule(flowEntries, port, aclName, ace, NwConstants.MOD_FLOW);
558         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + port.getInterfaceId(), flowEntries, NwConstants.DEL_FLOW);
559         return true;
560     }
561
562     @Override
563     public void updateRemoteAcl(Acl aclBefore, Acl aclAfter, Collection<AclInterface> portsBefore) {
564         handleRemoteAclUpdate(aclBefore, aclAfter, portsBefore);
565     }
566
567     /**
568      * Bind service.
569      *
570      * @param aclInterface the acl interface
571      */
572     public abstract void bindService(AclInterface aclInterface);
573
574     /**
575      * Unbind service.
576      *
577      * @param aclInterface the acl interface
578      */
579     protected abstract void unbindService(AclInterface aclInterface);
580
581     /**
582      * Programs DHCP Service flows.
583      *
584      * @param flowEntries the flow entries
585      * @param port the acl interface
586      * @param action add/modify/remove action
587      * @param addOrRemove addorRemove
588      */
589     protected abstract void programDhcpService(List<FlowEntity> flowEntries, AclInterface port,
590             Action action, int addOrRemove);
591
592     /**
593      * Programs DHCP service flows.
594      *
595      * @param flowEntries the flow entries
596      * @param port the acl interface
597      * @param allowedAddresses the allowed addresses
598      * @param addOrRemove addorRemove
599      */
600     protected abstract void processDhcpServiceUpdate(List<FlowEntity> flowEntries, AclInterface port,
601             List<AllowedAddressPairs> allowedAddresses, int addOrRemove);
602
603     /**
604      * Programs the anti-spoofing rules.
605      *
606      * @param flowEntries the flow entries
607      * @param port the acl interface
608      * @param allowedAddresses the allowed addresses
609      * @param action add/modify/remove action
610      * @param addOrRemove addorRemove
611      */
612     protected abstract void programAntiSpoofingRules(List<FlowEntity> flowEntries, AclInterface port,
613             List<AllowedAddressPairs> allowedAddresses, Action action, int addOrRemove);
614
615     /**
616      * Programs broadcast rules.
617      *
618      * @param flowEntries the flow entries
619      * @param port the Acl Interface port
620      * @param addOrRemove whether to delete or add flow
621      */
622     protected abstract void programBroadcastRules(List<FlowEntity> flowEntries, AclInterface port, Action action,
623             int addOrRemove);
624
625     /**
626      * Programs broadcast rules.
627      *
628      * @param flowEntries the flow entries
629      * @param port the Acl Interface port
630      * @param subnetInfoList the port subnet info list
631      * @param addOrRemove whether to delete or add flow
632      */
633     protected abstract void programSubnetBroadcastRules(List<FlowEntity> flowEntries, AclInterface port,
634             List<SubnetInfo> subnetInfoList, int addOrRemove);
635
636     protected abstract void programIcmpv6RARule(List<FlowEntity> flowEntries, AclInterface port,
637             List<SubnetInfo> subnets, int addOrRemove);
638
639     /**
640      * Add Flow to list.
641      *
642      * @param dpId
643      *            the dpId
644      * @param tableId
645      *            the tableId
646      * @param flowId
647      *            the flowId
648      * @param priority
649      *            the priority
650      * @param idleTimeOut
651      *            the idle timeout
652      * @param hardTimeOut
653      *            the hard timeout
654      * @param cookie
655      *            the cookie
656      * @param matches
657      *            the list of matches to be writted
658      * @param instructions
659      *            the list of instruction to be written.
660      * @param addOrRemove
661      *            add or remove the entries.
662      */
663     protected void addFlowEntryToList(List<FlowEntity> flowEntries, Uint64 dpId, short tableId, String flowId,
664             int priority, int idleTimeOut, int hardTimeOut, Uint64 cookie, List<? extends MatchInfoBase> matches,
665             List<InstructionInfo> instructions, int addOrRemove) {
666         List<InstructionInfo> instructionInfos = null;
667         if (addOrRemove == NwConstants.ADD_FLOW) {
668             instructionInfos = instructions;
669         }
670         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, tableId, flowId, priority,
671                 flowId, idleTimeOut, hardTimeOut, cookie, matches, instructionInfos);
672         LOG.trace("Adding flow to list: DpnId {}, flowId {}", dpId, flowId);
673         flowEntries.add(flowEntity);
674     }
675
676     protected void programFlows(String jobName, List<FlowEntity> flowEntries, int addOrRemove) {
677         List<List<FlowEntity>> flowEntityParts = Lists.partition(flowEntries, AclConstants.FLOWS_PER_TRANSACTION);
678         for (List<FlowEntity> part : flowEntityParts) {
679             jobCoordinator.enqueueJob(jobName,
680                 () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
681                     tx -> {
682                         if (addOrRemove == NwConstants.ADD_FLOW) {
683                             for (FlowEntity flowEntity: part) {
684                                 mdsalManager.addFlow(tx, flowEntity);
685                             }
686                         } else {
687                             for (FlowEntity flowEntity: part) {
688                                 mdsalManager.removeFlow(tx, flowEntity);
689                             }
690                         }
691                     })), AclConstants.JOB_MAX_RETRIES);
692         }
693     }
694
695     protected List<InstructionInfo> getDispatcherTableResubmitInstructions() {
696         return getDispatcherTableResubmitInstructions(new ArrayList<>());
697     }
698
699     /**
700      * Gets the dispatcher table resubmit instructions based on ingress/egress service mode w.r.t switch.
701      *
702      * @param actionsInfos
703      *            the actions infos
704      * @return the instructions for dispatcher table resubmit
705      */
706     protected List<InstructionInfo> getDispatcherTableResubmitInstructions(List<ActionInfo> actionsInfos) {
707         short dispatcherTableId = NwConstants.LPORT_DISPATCHER_TABLE;
708         if (ServiceModeEgress.class.equals(this.serviceMode)) {
709             dispatcherTableId = NwConstants.EGRESS_LPORT_DISPATCHER_TABLE;
710         }
711
712         List<InstructionInfo> instructions = new ArrayList<>();
713         actionsInfos.add(new ActionNxResubmit(dispatcherTableId));
714         instructions.add(new InstructionApplyActions(actionsInfos));
715         return instructions;
716     }
717
718     protected void handleRemoteAclUpdate(Acl aclBefore, Acl aclAfter, Collection<AclInterface> portsBefore) {
719         String aclName = aclAfter.getAclName();
720         Collection<AclInterface> interfaceList = aclDataUtil.getInterfaceList(new Uuid(aclName));
721         if (interfaceList.isEmpty()) {
722             LOG.trace("handleRemoteAclUpdate: No interfaces found with ACL={}", aclName);
723             return;
724         }
725         Set<Uuid> remoteAclsBefore = AclServiceUtils.getRemoteAclIdsByDirection(aclBefore, this.direction);
726         Set<Uuid> remoteAclsAfter = AclServiceUtils.getRemoteAclIdsByDirection(aclAfter, this.direction);
727
728         Set<Uuid> remoteAclsAdded = new HashSet<>(remoteAclsAfter);
729         remoteAclsAdded.removeAll(remoteAclsBefore);
730
731         Set<Uuid> remoteAclsDeleted = new HashSet<>(remoteAclsBefore);
732         remoteAclsDeleted.removeAll(remoteAclsAfter);
733
734         List<FlowEntity> addFlowEntries = new ArrayList<>();
735         List<FlowEntity> deleteFlowEntries = new ArrayList<>();
736         if (!remoteAclsAdded.isEmpty() || !remoteAclsDeleted.isEmpty()) {
737             // delete and add flows in ACL dispatcher table for all applicable ports
738             for (AclInterface portBefore : portsBefore) {
739                 if (portBefore.getDpId() != null) {
740                     programAclDispatcherTable(deleteFlowEntries, portBefore, NwConstants.DEL_FLOW);
741                 } else {
742                     LOG.debug("Skip ACL dispatcher table update as DP ID for interface {} is not present.",
743                             portBefore.getInterfaceId());
744                 }
745             }
746             for (AclInterface port : interfaceList) {
747                 programAclDispatcherTable(addFlowEntries, port, NwConstants.ADD_FLOW);
748             }
749         }
750         Set<BigInteger> dpns = interfaceList.stream().filter(port -> {
751             if (port.getDpId() == null) {
752                 LOG.debug("Skip remote ACL table update as DP ID for interface {} is not present.",
753                         port.getInterfaceId());
754                 return false;
755             }
756             return true;
757         }).map(AclInterface::getDpId).collect(Collectors.toSet());
758
759         programRemoteAclTable(deleteFlowEntries, aclName, remoteAclsDeleted, dpns, NwConstants.DEL_FLOW);
760         programRemoteAclTable(addFlowEntries, aclName, remoteAclsAdded, dpns, NwConstants.ADD_FLOW);
761
762         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + aclName, deleteFlowEntries, NwConstants.DEL_FLOW);
763         programFlows(AclConstants.ACL_JOB_KEY_PREFIX + aclName, addFlowEntries, NwConstants.ADD_FLOW);
764     }
765
766     private void programRemoteAclTable(List<FlowEntity> flowEntries, String aclName, Set<Uuid> remoteAclIds,
767             Set<BigInteger> dpns, int addOrRemove) {
768         for (Uuid remoteAclId : remoteAclIds) {
769             Collection<AclInterface> remoteAclInterfaces = aclDataUtil.getInterfaceList(remoteAclId);
770             if (remoteAclInterfaces.isEmpty()) {
771                 continue;
772             }
773             Set<AllowedAddressPairs> aaps =
774                     remoteAclInterfaces.stream().map(AclInterface::getAllowedAddressPairs).flatMap(List::stream)
775                             .filter(AclServiceUtils::isNotIpAllNetwork).collect(Collectors.toSet());
776
777             Integer aclTag = aclServiceUtils.getAclTag(remoteAclId);
778             if (addOrRemove == NwConstants.ADD_FLOW) {
779                 for (BigInteger dpn : dpns) {
780                     for (AllowedAddressPairs aap : aaps) {
781                         programRemoteAclTableFlow(flowEntries, Uint64.valueOf(dpn), aclTag, aap, addOrRemove);
782                     }
783                 }
784             } else if (addOrRemove == NwConstants.DEL_FLOW) {
785                 Set<BigInteger> remoteAclDpns = new HashSet<>();
786                 Map<String, Set<AclInterface>> mapAclWithPortSet =
787                         aclDataUtil.getRemoteAclInterfaces(remoteAclId, this.direction);
788                 if (mapAclWithPortSet != null) {
789                     Map<String, Set<AclInterface>> copyOfMapAclWithPortSet = new HashMap<>(mapAclWithPortSet);
790                     copyOfMapAclWithPortSet.remove(aclName);
791                     remoteAclDpns = collectDpns(copyOfMapAclWithPortSet);
792                 }
793                 Set<BigInteger> dpnsToOperate = new HashSet<>(dpns);
794                 dpnsToOperate.removeAll(remoteAclDpns);
795                 LOG.debug(
796                         "Deleting flows in Remote ACL table for remoteAclId={}, direction={}, dpnsToOperate={}, "
797                                 + "remoteAclDpns={}, dpns={}",
798                         remoteAclId.getValue(), directionString, dpnsToOperate, remoteAclDpns, dpns);
799
800                 for (BigInteger dpn : dpnsToOperate) {
801                     for (AllowedAddressPairs aap : aaps) {
802                         programRemoteAclTableFlow(flowEntries, Uint64.valueOf(dpn), aclTag, aap, addOrRemove);
803                     }
804                 }
805             }
806         }
807     }
808
809     private void updateRemoteAclFilterTable(AclInterface port, int addOrRemove) {
810         updateRemoteAclFilterTable(port, port.getSecurityGroups(), port.getAllowedAddressPairs(), addOrRemove);
811     }
812
813     private void updateRemoteAclFilterTable(AclInterface port, List<Uuid> aclList, List<AllowedAddressPairs> aaps,
814             int addOrRemove) {
815         if (aclList == null) {
816             LOG.debug("Port {} without SGs", port.getInterfaceId());
817             return;
818         }
819         String portId = port.getInterfaceId();
820         LOG.trace("updateRemoteAclFilterTable for portId={}, aclList={}, aaps={}, addOrRemove={}", portId, aclList,
821                 aaps, addOrRemove);
822
823         ConcurrentMap<Uuid, Map<String, Set<AclInterface>>> mapOfAclWithInterfacesList =
824                 aclDataUtil.getRemoteAclInterfaces(aclList, this.direction);
825         for (Uuid aclId : aclList) {
826             Map<String, Set<AclInterface>> mapAclWithPortSet = mapOfAclWithInterfacesList.get(aclId);
827             if (aclDataUtil.getRemoteAcl(aclId, this.direction) != null) {
828                 Integer aclTag = aclServiceUtils.getAclTag(aclId);
829                 jobCoordinator.enqueueJob(aclId.getValue().intern(), () -> {
830                     List<FlowEntity> flowEntries = new ArrayList<>();
831                     syncRemoteAclTable(flowEntries, portId, aclId, aclTag, aaps, mapAclWithPortSet, addOrRemove);
832                     programFlows(AclConstants.ACL_JOB_KEY_PREFIX + aclId.getValue(), flowEntries, addOrRemove);
833                     return Collections.emptyList();
834                 });
835             }
836         }
837         Set<Uuid> remoteAclIds = aclServiceUtils.getRemoteAclIdsByDirection(aclList, direction);
838         for (Uuid remoteAclId : remoteAclIds) {
839             List<Uuid> aclIds = new ArrayList<Uuid>(port.getSecurityGroups());
840             aclIds.removeAll(aclList);
841             if (addOrRemove == NwConstants.DEL_FLOW && aclServiceUtils.doesRemoteAclIdExistsInAcls(aclIds, remoteAclId,
842                     this.direction)) {
843                 LOG.debug("Skipping delete as remoteAclId {} is used with other ACE configured with port {}",
844                         remoteAclId, portId);
845                 return;
846             }
847             List<FlowEntity> flowEntries = new ArrayList<>();
848             syncRemoteAclTableFromOtherDpns(flowEntries, port, remoteAclId, addOrRemove);
849             programFlows(AclConstants.ACL_JOB_KEY_PREFIX + remoteAclId.getValue(), flowEntries, addOrRemove);
850         }
851     }
852
853     private void syncRemoteAclTable(List<FlowEntity> flowEntries, String portId, Uuid acl, Integer aclTag,
854             List<AllowedAddressPairs> aaps, Map<String, Set<AclInterface>> mapAclWithPortSet, int addOrRemove) {
855         Set<BigInteger> dpns = collectDpns(mapAclWithPortSet);
856         for (AllowedAddressPairs aap : aaps) {
857             if (!AclServiceUtils.isNotIpAllNetwork(aap)) {
858                 continue;
859             }
860             if (aclServiceUtils.skipDeleteInCaseOfOverlappingIP(portId, acl, aap.getIpAddress(),
861                     addOrRemove)) {
862                 LOG.debug("Skipping delete of IP={} in remote ACL table for remoteAclId={}, portId={}",
863                         aap.getIpAddress(), portId, acl.getValue());
864                 continue;
865             }
866             for (BigInteger dpId : dpns) {
867                 programRemoteAclTableFlow(flowEntries, Uint64.valueOf(dpId), aclTag, aap, addOrRemove);
868             }
869         }
870     }
871
872     private void syncRemoteAclTableFromOtherDpns(List<FlowEntity> flowEntries, AclInterface port, Uuid remoteAclId,
873             int addOrRemove) {
874         Collection<AclInterface> aclInterfaces = aclDataUtil.getInterfaceList(remoteAclId);
875
876         if (!aclInterfaces.isEmpty() && isFirstPortInDpnWithRemoteAclId(port, remoteAclId)) {
877             Integer aclTag = aclServiceUtils.getAclTag(remoteAclId);
878             for (AclInterface aclInterface : aclInterfaces) {
879                 if (port.getInterfaceId().equals(aclInterface.getInterfaceId())) {
880                     continue;
881                 }
882                 for (AllowedAddressPairs aap : aclInterface.getAllowedAddressPairs()) {
883                     if (AclServiceUtils.isNotIpAllNetwork(aap)) {
884                         programRemoteAclTableFlow(flowEntries, Uint64.valueOf(port.getDpId()),
885                             aclTag, aap, addOrRemove);
886                     }
887                 }
888             }
889         }
890     }
891
892     private boolean isFirstPortInDpnWithRemoteAclId(AclInterface port, Uuid remoteAclId) {
893         String portId = port.getInterfaceId();
894         BigInteger dpId = port.getDpId();
895         Map<String, Set<AclInterface>> remoteAclInterfacesMap =
896                 aclDataUtil.getRemoteAclInterfaces(remoteAclId, direction);
897         if (remoteAclInterfacesMap != null) {
898             for (Set<AclInterface> interfaceSet : remoteAclInterfacesMap.values()) {
899                 for (AclInterface aclInterface : interfaceSet) {
900                     if (portId.equals(aclInterface.getInterfaceId())) {
901                         continue;
902                     }
903                     if (dpId.equals(aclInterface.getDpId())) {
904                         return false;
905                     }
906                 }
907             }
908         }
909         return true;
910     }
911
912     protected abstract void programRemoteAclTableFlow(List<FlowEntity> flowEntries, Uint64 dpId, Integer aclTag,
913             AllowedAddressPairs aap, int addOrRemove);
914
915     protected Set<BigInteger> collectDpns(@Nullable Map<String, Set<AclInterface>> mapAclWithPortSet) {
916         Set<BigInteger> dpns = new HashSet<>();
917         if (mapAclWithPortSet == null) {
918             return dpns;
919         }
920         for (Set<AclInterface> innerSet : mapAclWithPortSet.values()) {
921             if (innerSet == null) {
922                 continue;
923             }
924             for (AclInterface inter : innerSet) {
925                 dpns.add(inter.getDpId());
926             }
927         }
928         return dpns;
929     }
930
931     /**
932      * Programs the port specific fixed rules.
933      *
934      * @param flowEntries the flow entries
935      * @param dpId the dp id
936      * @param allowedAddresses the allowed addresses
937      * @param lportTag the lport tag
938      * @param portId the portId
939      * @param action the action
940      * @param write whether to add or remove the flow.
941      */
942     protected void programAclPortSpecificFixedRules(List<FlowEntity> flowEntries, Uint64 dpId,
943             List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int write) {
944         programGotoClassifierTableRules(flowEntries, dpId, allowedAddresses, lportTag, write);
945         if (action == Action.ADD || action == Action.REMOVE) {
946             programConntrackRecircRules(flowEntries, dpId, allowedAddresses, lportTag, portId, write);
947             programPortSpecificDropRules(flowEntries, dpId, lportTag, write);
948             programAclCommitRules(flowEntries, dpId, lportTag, portId, write);
949         }
950         LOG.info("programAclPortSpecificFixedRules: flows for dpId={}, lportId={}, action={}, write={}", dpId, lportTag,
951                 action, write);
952     }
953
954     protected abstract void programGotoClassifierTableRules(List<FlowEntity> flowEntries, Uint64 dpId,
955             List<AllowedAddressPairs> aaps, int lportTag, int addOrRemove);
956
957     /**
958      * Adds the rule to send the packet to the netfilter to check whether it is a known packet.
959      *
960      * @param flowEntries the flow entries
961      * @param dpId the dpId
962      * @param aaps the allowed address pairs
963      * @param lportTag the lport tag
964      * @param portId the portId
965      * @param addOrRemove whether to add or remove the flow
966      */
967     protected void programConntrackRecircRules(List<FlowEntity> flowEntries, Uint64 dpId,
968             List<AllowedAddressPairs> aaps, int lportTag, String portId, int addOrRemove) {
969         if (AclServiceUtils.doesIpv4AddressExists(aaps)) {
970             programConntrackRecircRule(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV4, addOrRemove);
971         }
972         if (AclServiceUtils.doesIpv6AddressExists(aaps)) {
973             programConntrackRecircRule(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV6, addOrRemove);
974         }
975     }
976
977     protected void programConntrackRecircRule(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
978             String portId, MatchEthernetType matchEtherType, int addOrRemove) {
979         List<MatchInfoBase> matches = new ArrayList<>();
980         matches.add(matchEtherType);
981         matches.add(AclServiceUtils.buildLPortTagMatch(lportTag, serviceMode));
982
983         List<InstructionInfo> instructions = new ArrayList<>();
984         if (addOrRemove == NwConstants.ADD_FLOW) {
985             Long elanTag = getElanIdFromAclInterface(portId);
986             if (elanTag == null) {
987                 LOG.error("ElanId not found for portId={}; Context: dpId={}, lportTag={}, addOrRemove={},", portId,
988                         dpId, lportTag, addOrRemove);
989                 return;
990             }
991             List<ActionInfo> actionsInfos = new ArrayList<>();
992             actionsInfos.add(new ActionNxConntrack(2, 0, 0, elanTag.intValue(), getAclForExistingTrafficTable()));
993             instructions.add(new InstructionApplyActions(actionsInfos));
994         }
995
996         String flowName =
997                 this.directionString + "_Fixed_Conntrk_" + dpId + "_"
998                     + lportTag + "_" + matchEtherType + "_Recirc";
999         addFlowEntryToList(flowEntries, dpId, getAclConntrackSenderTable(), flowName,
1000                 AclConstants.ACL_DEFAULT_PRIORITY, 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions,
1001                 addOrRemove);
1002     }
1003
1004     /**
1005      * Adds the rules to drop the unknown/invalid packets .
1006      *
1007      * @param flowEntries the flow entries
1008      * @param dpId the dpId
1009      * @param lportTag the lport tag
1010      * @param addOrRemove whether to add or remove the flow
1011      */
1012     protected void programPortSpecificDropRules(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1013             int addOrRemove) {
1014         LOG.debug("Programming Drop Rules: DpId={}, lportTag={}, addOrRemove={}", dpId, lportTag, addOrRemove);
1015         programConntrackInvalidDropRule(flowEntries, dpId, lportTag, addOrRemove);
1016         programAclRuleMissDropRule(flowEntries, dpId, lportTag, addOrRemove);
1017     }
1018
1019     /**
1020      * Adds the rule to drop the conntrack invalid packets .
1021      *
1022      * @param flowEntries the flow entries
1023      * @param dpId the dpId
1024      * @param lportTag the lport tag
1025      * @param addOrRemove whether to add or remove the flow
1026      */
1027     protected void programConntrackInvalidDropRule(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1028             int addOrRemove) {
1029         List<MatchInfoBase> matches = AclServiceOFFlowBuilder.addLPortTagMatches(lportTag,
1030                 AclConstants.TRACKED_INV_CT_STATE, AclConstants.TRACKED_INV_CT_STATE_MASK, serviceMode);
1031         List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
1032
1033         String flowId = this.directionString + "_Fixed_Conntrk_Drop" + dpId.toString()
1034                             + "_" + lportTag + "_Tracked_Invalid";
1035         addFlowEntryToList(flowEntries, dpId, getAclFilterCumDispatcherTable(), flowId,
1036                 AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY, 0, 0, AclServiceUtils.getDropFlowCookie(lportTag),
1037                 matches, instructions, addOrRemove);
1038     }
1039
1040     /**
1041      * Program ACL rule miss drop rule for a port.
1042      *
1043      * @param flowEntries the flow entries
1044      * @param dpId the dp id
1045      * @param lportTag the lport tag
1046      * @param addOrRemove the add or remove
1047      */
1048     protected void programAclRuleMissDropRule(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1049             int addOrRemove) {
1050         List<MatchInfoBase> matches = new ArrayList<>();
1051         matches.add(AclServiceUtils.buildLPortTagMatch(lportTag, serviceMode));
1052         List<InstructionInfo> instructions = AclServiceOFFlowBuilder.getDropInstructionInfo();
1053
1054         String flowId = this.directionString + "_Fixed_Acl_Rule_Miss_Drop_" + dpId.toString() + "_" + lportTag;
1055         addFlowEntryToList(flowEntries, dpId, getAclFilterCumDispatcherTable(), flowId,
1056                 AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY, 0, 0, AclServiceUtils.getDropFlowCookie(lportTag),
1057                 matches, instructions, addOrRemove);
1058     }
1059
1060     /**
1061      * Program acl commit rules.
1062      *
1063      * @param flowEntries the flow entries
1064      * @param dpId the dp id
1065      * @param lportTag the lport tag
1066      * @param portId the port id
1067      * @param addOrRemove the add or remove
1068      */
1069     protected void programAclCommitRules(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag, String portId,
1070             int addOrRemove) {
1071         programAclCommitRuleForConntrack(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV4, addOrRemove);
1072         programAclCommitRuleForConntrack(flowEntries, dpId, lportTag, portId, MatchEthernetType.IPV6, addOrRemove);
1073         programAclCommitRuleForNonConntrack(flowEntries, dpId, lportTag, addOrRemove);
1074     }
1075
1076     /**
1077      * Program acl commit rule for conntrack.
1078      *
1079      * @param flowEntries the flow entries
1080      * @param dpId the dp id
1081      * @param lportTag the lport tag
1082      * @param portId the port id
1083      * @param matchEtherType the match ether type
1084      * @param addOrRemove the add or remove
1085      */
1086     protected void programAclCommitRuleForConntrack(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1087             String portId, MatchEthernetType matchEtherType, int addOrRemove) {
1088         List<MatchInfoBase> matches = new ArrayList<>();
1089         matches.add(matchEtherType);
1090         matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndConntrackClassifierType(lportTag,
1091                 AclConntrackClassifierType.CONNTRACK_SUPPORTED, serviceMode));
1092
1093         List<ActionInfo> actionsInfos = new ArrayList<>();
1094         if (addOrRemove == NwConstants.ADD_FLOW) {
1095             Long elanId = getElanIdFromAclInterface(portId);
1096             if (elanId == null) {
1097                 LOG.error("ElanId not found for portId={}; Context: dpId={}, lportTag={}, addOrRemove={}", portId, dpId,
1098                         lportTag, addOrRemove);
1099                 return;
1100             }
1101             List<NxCtAction> ctActionsList =
1102                     Lists.newArrayList(new ActionNxConntrack.NxCtMark(AclConstants.CT_MARK_EST_STATE));
1103             actionsInfos.add(new ActionNxConntrack(2, 1, 0, elanId.intValue(), (short) 255, ctActionsList));
1104             actionsInfos.add(new ActionNxCtClear());
1105         }
1106         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
1107
1108         String flowName = directionString + "_Acl_Commit_Conntrack_" + dpId.toString()
1109                             + "_" + lportTag + "_" + matchEtherType;
1110         // Flow for conntrack traffic to commit and resubmit to dispatcher
1111         addFlowEntryToList(flowEntries, dpId, getAclCommitterTable(), flowName, AclConstants.ACL_DEFAULT_PRIORITY,
1112                 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
1113     }
1114
1115     /**
1116      * Program acl commit rule for non conntrack.
1117      *
1118      * @param flowEntries the flow entries
1119      * @param dpId the dp id
1120      * @param lportTag the lport tag
1121      * @param addOrRemove the add or remove
1122      */
1123     protected void programAclCommitRuleForNonConntrack(List<FlowEntity> flowEntries, Uint64 dpId, int lportTag,
1124             int addOrRemove) {
1125         List<MatchInfoBase> matches = new ArrayList<>();
1126         matches.addAll(AclServiceUtils.buildMatchesForLPortTagAndConntrackClassifierType(lportTag,
1127                 AclConntrackClassifierType.NON_CONNTRACK_SUPPORTED, serviceMode));
1128
1129         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions();
1130         String flowName = this.directionString + "_Acl_Commit_Non_Conntrack_" + dpId + "_" + lportTag;
1131         // Flow for non-conntrack traffic to resubmit to dispatcher
1132         addFlowEntryToList(flowEntries, dpId, getAclCommitterTable(), flowName, AclConstants.ACL_DEFAULT_PRIORITY,
1133                 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
1134     }
1135
1136     @Nullable
1137     protected Long getElanIdFromAclInterface(String elanInterfaceName) {
1138         AclInterface aclInterface = aclInterfaceCache.get(elanInterfaceName);
1139         if (null != aclInterface) {
1140             return aclInterface.getElanId();
1141         }
1142         return null;
1143     }
1144
1145     protected abstract boolean isValidDirection(Class<? extends DirectionBase> direction);
1146
1147     protected abstract short getAclConntrackSenderTable();
1148
1149     protected abstract short getAclForExistingTrafficTable();
1150
1151     protected abstract short getAclFilterCumDispatcherTable();
1152
1153     protected abstract short getAclRuleBasedFilterTable();
1154
1155     protected abstract short getAclRemoteAclTable();
1156
1157     protected abstract short getAclCommitterTable();
1158 }