OPNFLWPLUG-972 : Point to openflowplugin liblldp
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NatTunnelInterfaceStateListener.java
index d5858524a72938a91b9754f5b865d391d51c037e..6de9fb59029559cc842b97b08ea938cc332bc78b 100644 (file)
@@ -14,6 +14,7 @@ import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.JdkFutureAdapters;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -80,8 +81,7 @@ import org.slf4j.LoggerFactory;
 
 @Singleton
 public class NatTunnelInterfaceStateListener
-    extends AsyncDataTreeChangeListenerBase<StateTunnelList, NatTunnelInterfaceStateListener>
-    implements AutoCloseable {
+    extends AsyncDataTreeChangeListenerBase<StateTunnelList, NatTunnelInterfaceStateListener> {
 
     private static final Logger LOG = LoggerFactory.getLogger(NatTunnelInterfaceStateListener.class);
     private final DataBroker dataBroker;
@@ -98,7 +98,7 @@ public class NatTunnelInterfaceStateListener
     private final FibRpcService fibRpcService;
     private final IElanService elanManager;
     private final IInterfaceManager interfaceManager;
-    private NatMode natMode = NatMode.Controller;
+    private final NatMode natMode;
 
     protected enum TunnelAction {
         TUNNEL_EP_ADD,
@@ -116,11 +116,14 @@ public class NatTunnelInterfaceStateListener
      * @param naptSwitchHA           - NAPT Switch HA
      * @param mdsalManager           - MDSAL Manager
      * @param idManager              - ID manager
-     * @param externalRouterListner  - External Router Listner
+     * @param externalRouterListner  - External Router Listener
+     * @param natServiceManager      - Nat Service Manager
      * @param interfaceService       - Interface Service
-     * @param floatingIPListener     -  Floating IP Listner
+     * @param floatingIPListener     -  Floating IP Listener
      * @param fibRpcService          - FIB RPC Service
+     * @param config                 - Nat Service Config
      * @param elanManager            - Elan Manager
+     * @param interfaceManager       - Interface Manager
      */
     @Inject
     public NatTunnelInterfaceStateListener(final DataBroker dataBroker,
@@ -155,6 +158,8 @@ public class NatTunnelInterfaceStateListener
         this.interfaceManager = interfaceManager;
         if (config != null) {
             this.natMode = config.getNatMode();
+        } else {
+            this.natMode = NatMode.Controller;
         }
     }
 
@@ -211,19 +216,17 @@ public class NatTunnelInterfaceStateListener
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    void removeSNATFromDPN(BigInteger dpnId, String routerName, long routerVpnId, Uuid networkId,
-                           WriteTransaction writeFlowInvTx) {
+    void removeSNATFromDPN(BigInteger dpnId, String routerName, long routerId, long routerVpnId,
+            Uuid networkId, ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
         //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 : SNAT -> Invalid routerId returned for routerName {}", routerName);
             return;
         }
         Collection<String> externalIpCache = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,routerName);
         if (extNwProvType == null) {
             return;
         }
@@ -234,7 +237,7 @@ public class NatTunnelInterfaceStateListener
             externalIpLabel = NatUtil.getExternalIpsLabelForRouter(dataBroker, routerId);
         }
         try {
-            final String externalVpnName = NatUtil.getAssociatedVPN(dataBroker, networkId, LOG);
+            final String externalVpnName = NatUtil.getAssociatedVPN(dataBroker, networkId);
             if (externalVpnName == null) {
                 LOG.error("removeSNATFromDPN : SNAT -> No VPN associated with ext nw {} in router {}",
                     networkId, routerId);
@@ -243,8 +246,8 @@ public class NatTunnelInterfaceStateListener
 
             BigInteger naptSwitch = dpnId;
             boolean naptStatus =
-                naptSwitchHA.isNaptSwitchDown(routerName, dpnId, naptSwitch, routerVpnId, externalIpCache, false,
-                        writeFlowInvTx);
+                naptSwitchHA.isNaptSwitchDown(routerName, routerId, dpnId, naptSwitch,
+                        routerVpnId, externalIpCache, false, writeFlowInvTx);
             if (!naptStatus) {
                 LOG.debug("removeSNATFromDPN:SNAT->NaptSwitchDown:Switch with DpnId {} is not naptSwitch for router {}",
                     dpnId, routerName);
@@ -283,7 +286,8 @@ public class NatTunnelInterfaceStateListener
                 LOG.debug("removeSNATFromDPN : SNAT->Removed default SNAT miss entry flow for dpnID {}, routerName {}",
                     dpnId, routerName);
             } else {
-                naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, dpnId, externalIpLabel, writeFlowInvTx);
+                naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, routerId, dpnId,
+                        externalIpLabel, writeFlowInvTx);
                 //remove table 26 flow ppointing to table46
                 FlowEntity flowEntity = null;
                 try {
@@ -340,22 +344,20 @@ public class NatTunnelInterfaceStateListener
             switch (tunnelAction) {
                 case TUNNEL_EP_ADD:
                     WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
-                    List<ListenableFuture<Void>> futures = new ArrayList<>();
                     if (isTunnelInLogicalGroup(stateTunnelList)
                             || !hndlTepAddForAllRtrs(srcDpnId, tunnelType, tunnelName, srcTepIp, destTepIp,
                             writeFlowInvTx)) {
                         LOG.debug("hndlTepEvntsForDpn : Unable to process TEP ADD");
                     }
-                    futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
+                    NatUtil.waitForTransactionToComplete(writeFlowInvTx);
                     break;
                 case TUNNEL_EP_DELETE:
                     WriteTransaction writeFlowRemovetx = dataBroker.newWriteOnlyTransaction();
-                    List<ListenableFuture<Void>> futuresRemove = new ArrayList<>();
                     if (!handleTepDelForAllRtrs(srcDpnId, tunnelType, tunnelName, srcTepIp, destTepIp,
                             writeFlowRemovetx)) {
                         LOG.debug("hndlTepEvntsForDpn : Unable to process TEP DEL");
                     }
-                    futuresRemove.add(NatUtil.waitForTransactionToComplete(writeFlowRemovetx));
+                    NatUtil.waitForTransactionToComplete(writeFlowRemovetx);
                     break;
                 default:
                     LOG.warn("hndlTepEvntsForDpn: unknown tunnelAction: {}", tunnelAction);
@@ -377,7 +379,7 @@ public class NatTunnelInterfaceStateListener
                 SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker,
                         LogicalDatastoreType.OPERATIONAL, dpnRoutersListId);
         if (!optionalRouterDpnList.isPresent()) {
-            LOG.warn("hndlTepAddForAllRtrs : RouterDpnList model is empty for DPN {}. Hence ignoring TEP add event "
+            LOG.info("hndlTepAddForAllRtrs : RouterDpnList model is empty for DPN {}. Hence ignoring TEP add event "
                     + "for the ITM TUNNEL TYPE {} b/w SRC IP {} and DST IP {} and TUNNEL NAME {} ",
                 srcDpnId, tunnelType, srcTepIp, destTepIp, tunnelName);
             return false;
@@ -393,13 +395,25 @@ public class NatTunnelInterfaceStateListener
 
         String nextHopIp = NatUtil.getEndpointIpAddressForDPN(dataBroker, srcDpnId);
         for (RoutersList router : routersList) {
+            String routerName = router.getRouter();
+            long routerId = NatUtil.getVpnId(dataBroker, routerName);
+            if (routerId == NatConstants.INVALID_ID) {
+                LOG.error("hndlTepAddForAllRtrs :Invalid ROUTER-ID {} returned for routerName {}",
+                        routerId, routerName);
+                return false;
+            }
+            LOG.debug("hndlTepAddForAllRtrs : TEP ADD : DNAT -> Advertising routes for router {} ", routerName);
+            Uuid externalNetworkId = NatUtil.getNetworkIdFromRouterName(dataBroker,routerName);
+            ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,
+                    routerName, externalNetworkId);
+            if (extNwProvType == null) {
+                return false;
+            }
+            hndlTepAddForDnatInEachRtr(router, routerId, nextHopIp, srcDpnId, extNwProvType, writeFlowInvTx);
 
-            LOG.debug("hndlTepAddForAllRtrs : TEP ADD : DNAT -> Advertising routes for router {} ", router.getRouter());
-            hndlTepAddForDnatInEachRtr(router, nextHopIp, srcDpnId, writeFlowInvTx);
-
-            LOG.debug("hndlTepAddForAllRtrs : TEP ADD : SNAT -> Advertising routes for router {} ", router.getRouter());
-            hndlTepAddForSnatInEachRtr(router, srcDpnId, tunnelType, srcTepIp, destTepIp,
-                tunnelName, nextHopIp, writeFlowInvTx);
+            LOG.debug("hndlTepAddForAllRtrs : TEP ADD : SNAT -> Advertising routes for router {} ", routerName);
+            hndlTepAddForSnatInEachRtr(router, routerId, srcDpnId, tunnelType, srcTepIp, destTepIp,
+                tunnelName, nextHopIp, extNwProvType, writeFlowInvTx);
         }
         return true;
     }
@@ -451,18 +465,32 @@ public class NatTunnelInterfaceStateListener
         }
 
         for (RoutersList router : routersList) {
-            LOG.debug("handleTepDelForAllRtrs :  TEP DEL : DNAT -> Withdrawing routes for router {} ",
-                    router.getRouter());
-            hndlTepDelForDnatInEachRtr(router, srcDpnId, writeFlowInvTx);
+            String routerName = router.getRouter();
+            LOG.debug("handleTepDelForAllRtrs :  TEP DEL : DNAT -> Withdrawing routes for router {} ", routerName);
+            long routerId = NatUtil.getVpnId(dataBroker, routerName);
+            if (routerId == NatConstants.INVALID_ID) {
+                LOG.error("handleTepDelForAllRtrs :Invalid ROUTER-ID {} returned for routerName {}",
+                        routerId, routerName);
+                return false;
+            }
+            Uuid externalNetworkId = NatUtil.getNetworkIdFromRouterName(dataBroker,routerName);
+            ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,
+                    routerName, externalNetworkId);
+            if (extNwProvType == null) {
+                return false;
+            }
+            hndlTepDelForDnatInEachRtr(router, routerId, srcDpnId, extNwProvType, writeFlowInvTx);
             LOG.debug("handleTepDelForAllRtrs :  TEP DEL : SNAT -> Withdrawing and Advertising routes for router {} ",
                 router.getRouter());
-            hndlTepDelForSnatInEachRtr(router, srcDpnId, tunnelType, srcTepIp, destTepIp, tunnelName, writeFlowInvTx);
+            hndlTepDelForSnatInEachRtr(router, routerId, srcDpnId, tunnelType, srcTepIp, destTepIp,
+                    tunnelName, extNwProvType, writeFlowInvTx);
         }
         return true;
     }
 
-    private void hndlTepAddForSnatInEachRtr(RoutersList router, final BigInteger srcDpnId, String tunnelType,
-            String srcTepIp, String destTepIp, String tunnelName, String nextHopIp, WriteTransaction writeFlowInvTx) {
+    private void hndlTepAddForSnatInEachRtr(RoutersList router, long routerId, final BigInteger srcDpnId,
+            String tunnelType, String srcTepIp, String destTepIp, String tunnelName, String nextHopIp,
+            ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
 
         /*SNAT : Remove the old routes to the external IP having the old TEP IP as the next hop IP
                  Advertise to the BGP about the new route to the external IP having the new TEP IP
@@ -481,7 +509,6 @@ public class NatTunnelInterfaceStateListener
             return;
         }
 
-        long routerId = NatUtil.getVpnId(dataBroker, routerName);
         BigInteger naptId = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
         if (naptId == null || naptId.equals(BigInteger.ZERO)) {
             LOG.warn("hndlTepAddForSnatInEachRtr : SNAT -> Ignoring TEP add for the DPN {} having the router {} since"
@@ -493,6 +520,7 @@ public class NatTunnelInterfaceStateListener
             natServiceManager.notify(routerData.get(), naptId, srcDpnId,
                     SnatServiceManager.Action.SNAT_ROUTER_ENBL);
         } else {
+            Uuid bgpVpnUuId = NatUtil.getVpnForRouter(dataBroker, routerName);
             //Check if the DPN having the router is the NAPT switch
             if (!naptId.equals(srcDpnId)) {
                 /*
@@ -501,7 +529,7 @@ public class NatTunnelInterfaceStateListener
             3) Install the flow 26 which forwards the packet to the group.
                  */
                 if (!hndlTepAddOnNonNaptSwitch(srcDpnId, naptId, tunnelType, srcTepIp, destTepIp, tunnelName,
-                        routerName, routerId, writeFlowInvTx)) {
+                        routerName, routerId, bgpVpnUuId, writeFlowInvTx)) {
                     LOG.error("hndlTepAddForSnatInEachRtr : Unable to process the TEP add event on NON-NAPT switch {}",
                             srcDpnId);
                     return;
@@ -509,7 +537,7 @@ public class NatTunnelInterfaceStateListener
                 return;
             }
             if (!hndlTepAddOnNaptSwitch(srcDpnId, tunnelType, srcTepIp, destTepIp, tunnelName, routerId,
-                    routerData, nextHopIp,writeFlowInvTx)) {
+                    routerData, nextHopIp, bgpVpnUuId, extNwProvType, writeFlowInvTx)) {
                 LOG.debug("hndlTepAddForSnatInEachRtr : Unable to process the TEP add event on NAPT switch {}",
                         srcDpnId);
                 return;
@@ -520,7 +548,7 @@ public class NatTunnelInterfaceStateListener
 
     private boolean hndlTepAddOnNonNaptSwitch(BigInteger srcDpnId, BigInteger primaryDpnId, String tunnelType,
                                               String srcTepIp, String destTepIp, String tunnelName, String routerName,
-                                              long routerId, WriteTransaction writeFlowInvTx) {
+                                              long routerId, Uuid vpnName, WriteTransaction writeFlowInvTx) {
 
         /*
         1) Install default NAT rule from table 21 to 26
@@ -532,7 +560,6 @@ public class NatTunnelInterfaceStateListener
                 + "and TUNNEL NAME {} ",
             srcDpnId, routerName, tunnelType, srcTepIp, destTepIp, tunnelName);
         LOG.debug("hndlTepAddOnNonNaptSwitch : SNAT -> Install default NAT rule from table 21 to 26");
-        Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerName);
         Long vpnId;
         if (vpnName == null) {
             LOG.debug("hndlTepAddOnNonNaptSwitch : SNAT -> Internal VPN associated to router {}", routerId);
@@ -550,7 +577,7 @@ public class NatTunnelInterfaceStateListener
 
             LOG.debug("hndlTepAddOnNonNaptSwitch : SNAT -> Install the group which forward packet to the tunnel port "
                 + "for the NAPT switch {} and the flow 26 which forwards to group", primaryDpnId);
-            externalRouterListner.handleSwitches(srcDpnId, routerName, primaryDpnId, writeFlowInvTx);
+            externalRouterListner.handleSwitches(srcDpnId, routerName, routerId, primaryDpnId, writeFlowInvTx);
         } else {
             LOG.debug("hndlTepAddOnNonNaptSwitch : SNAT -> External BGP VPN (Private BGP) associated to router {}",
                     routerId);
@@ -573,7 +600,7 @@ public class NatTunnelInterfaceStateListener
             LOG.debug("hndlTepAddOnNonNaptSwitch : Install group in non NAPT switch {} for router {}",
                     srcDpnId, routerName);
             List<BucketInfo> bucketInfoForNonNaptSwitches =
-                externalRouterListner.getBucketInfoForNonNaptSwitches(srcDpnId, primaryDpnId, routerName);
+                externalRouterListner.getBucketInfoForNonNaptSwitches(srcDpnId, primaryDpnId, routerName, routerId);
             long groupId = externalRouterListner.installGroup(srcDpnId, routerName, bucketInfoForNonNaptSwitches);
 
             LOG.debug("hndlTepAddOnNonNaptSwitch : SNAT -> in the SNAT miss entry pointing to group {} "
@@ -587,8 +614,8 @@ public class NatTunnelInterfaceStateListener
 
     private boolean hndlTepAddOnNaptSwitch(BigInteger srcDpnId, String tunnelType, String srcTepIp,
                                            String destTepIp, String tunnelName, long routerId,
-                                           Optional<Routers> routerData, String nextHopIp,
-                                           WriteTransaction writeFlowInvTx) {
+                                           Optional<Routers> routerData, String nextHopIp, Uuid vpnName,
+                                           ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
         if (!routerData.isPresent()) {
             LOG.warn("hndlTepAddOnNaptSwitch: routerData is not present");
             return false;
@@ -607,7 +634,6 @@ public class NatTunnelInterfaceStateListener
         }
 
         LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Router {} is associated with Ext nw {}", routerId, networkId);
-        Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerName);
         Long vpnId;
         if (vpnName == null) {
             LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Internal VPN associated to router {}", routerId);
@@ -637,7 +663,7 @@ public class NatTunnelInterfaceStateListener
 
         //Withdraw the old route to the external IP from the BGP which was having the
         //next hop as the old TEP IP.
-        final String externalVpnName = NatUtil.getAssociatedVPN(dataBroker, networkId, LOG);
+        final String externalVpnName = NatUtil.getAssociatedVPN(dataBroker, networkId);
         if (externalVpnName == null) {
             LOG.error("hndlTepAddOnNaptSwitch :  SNAT -> No VPN associated with ext nw {} in router {}",
                 networkId, routerId);
@@ -658,7 +684,6 @@ public class NatTunnelInterfaceStateListener
         FIB manager.
         */
         String rd = NatUtil.getVpnRd(dataBroker, externalVpnName);
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,routerName);
         if (extNwProvType == null) {
             return false;
         }
@@ -666,7 +691,7 @@ public class NatTunnelInterfaceStateListener
         long l3Vni = 0;
         if (extNwProvType == ProviderTypes.VXLAN) {
             // Get the External Gateway MAC Address which is Router gateway MAC address for SNAT
-            gwMacAddress = NatUtil.getExtGwMacAddFromRouterId(dataBroker, routerId);
+            gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
             if (gwMacAddress != null) {
                 LOG.debug("hndlTepAddOnNaptSwitch : External Gateway MAC address {} found for External Router ID {}",
                         gwMacAddress, routerId);
@@ -684,89 +709,89 @@ public class NatTunnelInterfaceStateListener
                 l3Vni = NatOverVxlanUtil.getInternetVpnVni(idManager, externalVpnName, routerId).longValue();
             }
         }
-        if (externalIps != null) {
-            for (final String externalIp : externalIps) {
-                long serviceId = 0;
-                String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp);
-                if (extNwProvType == ProviderTypes.VXLAN) {
-                    LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Advertise the route to the externalIp {} "
-                            + "having nextHopIp {}", externalIp, nextHopIp);
-                    NatEvpnUtil.addRoutesForVxLanProvType(dataBroker, bgpManager, fibManager, externalVpnName, rd,
-                            externalIp, nextHopIp, l3Vni, tunnelName, gwMacAddress, writeFlowInvTx, RouteOrigin.STATIC,
-                            srcDpnId);
-                    serviceId = l3Vni;
-                } else {
 
-                    Long label = externalRouterListner.checkExternalIpLabel(routerId,
-                            externalIp);
-                    if (label == null || label == NatConstants.INVALID_ID) {
-                        LOG.error("hndlTepAddOnNaptSwitch : SNAT->Unable to advertise to the DC GW "
-                                + "since label is invalid");
-                        return false;
-                    }
-                    LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Advertise the route to the externalIp {} "
-                            + "having nextHopIp {}", externalIp, nextHopIp);
-                    long l3vni = 0;
-                    if (elanManager.isOpenStackVniSemanticsEnforced()) {
-                        l3vni = NatOverVxlanUtil.getInternetVpnVni(idManager, externalVpnName, l3vni).longValue();
-                    }
-                    Uuid externalSubnetId = NatUtil.getExternalSubnetForRouterExternalIp(dataBroker, externalIp,
-                            router);
-                    NatUtil.addPrefixToBGP(dataBroker, bgpManager, fibManager, externalVpnName, rd, externalSubnetId,
-                            fibExternalIp, nextHopIp, networkId.getValue(), null /* mac-address */, label, l3vni, LOG,
-                            RouteOrigin.STATIC, srcDpnId);
-                    serviceId = label;
+        for (final String externalIp : externalIps) {
+            long serviceId = 0;
+            String fibExternalIp = NatUtil.validateAndAddNetworkMask(externalIp);
+            if (extNwProvType == ProviderTypes.VXLAN) {
+                LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Advertise the route to the externalIp {} "
+                        + "having nextHopIp {}", externalIp, nextHopIp);
+                NatEvpnUtil.addRoutesForVxLanProvType(dataBroker, bgpManager, fibManager, externalVpnName, rd,
+                        externalIp, nextHopIp, l3Vni, tunnelName, gwMacAddress, writeFlowInvTx, RouteOrigin.STATIC,
+                        srcDpnId);
+                serviceId = l3Vni;
+            } else {
+
+                Long label = externalRouterListner.checkExternalIpLabel(routerId,
+                        externalIp);
+                if (label == null || label == NatConstants.INVALID_ID) {
+                    LOG.error("hndlTepAddOnNaptSwitch : SNAT->Unable to advertise to the DC GW "
+                            + "since label is invalid");
+                    return false;
+                }
+                LOG.debug("hndlTepAddOnNaptSwitch : SNAT -> Advertise the route to the externalIp {} "
+                        + "having nextHopIp {}", externalIp, nextHopIp);
+                long l3vni = 0;
+                if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
+                    l3vni = NatOverVxlanUtil.getInternetVpnVni(idManager, externalVpnName, l3vni).longValue();
                 }
+                Uuid externalSubnetId = NatUtil.getExternalSubnetForRouterExternalIp(dataBroker, externalIp,
+                        router);
+                NatUtil.addPrefixToBGP(dataBroker, bgpManager, fibManager, externalVpnName, rd, externalSubnetId,
+                        fibExternalIp, nextHopIp, networkId.getValue(), null /* mac-address */, label, l3vni,
+                        RouteOrigin.STATIC, srcDpnId);
+                serviceId = label;
+            }
 
-                LOG.debug("hndlTepAddOnNaptSwitch: SNAT -> Install custom FIB routes "
+            LOG.debug("hndlTepAddOnNaptSwitch: SNAT -> Install custom FIB routes "
                     + "(Table 21 -> Push MPLS label to Tunnel port");
-                List<Instruction> customInstructions = new ArrayList<>();
-                int customInstructionIndex = 0;
-                long externalSubnetVpnId = NatUtil.getExternalSubnetVpnIdForRouterExternalIp(dataBroker, externalIp,
-                        router);
-                if (externalSubnetVpnId != NatConstants.INVALID_ID) {
-                    LOG.debug("hndlTepAddOnNaptSwitch : Will install custom FIB router with external subnet VPN ID {}",
-                            externalSubnetVpnId);
-                    BigInteger subnetIdMetaData = MetaDataUtil.getVpnIdMetadata(externalSubnetVpnId);
-                    customInstructions.add(new InstructionWriteMetadata(subnetIdMetaData,
-                            MetaDataUtil.METADATA_MASK_VRFID).buildInstruction(customInstructionIndex));
-                    customInstructionIndex++;
-                }
-                customInstructions.add(new InstructionGotoTable(NwConstants.INBOUND_NAPT_TABLE)
-                        .buildInstruction(customInstructionIndex));
-                CreateFibEntryInput input =
+            List<Instruction> customInstructions = new ArrayList<>();
+            int customInstructionIndex = 0;
+            long externalSubnetVpnId = NatUtil.getExternalSubnetVpnIdForRouterExternalIp(dataBroker, externalIp,
+                    router);
+            if (externalSubnetVpnId != NatConstants.INVALID_ID) {
+                LOG.debug("hndlTepAddOnNaptSwitch : Will install custom FIB router with external subnet VPN ID {}",
+                        externalSubnetVpnId);
+                BigInteger subnetIdMetaData = MetaDataUtil.getVpnIdMetadata(externalSubnetVpnId);
+                customInstructions.add(new InstructionWriteMetadata(subnetIdMetaData,
+                        MetaDataUtil.METADATA_MASK_VRFID).buildInstruction(customInstructionIndex));
+                customInstructionIndex++;
+            }
+            customInstructions.add(new InstructionGotoTable(NwConstants.INBOUND_NAPT_TABLE)
+                    .buildInstruction(customInstructionIndex));
+            CreateFibEntryInput input =
                     new CreateFibEntryInputBuilder().setVpnName(externalVpnName).setSourceDpid(srcDpnId)
-                        .setInstruction(customInstructions).setIpAddress(fibExternalIp)
-                        .setServiceId(serviceId).setInstruction(customInstructions).build();
-                Future<RpcResult<Void>> future = fibRpcService.createFibEntry(input);
-                ListenableFuture<RpcResult<Void>> listenableFuture = JdkFutureAdapters.listenInPoolThread(future);
+                    .setInstruction(customInstructions).setIpAddress(fibExternalIp)
+                    .setServiceId(serviceId).setInstruction(customInstructions).build();
+            Future<RpcResult<Void>> future = fibRpcService.createFibEntry(input);
+            ListenableFuture<RpcResult<Void>> listenableFuture = JdkFutureAdapters.listenInPoolThread(future);
 
-                Futures.addCallback(listenableFuture, new FutureCallback<RpcResult<Void>>() {
+            Futures.addCallback(listenableFuture, new FutureCallback<RpcResult<Void>>() {
 
-                    @Override
-                    public void onFailure(@Nonnull Throwable error) {
-                        LOG.error("hndlTepAddOnNaptSwitch : SNAT->Error in generate label or fib install process",
-                                error);
-                    }
+                @Override
+                public void onFailure(@Nonnull Throwable error) {
+                    LOG.error("hndlTepAddOnNaptSwitch : SNAT->Error in generate label or fib install process",
+                            error);
+                }
 
-                    @Override
-                    public void onSuccess(RpcResult<Void> result) {
-                        if (result.isSuccessful()) {
-                            LOG.info("hndlTepAddOnNaptSwitch : SNAT -> Successfully installed custom FIB routes "
-                                    + "for prefix {}", externalIp);
-                        } else {
-                            LOG.error("hndlTepAddOnNaptSwitch : SNAT -> Error in rpc call to create custom Fib entries "
-                                    + "for prefix {} in DPN {}, {}", externalIp, srcDpnId, result.getErrors());
-                        }
+                @Override
+                public void onSuccess(@Nonnull RpcResult<Void> result) {
+                    if (result.isSuccessful()) {
+                        LOG.info("hndlTepAddOnNaptSwitch : SNAT -> Successfully installed custom FIB routes "
+                                + "for prefix {}", externalIp);
+                    } else {
+                        LOG.error("hndlTepAddOnNaptSwitch : SNAT -> Error in rpc call to create custom Fib entries "
+                                + "for prefix {} in DPN {}, {}", externalIp, srcDpnId, result.getErrors());
                     }
-                });
-            }
+                }
+            }, MoreExecutors.directExecutor());
         }
+
         return true;
     }
 
