OPNFLWPLUG-972 : Point to openflowplugin liblldp
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / RouterDpnChangeListener.java
index f3777d08db91cfbfae3aff39ba6ef527b729c435..eeeef374138886dfdfee761bf287913707c69f71 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.netvirt.natservice.internal;
 
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.ListenableFuture;
-
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -19,11 +18,9 @@ import java.util.Map;
 import javax.annotation.PostConstruct;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.mdsalutil.BucketInfo;
@@ -31,6 +28,7 @@ import org.opendaylight.genius.mdsalutil.FlowEntity;
 import org.opendaylight.genius.mdsalutil.GroupEntity;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
 import org.opendaylight.netvirt.elanmanager.api.IElanService;
 import org.opendaylight.netvirt.natservice.api.SnatServiceManager;
 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
@@ -40,22 +38,17 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Group
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.NeutronRouterDpns;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.DpnVpninterfacesList;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.router.dpn.list.dpn.vpninterfaces.list.RouterInterfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
 public class RouterDpnChangeListener
-        extends AsyncDataTreeChangeListenerBase<DpnVpninterfacesList, RouterDpnChangeListener>
-        implements AutoCloseable {
+        extends AsyncDataTreeChangeListenerBase<DpnVpninterfacesList, RouterDpnChangeListener> {
 
     private static final Logger LOG = LoggerFactory.getLogger(RouterDpnChangeListener.class);
     private final DataBroker dataBroker;
@@ -66,8 +59,9 @@ public class RouterDpnChangeListener
     private final INeutronVpnManager nvpnManager;
     private final ExternalNetworkGroupInstaller extNetGroupInstaller;
     private final IElanService elanManager;
-    private SnatServiceManager natServiceManager;
-    private NatMode natMode = NatMode.Controller;
+    private final JobCoordinator coordinator;
+    private final SnatServiceManager natServiceManager;
+    private final NatMode natMode;
 
     @Inject
     public RouterDpnChangeListener(final DataBroker dataBroker, final IMdsalApiManager mdsalManager,
@@ -78,7 +72,8 @@ public class RouterDpnChangeListener
                                    final INeutronVpnManager nvpnManager,
                                    final SnatServiceManager natServiceManager,
                                    final NatserviceConfig config,
-                                   final IElanService elanManager) {
+                                   final IElanService elanManager,
+                                   final JobCoordinator coordinator) {
         super(DpnVpninterfacesList.class, RouterDpnChangeListener.class);
         this.dataBroker = dataBroker;
         this.mdsalManager = mdsalManager;
@@ -89,9 +84,8 @@ public class RouterDpnChangeListener
         this.nvpnManager = nvpnManager;
         this.elanManager = elanManager;
         this.natServiceManager = natServiceManager;
-        if (config != null) {
-            this.natMode = config.getNatMode();
-        }
+        this.coordinator = coordinator;
+        this.natMode = config != null ? config.getNatMode() : NatMode.Controller;
     }
 
     @Override
@@ -115,10 +109,10 @@ public class RouterDpnChangeListener
     @Override
     protected void add(final InstanceIdentifier<DpnVpninterfacesList> identifier, final DpnVpninterfacesList dpnInfo) {
         LOG.trace("add : key: {}, value: {}", dpnInfo.getKey(), dpnInfo);
-        final String routerId = identifier.firstKeyOf(RouterDpnList.class).getRouterId();
+        final String routerUuid = identifier.firstKeyOf(RouterDpnList.class).getRouterId();
         BigInteger dpnId = dpnInfo.getDpnId();
         //check router is associated to external network
-        InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerId);
+        InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerUuid);
         Optional<Routers> routerData =
                 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
                         LogicalDatastoreType.CONFIGURATION, id);
@@ -132,94 +126,104 @@ public class RouterDpnChangeListener
                         LOG.warn("add : NAPT switch is not selected.");
                         return;
                     }
