Migrate ListenableFutures.addErrorLogging() users
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NatRouterInterfaceListener.java
index 19b043d2e165b7e719e621b801b7d7b9f721c124..3473f3245271aca7ae40ded3892dcce2dbafe366 100644 (file)
@@ -10,19 +10,19 @@ package org.opendaylight.netvirt.natservice.internal;
 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
 
-import java.math.BigInteger;
 import java.util.concurrent.locks.ReentrantLock;
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-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.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
-import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.router.interfaces.RouterInterfaceBuilder;
@@ -32,12 +32,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class NatRouterInterfaceListener
-    extends AsyncDataTreeChangeListenerBase<Interfaces, NatRouterInterfaceListener> {
+public class NatRouterInterfaceListener extends AbstractAsyncDataTreeChangeListener<Interfaces> {
 
     private static final Logger LOG = LoggerFactory.getLogger(NatRouterInterfaceListener.class);
     private final DataBroker dataBroker;
@@ -51,7 +51,9 @@ public class NatRouterInterfaceListener
     public NatRouterInterfaceListener(final DataBroker dataBroker, final OdlInterfaceRpcService interfaceManager,
         final IMdsalApiManager mdsalManager,final NaptManager naptManager,
         final NeutronvpnService neutronvpnService) {
-        super(Interfaces.class, NatRouterInterfaceListener.class);
+        super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(RouterInterfacesMap.class)
+                .child(RouterInterfaces.class).child(Interfaces.class),
+                Executors.newListeningSingleThreadExecutor("NatRouterInterfaceListener", LOG));
         this.dataBroker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.interfaceManager = interfaceManager;
@@ -60,32 +62,28 @@ public class NatRouterInterfaceListener
         this.neutronVpnService = neutronvpnService;
     }
 
-    @Override
-    @PostConstruct
     public void init() {
         LOG.info("{} init", getClass().getSimpleName());
-        registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
-    }
-
-    @Override
-    protected NatRouterInterfaceListener getDataTreeChangeListener() {
-        return NatRouterInterfaceListener.this;
     }
 
     @Override
-    protected InstanceIdentifier<Interfaces> getWildCardPath() {
-        return InstanceIdentifier.create(RouterInterfacesMap.class)
-            .child(RouterInterfaces.class).child(Interfaces.class);
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 
     @Override
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    protected void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
+    public void add(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
         LOG.trace("add : Add event - key: {}, value: {}", interfaceInfo.key(), interfaceInfo);
         final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();
         final String interfaceName = interfaceInfo.getInterfaceId();
-
+        if (NatUtil.isRouterInterfacePort(dataBroker, interfaceName)) {
+            LOG.info("ADD: Ignoring Router Interface Port {} for processing of router {}", interfaceName, routerId);
+            return;
+        }
         try {
             MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION,
                 NatUtil.getRouterInterfaceId(interfaceName), getRouterInterface(interfaceName, routerId));
@@ -96,8 +94,8 @@ public class NatRouterInterfaceListener
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
             .state.Interface interfaceState = NatUtil.getInterfaceStateFromOperDS(dataBroker, interfaceName);
         if (interfaceState != null) {
-            BigInteger dpId = NatUtil.getDpnForInterface(interfaceManager, interfaceName);
-            if (dpId.equals(BigInteger.ZERO)) {
+            Uint64 dpId = NatUtil.getDpIdFromInterface(interfaceState);
+            if (dpId.equals(Uint64.ZERO)) {
                 LOG.warn("ADD : Could not retrieve dp id for interface {} to handle router {} association model",
                         interfaceName, routerId);
                 return;
@@ -105,7 +103,7 @@ public class NatRouterInterfaceListener
             final ReentrantLock lock = NatUtil.lockForNat(dpId);
             lock.lock();
             try {
-                ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
+                LoggingFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL,
                     operTx -> {
                         NatUtil.addToNeutronRouterDpnsMap(routerId, interfaceName, dpId, operTx);
                         NatUtil.addToDpnRoutersMap(routerId, interfaceName, dpId, operTx);
@@ -113,6 +111,7 @@ public class NatRouterInterfaceListener
             } finally {
                 lock.unlock();
             }
+            LOG.info("ADD: Added neutron-router-dpns mapping for interface {} of router {}", interfaceName, routerId);
         } else {
             LOG.info("add : Interface {} not yet operational to handle router interface add event in router {}",
                     interfaceName, routerId);
@@ -120,43 +119,50 @@ public class NatRouterInterfaceListener
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
+    public void remove(InstanceIdentifier<Interfaces> identifier, Interfaces interfaceInfo) {
         LOG.trace("remove : Remove event - key: {}, value: {}", interfaceInfo.key(), interfaceInfo);
         final String routerId = identifier.firstKeyOf(RouterInterfaces.class).getRouterId().getValue();
         final String interfaceName = interfaceInfo.getInterfaceId();
-
-        //Delete the RouterInterfaces maintained in the ODL:L3VPN configuration model
-        ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
-            confTx -> confTx.delete(NatUtil.getRouterInterfaceId(interfaceName))), LOG,
-            "Error handling NAT router interface removal");
-
-        BigInteger dpId = NatUtil.getDpnForInterface(interfaceManager, interfaceName);
-        if (dpId.equals(BigInteger.ZERO)) {
-            LOG.warn("REMOVE : Could not retrieve DPN ID for interface {} to handle router {} dissociation model",
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface
+            interfaceState = NatUtil.getInterfaceStateFromOperDS(dataBroker, interfaceName);
+        if (interfaceState != null) {
+            Uint64 dpId = NatUtil.getDpIdFromInterface(interfaceState);
+            if (dpId.equals(Uint64.ZERO)) {
+                LOG.warn(
+                    "REMOVE : Could not retrieve DPN ID for interface {} to handle router {} dissociation model",
                     interfaceName, routerId);
-            return;
-        }
+                return;
+            }
 
-        final ReentrantLock lock = NatUtil.lockForNat(dpId);
-        lock.lock();
-        try {
-            if (NatUtil.isSnatEnabledForRouterId(dataBroker, routerId)) {
-                NatUtil.removeSnatEntriesForPort(dataBroker, naptManager, mdsalManager, neutronVpnService,
-                    interfaceName, routerId);
+            final ReentrantLock lock = NatUtil.lockForNat(dpId);
+            lock.lock();
+            try {
+                if (NatUtil.isSnatEnabledForRouterId(dataBroker, routerId)) {
+                    NatUtil.removeSnatEntriesForPort(dataBroker, naptManager, mdsalManager, neutronVpnService,
+                        interfaceName, routerId);
+                }
+                LoggingFutures.addErrorLogging(
+                    txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, operTx -> {
+                        //Delete the NeutronRouterDpnMap from the ODL:L3VPN operational model
+                        NatUtil
+                            .removeFromNeutronRouterDpnsMap(routerId, interfaceName, dpId, operTx);
+                        //Delete the DpnRouterMap from the ODL:L3VPN operational model
+                        NatUtil.removeFromDpnRoutersMap(dataBroker, routerId, interfaceName, dpId,
+                            interfaceManager, operTx);
+                    }), LOG, "Error handling NAT router interface removal");
+                //Delete the RouterInterfaces maintained in the ODL:L3VPN configuration model
+                LoggingFutures.addErrorLogging(
+                    txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                        confTx -> confTx.delete(NatUtil.getRouterInterfaceId(interfaceName))), LOG,
+                        "Error handling NAT router interface removal");
+            } finally {
+                lock.unlock();
             }
-            ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(OPERATIONAL, operTx -> {
-                //Delete the NeutronRouterDpnMap from the ODL:L3VPN operational model
-                NatUtil.removeFromNeutronRouterDpnsMap(routerId, interfaceName, dpId, operTx);
-                //Delete the DpnRouterMap from the ODL:L3VPN operational model
-                NatUtil.removeFromDpnRoutersMap(dataBroker, routerId, interfaceName, dpId, interfaceManager, operTx);
-            }), LOG, "Error handling NAT router interface removal");
-        } finally {
-            lock.unlock();
         }
     }
 
     @Override
-    protected void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {
+    public void update(InstanceIdentifier<Interfaces> identifier, Interfaces original, Interfaces update) {
         LOG.trace("update key: {}, original: {}, update: {}", update.key(), original, update);
     }
 
@@ -165,3 +171,4 @@ public class NatRouterInterfaceListener
             .setInterfaceName(interfaceName).setRouterName(routerName).build();
     }
 }
+