-    private void hndlTepAddForDnatInEachRtr(RoutersList router, String nextHopIp, BigInteger tepAddedDpnId,
-                                            WriteTransaction writeFlowInvTx) {
+    private void hndlTepAddForDnatInEachRtr(RoutersList router, long routerId, String nextHopIp,
+            BigInteger tepAddedDpnId, ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
         //DNAT : Advertise the new route to the floating IP having the new TEP IP as the next hop IP
         final String routerName = router.getRouter();
 
@@ -780,7 +805,7 @@ public class NatTunnelInterfaceStateListener
         }
         RouterPorts routerPorts = optRouterPorts.get();
         Uuid extNwId = routerPorts.getExternalNetworkId();
-        final String vpnName = NatUtil.getAssociatedVPN(dataBroker, extNwId, LOG);
+        final String vpnName = NatUtil.getAssociatedVPN(dataBroker, extNwId);
         if (vpnName == null) {
             LOG.info("hndlTepAddForDnatInEachRtr : DNAT -> No External VPN associated with ext nw {} for router {}",
                 extNwId, routerName);
@@ -788,8 +813,6 @@ public class NatTunnelInterfaceStateListener
         }
 
         String rd = NatUtil.getVpnRd(dataBroker, vpnName);
-        long routerId = NatUtil.getVpnId(dataBroker, routerName);
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,routerName);
         if (extNwProvType == null) {
             return;
         }