-                    //If it is a router port skip the notify.
-                    for (Uuid subnetUuid :router.getSubnetIds()) {
-                        try {
-                            Optional<Subnetmap> subnetMapEntry =
-                                    SingleTransactionDataBroker.syncReadOptional(dataBroker,
-                                            LogicalDatastoreType.CONFIGURATION, getSubnetMapIdentifier(subnetUuid));
-                            if (subnetMapEntry.isPresent()) {
-                                String routerPortUuid = subnetMapEntry.get().getRouterInterfacePortId().getValue();
-                                List<RouterInterfaces> routerInterfaces = dpnInfo.getRouterInterfaces();
-                                for (RouterInterfaces routerInterface : routerInterfaces) {
-                                    if (routerPortUuid.equals(routerInterface.getInterface())) {
-                                        return;
-                                    }
-                                }
-                            }
-                        } catch (ReadFailedException e) {
-                            LOG.warn("add : The subnet map entry is not present.");
-                            return;
-                        }
-
+                    //If it is for NAPT switch skip as the flows would be already programmed.
+                    if (naptSwitch.equals(dpnId)) {
+                        LOG.debug("Skipping the notification recived for NAPT switch {}", routerUuid);
+                        return;
                     }
                     natServiceManager.notify(router, naptSwitch, dpnId,
                             SnatServiceManager.Action.SNAT_ROUTER_ENBL);
                 } else {
-                    WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
-                    WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
-                    LOG.debug("add : Router {} is associated with ext nw {}", routerId, networkId);
-                    Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerId);
-                    Long vpnId;
-                    if (vpnName == null) {
-                        LOG.debug("add : Internal vpn associated to router {}", routerId);
-                        vpnId = NatUtil.getVpnId(dataBroker, routerId);
-                        if (vpnId == NatConstants.INVALID_ID) {
-                            LOG.error("add : Invalid vpnId returned for routerName {}", routerId);
-                            return;
+                    coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + dpnInfo.getKey(), () -> {
+                        WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
+                        WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
+                        LOG.debug("add : Router {} is associated with ext nw {}", routerUuid, networkId);
+                        Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerUuid);
+                        Long routerId = NatUtil.getVpnId(dataBroker, routerUuid);
+                        List<ListenableFuture<Void>> futures = new ArrayList<>();
+                        if (routerId == NatConstants.INVALID_ID) {
+                            LOG.error("add : Invalid routerId returned for routerName {}", routerUuid);
+                            writeFlowInvTx.cancel();
+                            removeFlowInvTx.cancel();
+                            return futures;
                         }
-                        LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerId);
-                        //Install default entry in FIB to SNAT table
-                        LOG.info("add : Installing default route in FIB on dpn {} for router {} with vpn {}",
-                                dpnId, routerId, vpnId);
-                        installDefaultNatRouteForRouterExternalSubnets(dpnId,
-                                NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
-                        snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, writeFlowInvTx);
-                    } else {
-                        LOG.debug("add : External BGP vpn associated to router {}", routerId);
-                        vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
-                        if (vpnId == NatConstants.INVALID_ID) {
-                            LOG.error("add : Invalid vpnId returned for routerName {}", routerId);
-                            return;
-                        }
-                        Long routId = NatUtil.getVpnId(dataBroker, routerId);
-                        if (routId == NatConstants.INVALID_ID) {
-                            LOG.error("add : Invalid routId returned for routerName {}", routerId);
-                            return;
+                        extNetGroupInstaller.installExtNetGroupEntries(networkId, dpnId);
+                        Long vpnId;
+                        if (vpnName == null) {
+                            LOG.debug("add : Internal vpn associated to router {}", routerUuid);
+                            vpnId = routerId;
+                            if (vpnId == NatConstants.INVALID_ID) {
+                                LOG.error("add : Invalid vpnId returned for routerName {}", routerUuid);
+                                writeFlowInvTx.cancel();
+                                removeFlowInvTx.cancel();
+                                return futures;
+                            }
+                            LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerUuid);
+                            //Install default entry in FIB to SNAT table
+                            LOG.info("add : Installing default route in FIB on dpn {} for router {} with vpn {}",
+                                    dpnId, routerUuid, vpnId);
+                            installDefaultNatRouteForRouterExternalSubnets(dpnId,
+                                    NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
+                            snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, writeFlowInvTx);
+                        } else {
+                            LOG.debug("add : External BGP vpn associated to router {}", routerUuid);
+                            vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
+                            if (vpnId == NatConstants.INVALID_ID) {
+                                LOG.error("add : Invalid vpnId returned for routerName {}", routerUuid);
+                                writeFlowInvTx.cancel();
+                                removeFlowInvTx.cancel();
+                                return futures;
+                            }
+
+                            LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerUuid);
+                            //Install default entry in FIB to SNAT table
+                            LOG.debug("add : Installing default route in FIB on dpn {} for routerId {} with "
+                                    + "vpnId {}...", dpnId, routerUuid, vpnId);
+                            installDefaultNatRouteForRouterExternalSubnets(dpnId,
+                                    NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
+                            snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, routerId, writeFlowInvTx);
                         }
