ACL: Support for non-conntrack supported traffic.
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclEventListener.java
index 7f388f7f719a35440ab4b46b3da49a6144d7b9c6..42c7874fbbb2b0e7871fc07c9665a2e087c229a6 100644 (file)
@@ -8,10 +8,10 @@
 package org.opendaylight.netvirt.aclservice.listeners;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
@@ -21,7 +21,9 @@ import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
 import org.opendaylight.netvirt.aclservice.utils.AclClusterUtil;
+import org.opendaylight.netvirt.aclservice.utils.AclConstants;
 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
+import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.AccessLists;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
@@ -36,32 +38,31 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
         ClusteredDataTreeChangeListener<Acl> {
 
     private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
+
     private final AclServiceManager aclServiceManager;
     private final AclClusterUtil aclClusterUtil;
     private final DataBroker dataBroker;
+    private final AclDataUtil aclDataUtil;
+    private final AclServiceUtils aclServiceUtils;
 
     @Inject
-    public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker) {
+    public AclEventListener(AclServiceManager aclServiceManager, AclClusterUtil aclClusterUtil, DataBroker dataBroker,
+            AclDataUtil aclDataUtil, AclServiceUtils aclServicUtils) {
         super(Acl.class, AclEventListener.class);
         this.aclServiceManager = aclServiceManager;
         this.aclClusterUtil = aclClusterUtil;
         this.dataBroker = dataBroker;
+        this.aclDataUtil = aclDataUtil;
+        this.aclServiceUtils = aclServicUtils;
     }
 
+    @Override
     @PostConstruct
-    // TODO new interface Lifecyle
-    public void start() {
+    public void init() {
         LOG.info("{} start", getClass().getSimpleName());
         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
     }
 
-    @Override
-    @PreDestroy
-    // TODO make AsyncDataTreeChangeListenerBase implement new interface Lifecyle
-    public void close() throws Exception {
-        super.close();
-    }
-
     @Override
     protected InstanceIdentifier<Acl> getWildCardPath() {
         return InstanceIdentifier
@@ -71,33 +72,49 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
 
     @Override
     protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
+        if (!AclServiceUtils.isOfAclInterest(acl)) {
+            LOG.trace("{} does not have SecurityRuleAttr augmentation", acl.getAclName());
+            return;
+        }
+
+        LOG.trace("On remove event, remove ACL: {}", acl);
+        this.aclServiceUtils.releaseAclTag(acl.getAclName());
         updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.REMOVE);
     }
 
     @Override
     protected void update(InstanceIdentifier<Acl> key, Acl aclBefore, Acl aclAfter) {
-        List<AclInterface> interfaceList = AclDataUtil.getInterfaceList(new Uuid(aclAfter.getAclName()));
+        if (!AclServiceUtils.isOfAclInterest(aclAfter) && !AclServiceUtils.isOfAclInterest(aclBefore)) {
+            LOG.trace("before {} and after {} does not have SecurityRuleAttr augmentation",
+                    aclBefore.getAclName(), aclAfter.getAclName());
+            return;
+        }
+
+        String aclName = aclAfter.getAclName();
+        Collection<AclInterface> interfaceList = aclDataUtil.getInterfaceList(new Uuid(aclName));
         // find and update added ace rules in acl
         List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
-        updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
+        updateRemoteAclCache(addedAceRules, aclName, AclServiceManager.Action.ADD);
         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
-            updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
+            LOG.debug("On update event, add Ace rules: {} for ACL: {}", addedAceRules, aclName);
+            updateAceRules(interfaceList, aclName, addedAceRules, AclServiceManager.Action.ADD);
         }
         // find and update deleted ace rules in acl
         List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
         if (interfaceList != null && aclClusterUtil.isEntityOwner()) {
-            updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
+            LOG.debug("On update event, remove Ace rules: {} for ACL: {}", deletedAceRules, aclName);
+            updateAceRules(interfaceList, aclName, deletedAceRules, AclServiceManager.Action.REMOVE);
         }
-        updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
-
+        updateRemoteAclCache(deletedAceRules, aclName, AclServiceManager.Action.REMOVE);
     }
 
-    private void updateAceRules(List<AclInterface> interfaceList, List<Ace> aceList, AclServiceManager.Action action) {
+    private void updateAceRules(Collection<AclInterface> interfaceList, String aclName, List<Ace> aceList,
+            AclServiceManager.Action action) {
         if (null != aceList && !aceList.isEmpty()) {
             LOG.trace("update ace rules - action: {} , ace rules: {}", action.name(), aceList);
             for (AclInterface port : interfaceList) {
                 for (Ace aceRule : aceList) {
-                    aclServiceManager.notifyAce(port, action, aceRule);
+                    aclServiceManager.notifyAce(port, action, aclName, aceRule);
                 }
             }
         }
@@ -105,9 +122,28 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
 
     @Override
     protected void add(InstanceIdentifier<Acl> key, Acl acl) {
-        updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
+        String aclName = acl.getAclName();
+        if (!AclServiceUtils.isOfAclInterest(acl)) {
+            LOG.trace("{} does not have SecurityRuleAttr augmentation", aclName);
+            return;
+        }
+
+        LOG.trace("On add event, add ACL: {}", acl);
+        Integer aclTag = this.aclServiceUtils.allocateAclTag(aclName);
+        if (aclTag != null && aclTag != AclConstants.INVALID_ACL_TAG) {
+            this.aclDataUtil.addAclTag(aclName, aclTag);
+        }
+
+        updateRemoteAclCache(acl.getAccessListEntries().getAce(), aclName, AclServiceManager.Action.ADD);
     }
 
+    /**
+     * Update remote acl cache.
+     *
+     * @param aceList the ace list
+     * @param aclName the acl name
+     * @param action the action
+     */
     private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
         if (null == aceList) {
             return;
@@ -116,9 +152,9 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
             SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
             if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
                 if (action == AclServiceManager.Action.ADD) {
-                    AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
+                    aclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
                 } else {
-                    AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
+                    aclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
                 }
             }
         }
@@ -138,7 +174,7 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
             return updatedAceList;
         }
         List<Ace> currentAceList = new ArrayList<>(currentAcl.getAccessListEntries().getAce());
-        for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext(); ) {
+        for (Iterator<Ace> iterator = updatedAceList.iterator(); iterator.hasNext();) {
             Ace ace1 = iterator.next();
             for (Ace ace2 : currentAceList) {
                 if (ace1.getRuleName().equals(ace2.getRuleName())) {