Merge "Reduce IPv6 Periodic Unsolicited RA timer interval"
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclEventListener.java
index c91d84eac3fa7b441b6c1871cabc7cb63fb36053..cf283904ca3a7aa5b312b57d0bb2be5c9195fb55 100644 (file)
@@ -10,29 +10,54 @@ package org.opendaylight.netvirt.aclservice.listeners;
 import java.util.ArrayList;
 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;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 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.AclDataUtil;
 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;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Singleton
 public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEventListener> implements
         ClusteredDataTreeChangeListener<Acl> {
 
     private static final Logger LOG = LoggerFactory.getLogger(AclEventListener.class);
-    private AclServiceManager aclServiceManager;
+    private final AclServiceManager aclServiceManager;
+    private final DataBroker dataBroker;
 
-    public AclEventListener(final AclServiceManager aclServiceManager) {
+    @Inject
+    public AclEventListener(final AclServiceManager aclServiceManager, DataBroker dataBroker) {
         super(Acl.class, AclEventListener.class);
         this.aclServiceManager = aclServiceManager;
+        this.dataBroker = dataBroker;
+    }
+
+    @PostConstruct
+    // TODO new interface Lifecyle
+    public void start() {
+        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
@@ -43,23 +68,25 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Acl> key, Acl accessListEntry) {
-        // no need to handle here as Acl will be removed from AclInterfaceListener
+    protected void remove(InstanceIdentifier<Acl> key, Acl acl) {
+        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 (interfaceList == null || interfaceList.isEmpty()) {
-            LOG.debug("acl {} is not associated with any interface.", aclAfter.getAclName());
-            return;
-        }
         // find and update added ace rules in acl
         List<Ace> addedAceRules = getChangedAceList(aclAfter, aclBefore);
-        updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
+        updateRemoteAclCache(addedAceRules, aclAfter.getAclName(), AclServiceManager.Action.ADD);
+        if (interfaceList != null && AclClusterUtil.isEntityOwner()) {
+            updateAceRules(interfaceList, addedAceRules, AclServiceManager.Action.ADD);
+        }
         // find and update deleted ace rules in acl
         List<Ace> deletedAceRules = getChangedAceList(aclBefore, aclAfter);
-        updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
+        if (interfaceList != null && AclClusterUtil.isEntityOwner()) {
+            updateAceRules(interfaceList, deletedAceRules, AclServiceManager.Action.REMOVE);
+        }
+        updateRemoteAclCache(deletedAceRules, aclAfter.getAclName(), AclServiceManager.Action.REMOVE);
 
     }
 
@@ -75,8 +102,24 @@ public class AclEventListener extends AsyncDataTreeChangeListenerBase<Acl, AclEv
     }
 
     @Override
-    protected void add(InstanceIdentifier<Acl> key, Acl dataObjectModification) {
-        // no need to handle here as Acl will be added from AclInterfaceListener
+    protected void add(InstanceIdentifier<Acl> key, Acl acl) {
+        updateRemoteAclCache(acl.getAccessListEntries().getAce(), acl.getAclName(), AclServiceManager.Action.ADD);
+    }
+
+    private void updateRemoteAclCache(List<Ace> aceList, String aclName, AclServiceManager.Action action) {
+        if (null == aceList) {
+            return;
+        }
+        for (Ace ace : aceList) {
+            SecurityRuleAttr aceAttributes = ace.getAugmentation(SecurityRuleAttr.class);
+            if (aceAttributes != null && aceAttributes.getRemoteGroupId() != null) {
+                if (action == AclServiceManager.Action.ADD) {
+                    AclDataUtil.addRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
+                } else {
+                    AclDataUtil.removeRemoteAclId(aceAttributes.getRemoteGroupId(), new Uuid(aclName));
+                }
+            }
+        }
     }
 
     @Override