-                        LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerId);
-                        //Install default entry in FIB to SNAT table
-                        LOG.debug("add : Installing default route in FIB on dpn {} for routerId {} with vpnId {}...",
-                                dpnId, routerId, vpnId);
-                        installDefaultNatRouteForRouterExternalSubnets(dpnId,
-                                NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
-                        snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, routId, writeFlowInvTx);
-                    }
-                    extNetGroupInstaller.installExtNetGroupEntries(networkId, dpnId);
 
-                    if (router.isEnableSnat()) {
-                        LOG.info("add : SNAT enabled for router {}", routerId);
-                        handleSNATForDPN(dpnId, routerId, vpnId, writeFlowInvTx, removeFlowInvTx);
-                    } else {
-                        LOG.info("add : SNAT is not enabled for router {} to handle addDPN event {}", routerId, dpnId);
-                    }
-                    List<ListenableFuture<Void>> futures = new ArrayList<>();
-                    futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
-                    futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
+
+                        if (router.isEnableSnat()) {
+                            LOG.info("add : SNAT enabled for router {}", routerUuid);
+                            ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,
+                                    routerUuid, networkId);
+                            if (extNwProvType == null) {
+                                LOG.error("add : External Network Provider Type missing");
+                                writeFlowInvTx.cancel();
+                                removeFlowInvTx.cancel();
+                                return futures;
+                            }
+                            handleSNATForDPN(dpnId, routerUuid, routerId, vpnId, writeFlowInvTx, removeFlowInvTx,
+                                    extNwProvType);
+                        } else {
+                            LOG.info("add : SNAT is not enabled for router {} to handle addDPN event {}",
+                                    routerUuid, dpnId);
+                        }
+                        futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
+                        futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
+                        return futures;
+                    }, NatConstants.NAT_DJC_MAX_RETRIES);
                 } // end of controller based SNAT
             }
         } else {
-            LOG.debug("add : Router {} is not associated with External network", routerId);
+            LOG.debug("add : Router {} is not associated with External network", routerUuid);
         }
     }
 
     @Override
     protected void remove(InstanceIdentifier<DpnVpninterfacesList> identifier, DpnVpninterfacesList dpnInfo) {
         LOG.trace("remove : key: {}, value: {}", dpnInfo.getKey(), dpnInfo);
-        final String routerId = identifier.firstKeyOf(RouterDpnList.class).getRouterId();
+        final String routerUuid = identifier.firstKeyOf(RouterDpnList.class).getRouterId();
+        Long routerId = NatUtil.getVpnId(dataBroker, routerUuid);
+        if (routerId == NatConstants.INVALID_ID) {
+            LOG.error("REMOVE: Invalid routId returned for routerName {}",routerUuid);
+            return;
+        }
         BigInteger dpnId = dpnInfo.getDpnId();
         //check router is associated to external network
-        InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerId);
+        InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerUuid);
         Optional<Routers> routerData =
                 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
                         LogicalDatastoreType.CONFIGURATION, id);
