NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / listeners / AclInterfaceStateListener.java
index 929222ef9db5890ec10adde38dab9a58bb705f37..6967a0a2302ab380a004fa24a7df704b53403fc1 100644 (file)
@@ -10,15 +10,15 @@ 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;
@@ -28,6 +28,7 @@ import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
 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;
@@ -40,8 +41,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<Interface,
-        AclInterfaceStateListener> implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
+public class AclInterfaceStateListener extends AbstractAsyncDataTreeChangeListener<Interface>
+        implements ClusteredDataTreeChangeListener<Interface>, RecoverableListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceStateListener.class);
 
@@ -60,7 +61,9 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
             DataBroker dataBroker, AclDataUtil aclDataUtil, IInterfaceManager interfaceManager,
             AclInterfaceCache aclInterfaceCache, AclServiceUtils aclServicUtils, JobCoordinator jobCoordinator,
             ServiceRecoveryRegistry serviceRecoveryRegistry) {
-        super(Interface.class, AclInterfaceStateListener.class);
+        super(dataBroker, LogicalDatastoreType.OPERATIONAL,
+                InstanceIdentifier.create(InterfacesState.class).child(Interface.class),
+                Executors.newListeningSingleThreadExecutor("AclInterfaceStateListener", LOG));
         this.aclServiceManger = aclServiceManger;
         this.aclClusterUtil = aclClusterUtil;
         this.dataBroker = dataBroker;
@@ -72,25 +75,22 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
         serviceRecoveryRegistry.addRecoverableListener(AclServiceUtils.getRecoverServiceRegistryKey(), this);
     }
 
-    @Override
-    @PostConstruct
     public void init() {
         LOG.info("{} start", getClass().getSimpleName());
-        registerListener();
     }
 
     @Override
     public void registerListener() {
-        registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
+        super.register();
     }
 
     @Override
-    protected InstanceIdentifier<Interface> getWildCardPath() {
-        return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
+    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;
         }
@@ -109,7 +109,7 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
             }
             if (aclList != null) {
                 for (Uuid acl : aclList) {
-                    jobCoordinator.enqueueJob(acl.getValue().intern(), () -> {
+                    jobCoordinator.enqueueJob(acl.getValue(), () -> {
                         aclDataUtil.removeAclInterfaceMap(acl, aclInterface);
                         return Collections.emptyList();
                     });
@@ -119,7 +119,7 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
     }
 
     @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.
@@ -134,7 +134,7 @@ 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;
         }
@@ -155,7 +155,7 @@ public class AclInterfaceStateListener extends AsyncDataTreeChangeListenerBase<I
             }
             aclInterfaceCache.addOrUpdate(added.getName(), (prevAclInterface, builder) -> {
                 builder.portSecurityEnabled(aclInPort.isPortSecurityEnabled())
-                    .securityGroups(aclInPort.getSecurityGroups())
+                    .interfaceType(aclInPort.getInterfaceType()).securityGroups(aclInPort.getSecurityGroups())
                     .allowedAddressPairs(aclInPort.getAllowedAddressPairs()).subnetInfo(aclInPort.getSubnetInfo());
             });
         }
@@ -174,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");
@@ -192,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());
     }
 }