@@ -797,7 +820,7 @@ public class NatTunnelInterfaceStateListener
         long l3Vni = 0;
         if (extNwProvType == ProviderTypes.VXLAN) {
             // Get the External Gateway MAC Address which is Router gateway MAC address for SNAT
-            gwMacAddress = NatUtil.getExtGwMacAddFromRouterId(dataBroker, routerId);
+            gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
             if (gwMacAddress != null) {
                 LOG.debug("hndlTepAddForDnatInEachRtr : External GwMAC address {} found for External Router ID {}",
                         gwMacAddress, routerId);
@@ -857,11 +880,11 @@ public class NatTunnelInterfaceStateListener
                     LOG.debug("hndlTepAddForDnatInEachRtr : DNAT -> Advertise the route to the externalIp {} "
                             + "having nextHopIp {}", externalIp, nextHopIp);
                     long l3vni = 0;
-                    if (elanManager.isOpenStackVniSemanticsEnforced()) {
+                    if (NatUtil.isOpenStackVniSemanticsEnforcedForGreAndVxlan(elanManager, extNwProvType)) {
                         l3vni = NatOverVxlanUtil.getInternetVpnVni(idManager, vpnName, l3vni).longValue();
                     }
                     NatUtil.addPrefixToBGP(dataBroker, bgpManager, fibManager, vpnName, rd, null,
-                            fibExternalIp, nextHopIp, null, null, label, l3vni, LOG, RouteOrigin.STATIC,
+                            fibExternalIp, nextHopIp, null, null, label, l3vni, RouteOrigin.STATIC,
                             fipCfgdDpnId);
                     serviceId = label;
                 }
@@ -885,7 +908,7 @@ public class NatTunnelInterfaceStateListener
                     }
 
                     @Override
-                    public void onSuccess(RpcResult<Void> result) {
+                    public void onSuccess(@Nonnull RpcResult<Void> result) {
                         if (result.isSuccessful()) {
                             LOG.info("hndlTepAddForDnatInEachRtr : DNAT -> Successfully installed custom FIB routes "
                                     + "for prefix {}", externalIp);
@@ -894,14 +917,14 @@ public class NatTunnelInterfaceStateListener
                                 + "entries for prefix {} in DPN {}, {}", externalIp, fipCfgdDpnId, result.getErrors());
                         }
                     }
-                });
+                }, MoreExecutors.directExecutor());
             }
         }
     }
 