@@ -233,51 +237,58 @@ public class RouterDpnChangeListener
                         LOG.warn("remove : NAPT switch is not selected.");
                         return;
                     }
+                    //If it is for NAPT switch skip as the flows would be already programmed.
+                    if (naptSwitch.equals(dpnId)) {
+                        LOG.debug("Skipping the notification recived for NAPT switch {}", routerUuid);
+                        return;
+                    }
                     natServiceManager.notify(router, naptSwitch, dpnId,
                             SnatServiceManager.Action.SNAT_ROUTER_DISBL);
                 } else {
-                    WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
-                    LOG.debug("remove : Router {} is associated with ext nw {}", routerId, networkId);
-                    Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerId);
-                    Long vpnId;
-                    if (vpnName == null) {
-                        LOG.debug("remove : Internal vpn associated to router {}", routerId);
-                        vpnId = NatUtil.getVpnId(dataBroker, routerId);
-                        if (vpnId == NatConstants.INVALID_ID) {
-                            LOG.error("remove : Invalid vpnId returned for routerName {}", routerId);
-                            return;
-                        }
-                        LOG.debug("remove : Retrieved vpnId {} for router {}", vpnId, routerId);
-                        //Remove default entry in FIB
-                        LOG.debug("remove : Removing default route in FIB on dpn {} for vpn {} ...", dpnId, vpnName);
-                        snatDefaultRouteProgrammer.removeDefNATRouteInDPN(dpnId, vpnId, removeFlowInvTx);
-                    } else {
-                        LOG.debug("remove : External vpn associated to router {}", routerId);
-                        vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
-                        if (vpnId == NatConstants.INVALID_ID) {
-                            LOG.error("remove : Invalid vpnId returned for routerName {}", routerId);
-                            return;
-                        }
-                        Long routId = NatUtil.getVpnId(dataBroker, routerId);
-                        if (routId == NatConstants.INVALID_ID) {
-                            LOG.error("remove : Invalid routId returned for routerName {}", routerId);
-                            return;
+                    coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + dpnInfo.getKey(), () -> {
+                        WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
+                        LOG.debug("remove : Router {} is associated with ext nw {}", routerUuid, networkId);
+                        Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerUuid);
+                        Long vpnId;
+                        List<ListenableFuture<Void>> futures = new ArrayList<>();
+                        if (vpnName == null) {
+                            LOG.debug("remove : Internal vpn associated to router {}", routerUuid);
+                            vpnId = routerId;
+                            if (vpnId == NatConstants.INVALID_ID) {
+                                LOG.error("remove : Invalid vpnId returned for routerName {}", routerUuid);
+                                removeFlowInvTx.cancel();
+                                return futures;
+                            }
+                            LOG.debug("remove : Retrieved vpnId {} for router {}", vpnId, routerUuid);
+                            //Remove default entry in FIB
+                            LOG.debug("remove : Removing default route in FIB on dpn {} for vpn {} ...", dpnId,
+                                    vpnName);
+                            snatDefaultRouteProgrammer.removeDefNATRouteInDPN(dpnId, vpnId, removeFlowInvTx);
+                        } else {
+                            LOG.debug("remove : External vpn associated to router {}", routerUuid);
+                            vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
+                            if (vpnId == NatConstants.INVALID_ID) {
+                                LOG.error("remove : Invalid vpnId returned for routerName {}", routerUuid);
+                                removeFlowInvTx.cancel();
+                                return futures;
+                            }
+                            LOG.debug("remove : Retrieved vpnId {} for router {}", vpnId, routerUuid);
+                            //Remove default entry in FIB
+                            LOG.debug("remove : Removing default route in FIB on dpn {} for vpn {} ...", dpnId,
+                                    vpnName);
+                            snatDefaultRouteProgrammer.removeDefNATRouteInDPN(dpnId, vpnId, routerId, removeFlowInvTx);
                         }
-                        LOG.debug("remove : Retrieved vpnId {} for router {}", vpnId, routerId);
-                        //Remove default entry in FIB
-                        LOG.debug("remove : Removing default route in FIB on dpn {} for vpn {} ...", dpnId, vpnName);
-                        snatDefaultRouteProgrammer.removeDefNATRouteInDPN(dpnId, vpnId, routId, removeFlowInvTx);
-                    }
 
-                    if (router.isEnableSnat()) {
-                        LOG.info("remove : SNAT enabled for router {}", routerId);
-                        removeSNATFromDPN(dpnId, routerId, vpnId, removeFlowInvTx);
-                    } else {
-                        LOG.info("remove : SNAT is not enabled for router {} to handle removeDPN event {}",
-                                routerId, dpnId);
-                    }
-                    List<ListenableFuture<Void>> futures = new ArrayList<>();
-                    futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
+                        if (router.isEnableSnat()) {
+                            LOG.info("remove : SNAT enabled for router {}", routerUuid);
+                            removeSNATFromDPN(dpnId, routerUuid, routerId, vpnId, networkId, removeFlowInvTx);
+                        } else {
+                            LOG.info("remove : SNAT is not enabled for router {} to handle removeDPN event {}",
+                                    routerUuid, dpnId);
+                        }
+                        futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
+                        return futures;
+                    }, NatConstants.NAT_DJC_MAX_RETRIES);
                 } // end of controller based SNAT
             }
         }
