NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclInterfaceStateListener.java
index 94eda89e860ff9205285ad914ece0b2e398ccd7a..6967a0a2302ab380a004fa24a7df704b53403fc1 100644 (file)
@@ -7,16 +7,18 @@
  */
 package org.opendaylight.netvirt.aclservice.listeners;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.SortedSet;
-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.genius.interfacemanager.interfaces.IInterfaceManager;
+import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
 import org.opendaylight.netvirt.aclservice.api.AclServiceManager;
 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
@@ -24,21 +26,23 @@ 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.netvirt.aclservice.utils.AclServiceUtils;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
+import org.opendaylight.serviceutils.srm.RecoverableListener;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
 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.DirectionEgress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
-        AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface> {
+public class AclInterfaceStateListener extends AbstractAsyncDataTreeChangeListener<Interface>
+        implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
 
@@ -50,12 +54,16 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
     private final IInterfaceManager interfaceManager;
     private final AclInterfaceCache aclInterfaceCache;
     private final AclServiceUtils aclServiceUtils;
+    protected final JobCoordinator jobCoordinator;
 
     @Inject
     public AclInterfaceStateListener(AclServiceManager aclServiceManger, AclClusterUtil aclClusterUtil,
             DataBroker dataBroker, AclDataUtil aclDataUtil, IInterfaceManager interfaceManager,
-            AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils) {
-        super(Interface.class, AclInterfaceStateListener.class);
+            AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils, JobCoordinator jobCoordinator,
+            ServiceRecoveryRegistry serviceRecoveryRegistry) {
+        super(dataBroker, LogicalDatastoreType.OPERATIONAL,
+                InstanceIdentifier.create(InterfacesState.class).child(Interface.class),
+                Executors.newListeningSingleThreadExecutor("AclInterfaceStateListener", LOG));
         this.aclServiceManger = aclServiceManger;
         this.aclClusterUtil = aclClusterUtil;
         this.dataBroker = dataBroker;
@@ -63,22 +71,26 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
         this.interfaceManager = interfaceManager;
         this.aclInterfaceCache = aclInterfaceCache;
         this.aclServiceUtils = aclServicUtils;
+        this.jobCoordinator = jobCoordinator;
+        serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
     }
 
-    @Override
-    @PostConstruct
     public void init() {
         LOG.info("{} start", getClass().getSimpleName());
-        registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
     }
 
     @Override
-    protected InstanceIdentifier<Interface> getWildCardPath() {
-        return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
+    public void registerListener() {
+        super.register();
+    }
+
+    @Override
+    public void deregisterListener() {
+        super.close();
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Interface> key, Interface deleted) {
+    public void remove(InstanceIdentifier<Interface> key, Interface deleted) {
         if (!L2vlan.class.equals(deleted.getType())) {
             return;
         }
@@ -96,13 +108,18 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
                 }
             }
             if (aclList != null) {
-                aclDataUtil.removeAclInterfaceMap(aclList, aclInterface);
+                for (Uuid acl : aclList) {
+                    jobCoordinator.enqueueJob(acl.getValue(), () -> {
+                        aclDataUtil.removeAclInterfaceMap(acl, aclInterface);
+                        return Collections.emptyList();
+                    });
+                }
             }
         }
     }
 
     @Override
-    protected void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
+    public void update(InstanceIdentifier<Interface> key, Interface before, Interface after) {
         /*
          * The update is not of interest as the attributes populated from this listener will not change.
          * The northbound updates are handled in AclInterfaceListener.
@@ -117,39 +134,39 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
     }
 
     @Override
-    protected void add(InstanceIdentifier<Interface> key, Interface added) {
+    public void add(InstanceIdentifier<Interface> key, Interface added) {
         if (!L2vlan.class.equals(added.getType())) {
             return;
         }
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface;
-        iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
-        if (iface == null) {
-            LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
-                    + "for ACL failed", added.getName());
-            return;
-        }
-        InterfaceAcl aclInPort = iface.getAugmentation(InterfaceAcl.class);
-        if (aclInPort == null) {
-            LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
-                    added.getName());
-            return;
-        }
 
+        if (aclInterfaceCache.get(added.getName()) == null) {
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
+                        .Interface iface = interfaceManager.getInterfaceInfoFromConfigDataStore(added.getName());
+            if (iface == null) {
+                LOG.error("No interface with name {} available in interfaceConfig, servicing interfaceState ADD"
+                        + "for ACL failed", added.getName());
+                return;
+            }
+            InterfaceAcl aclInPort = iface.augmentation(InterfaceAcl.class);
+            if (aclInPort == null) {
+                LOG.trace("Interface {} is not an ACL Interface, ignoring ADD interfaceState event",
+                        added.getName());
+                return;
+            }
+            aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
+                builder.portSecurityEnabled(aclInPort.isPortSecurityEnabled())
+                    .interfaceType(aclInPort.getInterfaceType()).securityGroups(aclInPort.getSecurityGroups())
+                    .allowedAddressPairs(aclInPort.getAllowedAddressPairs()).subnetInfo(aclInPort.getSubnetInfo());
+            });
+        }
         AclInterface aclInterface = aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
             builder.dpId(AclServiceUtils.getDpIdFromIterfaceState(added)).lPortTag(added.getIfIndex())
                 .isMarkedForDelete(false);
-
             if (AclServiceUtils.isOfInterest(prevAclInterface)) {
-                if (prevAclInterface.getSubnetIpPrefixes() == null) {
-                    // For upgrades
-                    List<IpPrefixOrAddress> subnetIpPrefixes = AclServiceUtils.getSubnetIpPrefixes(dataBroker,
-                            added.getName());
-                    builder.subnetIpPrefixes(subnetIpPrefixes);
-                }
                 SortedSet<Integer> ingressRemoteAclTags =
-                        aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionIngress.class);
+                        aclServiceUtils.getRemoteAclTags(prevAclInterface.getSecurityGroups(), DirectionIngress.class);
                 SortedSet<Integer> egressRemoteAclTags =
-                        aclServiceUtils.getRemoteAclTags(aclInPort.getSecurityGroups(), DirectionEgress.class);
+                        aclServiceUtils.getRemoteAclTags(prevAclInterface.getSecurityGroups(), DirectionEgress.class);
                 builder.ingressRemoteAclTags(ingressRemoteAclTags).egressRemoteAclTags(egressRemoteAclTags);
             }
         });
@@ -157,7 +174,7 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
         if (AclServiceUtils.isOfInterest(aclInterface)) {
             List<Uuid> aclList = aclInterface.getSecurityGroups();
             if (aclList != null) {
-                aclDataUtil.addAclInterfaceMap(aclList, aclInterface);
+                aclDataUtil.addOrUpdateAclInterfaceMap(aclList, aclInterface);
             }
             if (aclInterface.getElanId() == null) {
                 LOG.debug("On Add event, skip ADD since ElanId is not updated");
@@ -175,7 +192,9 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
     }
 
     @Override
-    protected AclInterfaceStateListener getDataTreeChangeListener() {
-        return AclInterfaceStateListener.this;
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 }