-    private void hndlTepDelForSnatInEachRtr(RoutersList router, BigInteger dpnId, String tunnelType,
+    private void hndlTepDelForSnatInEachRtr(RoutersList router, long routerId, BigInteger dpnId, String tunnelType,
                                             String srcTepIp, String destTepIp, String tunnelName,
-                                            WriteTransaction writeFlowInvTx) {
+                                            ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
        /*SNAT :
             1) Elect a new switch as the primary NAPT
             2) Advertise the new routes to BGP for the newly elected TEP IP as the DPN IP
@@ -923,17 +946,9 @@ public class NatTunnelInterfaceStateListener
             return;
         }
 
-        //Check if the router ID is valid
-        long routerId = NatUtil.getVpnId(dataBroker, routerName);
-        if (routerId == NatConstants.INVALID_ID) {
-            LOG.error("hndlTepDelForSnatInEachRtr : SNAT -> Invalid ROUTER-ID {} returned for routerName {}",
-                    routerId, routerName);
-            return;
-        }
-
         //Check if the DPN having the router is the NAPT switch
         BigInteger naptId = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
-        if (naptId == null || naptId.equals(BigInteger.ZERO) || (!naptId.equals(dpnId))) {
+        if (naptId == null || naptId.equals(BigInteger.ZERO) || !naptId.equals(dpnId)) {
             LOG.warn("hndlTepDelForSnatInEachRtr : SNAT -> Ignoring TEP delete for the DPN {} since"
                     + " its NOT a NAPT switch for the TUNNEL TYPE {} b/w SRC IP {} and DST IP {} and"
                     + "TUNNEL NAME {} ", dpnId, tunnelType, srcTepIp, destTepIp, tunnelName);
@@ -953,41 +968,36 @@ public class NatTunnelInterfaceStateListener
             }
 
             LOG.debug("hndlTepDelForSnatInEachRtr : SNAT->Router {} is associated with ext nw {}", routerId, networkId);
-            Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerName);
-            Long vpnId;
-            if (vpnName == null) {
+            Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
+            Long bgpVpnId;
+            if (bgpVpnUuid == null) {
                 LOG.debug("hndlTepDelForSnatInEachRtr : SNAT->Internal VPN-ID {} associated to router {}",
                         routerId, routerName);
-                vpnId = routerId;
+                bgpVpnId = routerId;
 
                 //Install default entry in FIB to SNAT table
                 LOG.debug("hndlTepDelForSnatInEachRtr : Installing default route in FIB on DPN {} for router {} with"
-                        + " vpn {}...", dpnId, routerName, vpnId);
-                defaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, writeFlowInvTx);
+                        + " vpn {}...", dpnId, routerName, bgpVpnId);
+                defaultRouteProgrammer.installDefNATRouteInDPN(dpnId, bgpVpnId, writeFlowInvTx);
             } else {
-                vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
-                if (vpnId == NatConstants.INVALID_ID) {
+                bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
+                if (bgpVpnId == NatConstants.INVALID_ID) {
                     LOG.error("hndlTepDelForSnatInEachRtr :SNAT->Invalid Private BGP VPN ID returned for routerName {}",
                             routerName);
                     return;
                 }
                 LOG.debug("hndlTepDelForSnatInEachRtr :SNAT->External BGP VPN (Private BGP) {} associated to router {}",
-                        vpnId, routerName);
+                        bgpVpnId, routerName);
                 //Install default entry in FIB to SNAT table
                 LOG.debug("hndlTepDelForSnatInEachRtr : Installing default route in FIB on dpn {} for routerId {} "
-                        + "with vpnId {}...", dpnId, routerId, vpnId);
-                defaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, routerId, writeFlowInvTx);
+                        + "with vpnId {}...", dpnId, routerId, bgpVpnId);
+                defaultRouteProgrammer.installDefNATRouteInDPN(dpnId, bgpVpnId, routerId, writeFlowInvTx);
             }
 
             if (routerData.get().isEnableSnat()) {
                 LOG.info("hndlTepDelForSnatInEachRtr : SNAT enabled for router {}", routerId);
 
                 long routerVpnId = routerId;
-                long bgpVpnId = NatConstants.INVALID_ID;
-                Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
-                if (bgpVpnUuid != null) {
-                    bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
-                }
                 if (bgpVpnId != NatConstants.INVALID_ID) {
                     LOG.debug("hndlTepDelForSnatInEachRtr : SNAT -> Private BGP VPN ID (Internal BGP VPN ID) {} "
                             + "associated to the router {}", bgpVpnId, routerName);
@@ -997,7 +1007,7 @@ public class NatTunnelInterfaceStateListener
                             + "associated to the router {}", routerVpnId, routerName);
                 }
                 //Re-elect the other available switch as the NAPT switch and program the NAT flows.
-                removeSNATFromDPN(dpnId, routerName, routerVpnId, networkId, writeFlowInvTx);
+                removeSNATFromDPN(dpnId, routerName, routerId, routerVpnId, networkId, extNwProvType, writeFlowInvTx);
             } else {
                 LOG.info("hndlTepDelForSnatInEachRtr : SNAT is not enabled for router {} to handle addDPN event {}",
                         routerId, dpnId);
@@ -1005,15 +1015,11 @@ public class NatTunnelInterfaceStateListener
         }
     }
 
-    private void hndlTepDelForDnatInEachRtr(RoutersList router, BigInteger tepDeletedDpnId,
-                                            WriteTransaction writeFlowInvTx) {
+    private void hndlTepDelForDnatInEachRtr(RoutersList router, long routerId, BigInteger tepDeletedDpnId,
+            ProviderTypes extNwProvType, WriteTransaction writeFlowInvTx) {
         //DNAT : Withdraw the routes from the BGP
         String routerName = router.getRouter();
-        long routerId = NatUtil.getVpnId(dataBroker, routerName);
-        if (routerId == NatConstants.INVALID_ID) {
-            LOG.error("hndlTepDelForDnatInEachRtr : Unable to get RouterId from RouterName {}", routerName);
-            return;
-        }
+
         LOG.debug("hndlTepDelForDnatInEachRtr : DNAT -> Trying to clear routes to the Floating IP "
                 + "associated to the router {}", routerName);
 
@@ -1027,14 +1033,13 @@ public class NatTunnelInterfaceStateListener
         }
         RouterPorts routerPorts = optRouterPorts.get();
         Uuid extNwId = routerPorts.getExternalNetworkId();
-        final String vpnName = NatUtil.getAssociatedVPN(dataBroker, extNwId, LOG);
+        final String vpnName = NatUtil.getAssociatedVPN(dataBroker, extNwId);
         if (vpnName == null) {
             LOG.error("hndlTepDelForDnatInEachRtr : DNAT -> No External VPN associated with Ext N/W {} for Router {}",
                 extNwId, routerName);
             return;
         }
         String rd = NatUtil.getVpnRd(dataBroker, vpnName);
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName);
         if (extNwProvType == null) {
             return;
         }
@@ -1102,7 +1107,7 @@ public class NatTunnelInterfaceStateListener
                     }
 
                     @Override
-                    public void onSuccess(RpcResult<Void> result) {
+                    public void onSuccess(@Nonnull RpcResult<Void> result) {
                         if (result.isSuccessful()) {
                             LOG.info("hndlTepDelForDnatInEachRtr : DNAT -> Successfully removed the entry pushing the "
                                 + "MPLS label to the tunnel");
@@ -1111,7 +1116,7 @@ public class NatTunnelInterfaceStateListener
                                 + "21 entry pushing the MPLS label to the tunnnel due to {}", result.getErrors());
                         }
                     }
-                });
+                }, MoreExecutors.directExecutor());
             }
         }
     }
@@ -1120,7 +1125,7 @@ public class NatTunnelInterfaceStateListener
         String ifaceName = stateTunnelList.getTunnelInterfaceName();
         if (getTunnelType(stateTunnelList) == NatConstants.ITMTunnelLocType.Internal.getValue()) {
             Interface configIface = interfaceManager.getInterfaceInfoFromConfigDataStore(ifaceName);
-            IfTunnel ifTunnel = (configIface != null) ? configIface.getAugmentation(IfTunnel.class) : null;
+            IfTunnel ifTunnel = configIface != null ? configIface.getAugmentation(IfTunnel.class) : null;
             if (ifTunnel != null && ifTunnel.getTunnelInterfaceType().isAssignableFrom(TunnelTypeVxlan.class)) {
                 ParentRefs refs = configIface.getAugmentation(ParentRefs.class);
                 if (refs != null && !Strings.isNullOrEmpty(refs.getParentInterface())) {