@@ -291,18 +302,13 @@ public class RouterDpnChangeListener
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    void handleSNATForDPN(BigInteger dpnId, String routerName, Long routerVpnId, WriteTransaction writeFlowInvTx,
-                          WriteTransaction removeFlowInvTx) {
+    void handleSNATForDPN(BigInteger dpnId, String routerName, long routerId, Long routerVpnId,
+            WriteTransaction writeFlowInvTx, WriteTransaction removeFlowInvTx, ProviderTypes extNwProvType) {
        //Check if primary and secondary switch are selected, If not select the role
         //Install select group to NAPT switch
         //Install default miss entry to NAPT switch
         BigInteger naptSwitch;
         try {
-            Long routerId = NatUtil.getVpnId(dataBroker, routerName);
-            if (routerId == NatConstants.INVALID_ID) {
-                LOG.error("handleSNATForDPN : Invalid routerId returned for routerName {}", routerName);
-                return;
-            }
             BigInteger naptId = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
             if (naptId == null || naptId.equals(BigInteger.ZERO) || !naptSwitchHA.getSwitchStatus(naptId)) {
                 LOG.debug("handleSNATForDPN : No NaptSwitch is selected for router {}", routerName);
@@ -316,14 +322,14 @@ public class RouterDpnChangeListener
                 LOG.debug("handleSNATForDPN : Switch {} is elected as NaptSwitch for router {}", dpnId, routerName);
 
                 // When NAPT switch is elected during first VM comes up for the given Router
-                if (elanManager.isOpenStackVniSemanticsEnforced()) {
+                if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
                     NatOverVxlanUtil.validateAndCreateVxlanVniPool(dataBroker, nvpnManager,
                             idManager, NatConstants.ODL_VNI_POOL_NAME);
                 }
 
                 Routers extRouters = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
                 if (extRouters != null) {
-                    NatUtil.createRouterIdsConfigDS(dataBroker, routerName);
+                    NatUtil.createRouterIdsConfigDS(dataBroker, routerId, routerName);
                     naptSwitchHA.subnetRegisterMapping(extRouters, routerId);
                 }
 
@@ -343,7 +349,7 @@ public class RouterDpnChangeListener
                 if (naptId != null && !naptId.equals(BigInteger.ZERO)) {
                     LOG.debug("handleSNATForDPN : Removing primary flows from old napt switch {} for router {}",
                             naptId, routerName);
-                    naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, naptId, null, removeFlowInvTx);
+                    naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, routerId, naptId, null, removeFlowInvTx);
                 }
             } else if (naptId.equals(dpnId)) {
                 LOG.debug("handleSNATForDPN : NaptSwitch {} gone down during cluster reboot came alive", naptId);
@@ -354,12 +360,7 @@ public class RouterDpnChangeListener
 
                 //installing group
                 List<BucketInfo> bucketInfo = naptSwitchHA.handleGroupInNeighborSwitches(dpnId,
-                        routerName, naptSwitch);
-                if (bucketInfo == null) {
-                    LOG.error("handleSNATForDPN:Failed to populate bucketInfo for dpnId {},routername {},naptSwitch {}",
-                            dpnId, routerName, naptSwitch);
-                    return;
-                }
+                        routerName, routerId, naptSwitch);
                 naptSwitchHA.installSnatGroupEntry(dpnId, bucketInfo, routerName);
 
                 // Install miss entry (table 26) pointing to group
@@ -384,18 +385,14 @@ public class RouterDpnChangeListener
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    void removeSNATFromDPN(BigInteger dpnId, String routerName, long routerVpnId, WriteTransaction removeFlowInvTx) {
+    void removeSNATFromDPN(BigInteger dpnId, String routerName, long routerId, long routerVpnId,
+            Uuid extNetworkId, WriteTransaction removeFlowInvTx) {
         //irrespective of naptswitch or non-naptswitch, SNAT default miss entry need to be removed
         //remove miss entry to NAPT switch
         //if naptswitch elect new switch and install Snat flows and remove those flows in oldnaptswitch
 
-        Long routerId = NatUtil.getVpnId(dataBroker, routerName);
-        if (routerId == NatConstants.INVALID_ID) {
-            LOG.error("removeSNATFromDPN : Invalid routerId returned for routerName {}", routerName);
-            return;
-        }
         Collection<String> externalIpCache = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName);
+        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, extNetworkId);
         if (extNwProvType == null) {
             return;
         }
@@ -413,8 +410,8 @@ public class RouterDpnChangeListener
         }
         try {
             boolean naptStatus =
-                naptSwitchHA.isNaptSwitchDown(routerName, dpnId, naptSwitch, routerVpnId, externalIpCache,
-                        removeFlowInvTx);
+                naptSwitchHA.isNaptSwitchDown(routerName, routerId, dpnId, naptSwitch, routerVpnId,
+                        externalIpCache, removeFlowInvTx);
             if (!naptStatus) {
                 LOG.debug("removeSNATFromDPN: Switch with DpnId {} is not naptSwitch for router {}",
                     dpnId, routerName);
@@ -453,7 +450,8 @@ public class RouterDpnChangeListener
                 LOG.debug("removeSNATFromDPN : Removed default SNAT miss entry flow for dpnID {} with routerName {}",
                     dpnId, routerName);
             } else {
-                naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, naptSwitch, externalIpLabel, removeFlowInvTx);
+                naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, routerId, naptSwitch,
+                        externalIpLabel, removeFlowInvTx);
                 //remove table 26 flow ppointing to table46
                 FlowEntity flowEntity = null;
                 try {
@@ -495,16 +493,10 @@ public class RouterDpnChangeListener
             if (vpnIdForSubnet != NatConstants.INVALID_ID) {
                 LOG.info("installDefaultNatRouteForRouterExternalSubnets : Installing default routes in FIB on dpn {} "
                         + "for subnetId {} with vpnId {}", dpnId, subnetId, vpnIdForSubnet);
-                snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnIdForSubnet, subnetId.getValue(),
-                        idManager);
+                snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnIdForSubnet, subnetId.getValue());
             } else {
                 LOG.debug("installDefaultNatRouteForRouterExternalSubnets : No vpnID for subnet {} found", subnetId);
             }
         }
     }
-
-    private InstanceIdentifier<Subnetmap> getSubnetMapIdentifier(Uuid subnetId) {
-        return InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
-                new SubnetmapKey(subnetId)).build();
-    }
 }