Bump odlparent->6.0.0,mdsal->5.0.3
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NaptSwitchHA.java
index 7ba4e2446b257bb255e31f4fbd76235953ace298..cdb1fea05ad72bb826662e4df16dcbe26ea87cf2 100644 (file)
@@ -8,8 +8,8 @@
 package org.opendaylight.netvirt.natservice.internal;
 
 import com.google.common.base.Optional;
-import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
@@ -18,10 +18,10 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
 import javax.inject.Inject;
 import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
@@ -38,7 +38,6 @@ import org.opendaylight.genius.mdsalutil.MatchInfo;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
-import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
 import org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable;
@@ -80,6 +79,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev16011
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitchKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -140,20 +141,20 @@ public class NaptSwitchHA {
         this.natOverVxlanUtil = natOverVxlanUtil;
     }
 
-    protected void removeSnatFlowsInOldNaptSwitch(String routerName, Long routerId, BigInteger naptSwitch,
-                                                  @Nullable Map<String, Long> externalIpmap,
+    protected void removeSnatFlowsInOldNaptSwitch(String routerName, Uint32 routerId, Uint64 naptSwitch,
+                                                  @Nullable Map<String, Uint32> externalIpmap, String externalVpnName,
                                                   TypedReadWriteTransaction<Configuration> confTx)
             throws ExecutionException, InterruptedException {
 
         //remove SNAT flows in old NAPT SWITCH
-        Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
-        String vpnName = getExtNetworkVpnName(routerName, networkId);
+        Uuid extNetworkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
+        String vpnName = getExtNetworkVpnName(routerName, extNetworkId);
         if (vpnName == null) {
             LOG.error("removeSnatFlowsInOldNaptSwitch : Vpn is not associated to externalN/w of router {}",
                 routerName);
             return;
         }
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, networkId);
+        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, extNetworkId);
         if (extNwProvType == null) {
             LOG.error("removeSnatFlowsInOldNaptSwitch : Unable to retrieve the External Network Provider Type "
                 + "for Router {}", routerName);
@@ -163,10 +164,10 @@ public class NaptSwitchHA {
             evpnNaptSwitchHA.evpnRemoveSnatFlowsInOldNaptSwitch(routerName, routerId, vpnName, naptSwitch, confTx);
         } else {
             //Remove the Terminating Service table entry which forwards the packet to Outbound NAPT Table
-            long tunnelId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, natOverVxlanUtil, elanManager,
+            Uint64 tunnelId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, natOverVxlanUtil, elanManager,
                     idManager, routerId, routerName);
             String tsFlowRef = externalRouterListener.getFlowRefTs(naptSwitch, NwConstants.INTERNAL_TUNNEL_TABLE,
-                tunnelId);
+                Uint32.valueOf(tunnelId));
             FlowEntity tsNatFlowEntity = NatUtil.buildFlowEntity(naptSwitch, NwConstants.INTERNAL_TUNNEL_TABLE,
                 tsFlowRef);
 
@@ -205,8 +206,8 @@ public class NaptSwitchHA {
         Collection<Uuid> externalSubnetIdsForRouter = NatUtil.getExternalSubnetIdsForRouter(dataBroker,
             routerName);
         for (Uuid externalSubnetId : externalSubnetIdsForRouter) {
-            long subnetVpnId = NatUtil.getVpnId(dataBroker, externalSubnetId.getValue());
-            if (subnetVpnId != -1 && !NatUtil.checkForRoutersWithSameExtSubnetAndNaptSwitch(
+            Uint32 subnetVpnId = NatUtil.getVpnId(dataBroker, externalSubnetId.getValue());
+            if (subnetVpnId != NatConstants.INVALID_ID && !NatUtil.checkForRoutersWithSameExtSubnetAndNaptSwitch(
                 dataBroker, externalSubnetId, routerName, naptSwitch)) {
                 String natPfibSubnetFlowRef = externalRouterListener.getFlowRefTs(naptSwitch,
                     NwConstants.NAPT_PFIB_TABLE, subnetVpnId);
@@ -232,14 +233,13 @@ public class NaptSwitchHA {
         // Remove the NAPT_PFIB_TABLE(47) flow entry forwards the packet to Fib Table for outbound traffic
         // matching on the vpn ID.
         boolean switchSharedByRouters = false;
-        Uuid extNetworkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
         if (extNetworkId != null && !NatUtil.checkForRoutersWithSameExtNetAndNaptSwitch(
-            dataBroker, networkId, routerName, naptSwitch)) {
+            dataBroker, extNetworkId, routerName, naptSwitch)) {
             List<String> routerNamesAssociated = getRouterIdsForExtNetwork(extNetworkId);
             for (String routerNameAssociated : routerNamesAssociated) {
                 if (!routerNameAssociated.equals(routerName)) {
-                    Long routerIdAssociated = NatUtil.getVpnId(dataBroker, routerNameAssociated);
-                    BigInteger naptDpn = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerNameAssociated);
+                    Uint32 routerIdAssociated = NatUtil.getVpnId(dataBroker, routerNameAssociated);
+                    Uint64 naptDpn = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerNameAssociated);
                     if (naptDpn != null && naptDpn.equals(naptSwitch)) {
                         LOG.debug("removeSnatFlowsInOldNaptSwitch : Napt switch {} is also acting as primary "
                             + "for router {}", naptSwitch, routerIdAssociated);
@@ -249,7 +249,7 @@ public class NaptSwitchHA {
                 }
             }
             if (!switchSharedByRouters) {
-                Long vpnId = getVpnIdForRouter(routerId, extNetworkId);
+                Uint32 vpnId = NatUtil.getVpnId(dataBroker,externalVpnName);
                 if (vpnId != NatConstants.INVALID_ID) {
                     String naptFibflowRef =
                         externalRouterListener.getFlowRefTs(naptSwitch, NwConstants.NAPT_PFIB_TABLE, vpnId);
@@ -269,22 +269,22 @@ public class NaptSwitchHA {
         //Remove Fib entries,tables 20->44 ,36-> 44
         String gwMacAddress = NatUtil.getExtGwMacAddFromRouterName(dataBroker, routerName);
         if (externalIpmap != null && !externalIpmap.isEmpty()) {
-            for (Entry<String, Long> entry : externalIpmap.entrySet()) {
+            for (Entry<String, Uint32> entry : externalIpmap.entrySet()) {
                 String externalIp = entry.getKey();
-                Long label = entry.getValue();
-                externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerId, externalIp, vpnName,
+                Uint32 label = entry.getValue();
+                externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerName, routerId, externalIp, vpnName,
                     extNetworkId, label, gwMacAddress, true, confTx);
                 LOG.debug("removeSnatFlowsInOldNaptSwitch : Successfully removed fib entries in old naptswitch {} "
                     + "for router {} and externalIps {} label {}", naptSwitch, routerId, externalIp, label);
             }
         } else {
             List<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerName);
-            if (networkId != null) {
-                externalRouterListener.clearFibTsAndReverseTraffic(naptSwitch, routerId, networkId,
+            if (extNetworkId != null) {
+                externalRouterListener.clearFibTsAndReverseTraffic(naptSwitch, routerId, extNetworkId,
                     externalIps, null, gwMacAddress, confTx);
                 LOG.debug(
                     "removeSnatFlowsInOldNaptSwitch : Successfully removed fib entries in old naptswitch {} for "
-                        + "router {} with networkId {} and externalIps {}", naptSwitch, routerId, networkId,
+                        + "router {} with networkId {} and externalIps {}", naptSwitch, routerId, extNetworkId,
                     externalIps);
             } else {
                 LOG.debug("removeSnatFlowsInOldNaptSwitch : External network not associated to router {}",
@@ -302,15 +302,16 @@ public class NaptSwitchHA {
                 + "no flows need to be removed in oldNaptSwitch {}", routerId, naptSwitch);
             return;
         }
-        BigInteger cookieSnatFlow = NatUtil.getCookieNaptFlow(routerId);
+        Uint64 cookieSnatFlow = NatUtil.getCookieNaptFlow(routerId);
         List<IntextIpProtocolType> intextIpProtocolTypes = ipPortMapping.getIntextIpProtocolType();
         for (IntextIpProtocolType intextIpProtocolType : intextIpProtocolTypes) {
             if (intextIpProtocolType.getIpPortMap() == null || intextIpProtocolType.getIpPortMap().isEmpty()) {
                 LOG.debug("removeSnatFlowsInOldNaptSwitch : No {} session associated to router {},"
                         + "no flows need to be removed in oldNaptSwitch {}",
                     intextIpProtocolType.getProtocol(), routerId, naptSwitch);
-                break;
+                continue;
             }
+            String protocol = intextIpProtocolType.getProtocol().name();
             List<IpPortMap> ipPortMaps = intextIpProtocolType.getIpPortMap();
             for (IpPortMap ipPortMap : ipPortMaps) {
                 String ipPortInternal = ipPortMap.getIpPortInternal();
@@ -325,7 +326,7 @@ public class NaptSwitchHA {
                 //Build and remove flow in outbound NAPT table
                 String switchFlowRef =
                     NatUtil.getNaptFlowRef(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, String.valueOf(routerId),
-                        internalIp, Integer.parseInt(internalPort));
+                        internalIp, Integer.parseInt(internalPort), protocol);
                 FlowEntity outboundNaptFlowEntity =
                     NatUtil.buildFlowEntity(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE,
                         cookieSnatFlow, switchFlowRef);
@@ -334,20 +335,10 @@ public class NaptSwitchHA {
                     + "with the DPN ID {} and router ID {}", NwConstants.OUTBOUND_NAPT_TABLE, naptSwitch, routerId);
                 mdsalManager.removeFlow(confTx, outboundNaptFlowEntity);
 
-                IpPortExternal ipPortExternal = ipPortMap.getIpPortExternal();
-                if (ipPortExternal == null) {
-                    LOG.debug(
-                        "removeSnatFlowsInOldNaptSwitch : External Ipport mapping not found for internalIp {} "
-                            + "with port {} for router {}", internalIp, internalPort, routerId);
-                    continue;
-                }
-                String externalIp = ipPortExternal.getIpAddress();
-                int externalPort = ipPortExternal.getPortNum();
-
                 //Build and remove flow in  inbound NAPT table
                 switchFlowRef =
                     NatUtil.getNaptFlowRef(naptSwitch, NwConstants.INBOUND_NAPT_TABLE, String.valueOf(routerId),
-                        externalIp, externalPort);
+                        internalIp, Integer.parseInt(internalPort), protocol);
                 FlowEntity inboundNaptFlowEntity =
                     NatUtil.buildFlowEntity(naptSwitch, NwConstants.INBOUND_NAPT_TABLE,
                         cookieSnatFlow, switchFlowRef);
@@ -360,7 +351,7 @@ public class NaptSwitchHA {
         }
     }
 
-    @Nonnull
+    @NonNull
     private List<String> getRouterIdsForExtNetwork(Uuid extNetworkId) {
         List<String> routerUuidsAsString = new ArrayList<>();
         InstanceIdentifier<Networks> extNetwork = InstanceIdentifier.builder(ExternalNetworks.class)
@@ -379,8 +370,8 @@ public class NaptSwitchHA {
         return routerUuidsAsString;
     }
 
-    public boolean isNaptSwitchDown(String routerName, Long routerId, BigInteger dpnId, BigInteger naptSwitch,
-                                    Long routerVpnId, Collection<String> externalIpCache,
+    public boolean isNaptSwitchDown(String routerName, Uint32 routerId, Uint64 dpnId, Uint64 naptSwitch,
+                                    Uint32 routerVpnId, Collection<String> externalIpCache,
                                     TypedReadWriteTransaction<Configuration> confTx)
             throws ExecutionException, InterruptedException {
         return isNaptSwitchDown(routerName, routerId, dpnId, naptSwitch, routerVpnId, externalIpCache, true,
@@ -389,8 +380,8 @@ public class NaptSwitchHA {
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public boolean isNaptSwitchDown(String routerName, Long routerId, BigInteger dpnId, BigInteger naptSwitch,
-                                    Long routerVpnId, Collection<String> externalIpCache, boolean isClearBgpRts,
+    public boolean isNaptSwitchDown(String routerName, Uint32 routerId, Uint64 dpnId, Uint64 naptSwitch,
+                                    Uint32 routerVpnId, Collection<String> externalIpCache, boolean isClearBgpRts,
                                     TypedReadWriteTransaction<Configuration> confTx)
             throws ExecutionException, InterruptedException {
         externalIpsCache = externalIpCache;
@@ -407,7 +398,7 @@ public class NaptSwitchHA {
         Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
         String vpnName = getExtNetworkVpnName(routerName, networkId);
         //elect a new NaptSwitch
-        naptSwitch = naptSwitchSelector.selectNewNAPTSwitch(routerName);
+        naptSwitch = naptSwitchSelector.selectNewNAPTSwitch(routerName, Arrays.asList(naptSwitch));
         if (natMode == NatMode.Conntrack) {
             Routers extRouters = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
             natServiceManager.notify(confTx, extRouters, null, dpnId, dpnId,
@@ -423,7 +414,7 @@ public class NaptSwitchHA {
                         SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
             }
         } else {
-            if (naptSwitch.equals(BigInteger.ZERO)) {
+            if (naptSwitch.equals(Uint64.ZERO)) {
                 LOG.warn("isNaptSwitchDown : No napt switch is elected since all the switches for router {}"
                         + " are down. SNAT IS NOT SUPPORTED FOR ROUTER {}", routerName, routerName);
                 boolean naptUpdatedStatus = updateNaptSwitch(routerName, naptSwitch);
@@ -488,7 +479,7 @@ public class NaptSwitchHA {
                 mdsalManager.addFlow(confTx, flowEntity);
             }
 
-            installSnatFlows(routerName, routerId, naptSwitch, routerVpnId, confTx);
+            installSnatFlows(routerName, routerId, naptSwitch, routerVpnId, networkId, vpnName, confTx);
 
             boolean flowInstalledStatus = handleNatFlowsInNewNaptSwitch(routerName, routerId, dpnId, naptSwitch,
                     routerVpnId, networkId);
@@ -501,12 +492,19 @@ public class NaptSwitchHA {
             }
 
             //remove group in new naptswitch, coz this switch acted previously as ordinary switch
-            long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(routerName), idManager);
-            try {
-                LOG.info("isNaptSwitchDown : Removing NAPT Group in new naptSwitch {}", naptSwitch);
-                mdsalManager.removeGroup(confTx, naptSwitch, groupId);
-            } catch (Exception ex) {
-                LOG.error("isNaptSwitchDown : Failed to remove group in new naptSwitch {}", naptSwitch, ex);
+            Uint32 groupId = NatUtil.getUniqueId(idManager, NatConstants.SNAT_IDPOOL_NAME,
+                NatUtil.getGroupIdKey(routerName));
+            if (groupId != NatConstants.INVALID_ID) {
+                try {
+                    LOG.info("isNaptSwitchDown : Removing NAPT Group in new naptSwitch {}",
+                        naptSwitch);
+                    mdsalManager.removeGroup(confTx, naptSwitch, groupId.longValue());
+                } catch (Exception ex) {
+                    LOG.error("isNaptSwitchDown : Failed to remove group in new naptSwitch {}",
+                        naptSwitch, ex);
+                }
+            } else {
+                LOG.error("NAT Service : Unable to obtain groupId for router:{}", routerName);
             }
         }
         return true;
@@ -531,29 +529,35 @@ public class NaptSwitchHA {
         return null;
     }
 
-    public void updateNaptSwitchBucketStatus(String routerName, long routerId, BigInteger naptSwitch) {
+    public void updateNaptSwitchBucketStatus(String routerName, Uint32 routerId, Uint64 naptSwitch) {
         LOG.debug("updateNaptSwitchBucketStatus : called");
 
-        List<BigInteger> dpnList = naptSwitchSelector.getDpnsForVpn(routerName);
+        List<Uint64> dpnList = naptSwitchSelector.getDpnsForVpn(routerName);
         //List<BigInteger> dpnList = getDpnListForRouter(routerName);
         if (dpnList.isEmpty()) {
             LOG.warn("updateNaptSwitchBucketStatus : No switches found for router {}", routerName);
             return;
         }
-        for (BigInteger dpn : dpnList) {
+        for (Uint64 dpn : dpnList) {
             if (!dpn.equals(naptSwitch)) {
                 LOG.debug("updateNaptSwitchBucketStatus : Updating SNAT_TABLE missentry for DpnId {} "
                         + "which is not naptSwitch for router {}", dpn, routerName);
                 List<BucketInfo> bucketInfoList = handleGroupInNeighborSwitches(dpn, routerName, routerId, naptSwitch);
+                if (bucketInfoList.isEmpty()) {
+                    LOG.error("Failed to populate bucketInfo for non-napt switch {} whose naptSwitch:{} for router:{}",
+                        dpn,naptSwitch,routerName);
+                    continue;
+                }
                 modifySnatGroupEntry(dpn, bucketInfoList, routerName);
+                externalRouterListener.installSnatMissEntry(dpn, bucketInfoList, routerName, routerId);
             }
         }
     }
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    private boolean handleNatFlowsInNewNaptSwitch(String routerName, Long routerId, BigInteger oldNaptSwitch,
-                                                    BigInteger newNaptSwitch, Long routerVpnId, Uuid networkId) {
+    private boolean handleNatFlowsInNewNaptSwitch(String routerName, Uint32 routerId, Uint64 oldNaptSwitch,
+                                                    Uint64 newNaptSwitch, Uint32 routerVpnId, Uuid networkId) {
         LOG.debug("handleNatFlowsInNewNaptSwitch : Proceeding to install flows in newNaptSwitch {} for routerId {}",
                 newNaptSwitch, routerId);
         IpPortMapping ipPortMapping = NatUtil.getIportMapping(dataBroker, routerId);
@@ -564,12 +568,12 @@ public class NaptSwitchHA {
             return true;
         }
         //getvpnId
-        Long vpnId = getVpnIdForRouter(routerId, networkId);
+        Uint32 vpnId = getVpnIdForRouter(routerId, networkId);
         if (vpnId == NatConstants.INVALID_ID) {
             LOG.error("handleNatFlowsInNewNaptSwitch : Invalid vpnId for routerId {}", routerId);
             return false;
         }
-        Long bgpVpnId;
+        Uint32 bgpVpnId;
         if (routerId.equals(routerVpnId)) {
             bgpVpnId = NatConstants.INVALID_ID;
         } else {
@@ -609,7 +613,7 @@ public class NaptSwitchHA {
                     continue;
                 }
                 String externalIpAddress = ipPortExternal.getIpAddress();
-                Integer extportNumber = ipPortExternal.getPortNum();
+                Integer extportNumber = ipPortExternal.getPortNum().toJava();
                 LOG.debug("handleNatFlowsInNewNaptSwitch : ExternalIPport {}:{} mapping for internal ipport {}:{}",
                         externalIpAddress, extportNumber, internalIpAddress, intportnum);
 
@@ -660,7 +664,7 @@ public class NaptSwitchHA {
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    private Long getVpnIdForRouter(Long routerId, Uuid networkId) {
+    private Uint32 getVpnIdForRouter(Uint32 routerId, Uuid networkId) {
         try {
             //getvpnId
             if (networkId == null) {
@@ -671,8 +675,8 @@ public class NaptSwitchHA {
                     LOG.debug("getVpnIdForRouter : vpn is not associated for network {} in router {}",
                             networkId, routerId);
                 } else {
-                    Long vpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
-                    if (vpnId > 0) {
+                    Uint32 vpnId = NatUtil.getVpnId(dataBroker, vpnUuid.getValue());
+                    if (vpnId.longValue() > 0) {
                         LOG.debug("getVpnIdForRouter : retrieved vpnId {} for router {}", vpnId, routerId);
                         return vpnId;
                     } else {
@@ -686,18 +690,9 @@ public class NaptSwitchHA {
         return NatConstants.INVALID_ID;
     }
 
-    public List<BucketInfo> handleGroupInPrimarySwitch() {
-        List<BucketInfo> listBucketInfo = new ArrayList<>();
-        List<ActionInfo> listActionInfoPrimary = new ArrayList<>();
-        listActionInfoPrimary.add(new ActionNxResubmit(NwConstants.INTERNAL_TUNNEL_TABLE));
-        BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
-        listBucketInfo.add(bucketPrimary);
-        return listBucketInfo;
-    }
-
-    @Nonnull
-    public List<BucketInfo> handleGroupInNeighborSwitches(BigInteger dpnId, String routerName, long routerId,
-            BigInteger naptSwitch) {
+    @NonNull
+    public List<BucketInfo> handleGroupInNeighborSwitches(Uint64 dpnId, String routerName, Uint32 routerId,
+            Uint64 naptSwitch) {
         List<BucketInfo> listBucketInfo = new ArrayList<>();
         String ifNamePrimary;
         if (routerId == NatConstants.INVALID_ID) {
@@ -722,28 +717,35 @@ public class NaptSwitchHA {
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    protected void installSnatGroupEntry(BigInteger dpnId, List<BucketInfo> bucketInfo, String routerName) {
+    protected void installSnatGroupEntry(Uint64 dpnId, List<BucketInfo> bucketInfo, String routerName) {
         GroupEntity groupEntity = null;
         try {
-            long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(routerName), idManager);
-            LOG.debug("installSnatGroupEntry : install SnatMissEntry for groupId {} for dpnId {} for router {}",
+            Uint32 groupId = NatUtil.getUniqueId(idManager, NatConstants.SNAT_IDPOOL_NAME,
+                NatUtil.getGroupIdKey(routerName));
+            if (groupId != NatConstants.INVALID_ID) {
+                LOG.debug(
+                    "installSnatGroupEntry : install SnatMissEntry for groupId {} for dpnId {} for router {}",
                     groupId, dpnId, routerName);
-            groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName,
-                GroupTypes.GroupAll, bucketInfo);
-            mdsalManager.syncInstallGroup(groupEntity);
-            LOG.debug("installSnatGroupEntry : installed the SNAT to NAPT GroupEntity:{}", groupEntity);
+                groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId.longValue(), routerName,
+                    GroupTypes.GroupAll, bucketInfo);
+                mdsalManager.syncInstallGroup(groupEntity);
+                LOG.debug("installSnatGroupEntry : installed the SNAT to NAPT GroupEntity:{}",
+                    groupEntity);
+            } else {
+                LOG.error("installSnatGroupEntry: Unable to obtain groupId for router:{}", routerName);
+            }
         } catch (Exception ex) {
             LOG.error("installSnatGroupEntry : Failed to install group for groupEntity {}", groupEntity, ex);
         }
     }
 
-    private void modifySnatGroupEntry(BigInteger dpnId, List<BucketInfo> bucketInfo, String routerName) {
+    private void modifySnatGroupEntry(Uint64 dpnId, List<BucketInfo> bucketInfo, String routerName) {
         installSnatGroupEntry(dpnId, bucketInfo, routerName);
         LOG.debug("modifySnatGroupEntry : modified SnatMissEntry for dpnId {} of router {}", dpnId, routerName);
     }
 
     @Nullable
-    protected String getTunnelInterfaceName(BigInteger srcDpId, BigInteger dstDpId) {
+    protected String getTunnelInterfaceName(Uint64 srcDpId, Uint64 dstDpId) {
         Class<? extends TunnelTypeBase> tunType = TunnelTypeVxlan.class;
         RpcResult<GetTunnelInterfaceNameOutput> rpcResult;
 
@@ -781,7 +783,7 @@ public class NaptSwitchHA {
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public boolean updateNaptSwitch(String routerName, BigInteger naptSwitchId) {
+    public boolean updateNaptSwitch(String routerName, Uint64 naptSwitchId) {
         RouterToNaptSwitch naptSwitch = new RouterToNaptSwitchBuilder().withKey(new RouterToNaptSwitchKey(routerName))
             .setPrimarySwitchId(naptSwitchId).build();
         try {
@@ -797,20 +799,21 @@ public class NaptSwitchHA {
         return true;
     }
 
-    public FlowEntity buildSnatFlowEntity(BigInteger dpId, String routerName, long groupId,
-                                          long routerVpnId, int addordel) {
+    public FlowEntity buildSnatFlowEntity(Uint64 dpId, String routerName, long groupId,
+                                          Uint32 routerVpnId, int addordel) {
         FlowEntity flowEntity;
         List<MatchInfo> matches = new ArrayList<>();
         matches.add(MatchEthernetType.IPV4);
-        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerVpnId), MetaDataUtil.METADATA_MASK_VRFID));
+        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerVpnId.longValue()),
+                MetaDataUtil.METADATA_MASK_VRFID));
 
         String flowRef = getFlowRefSnat(dpId, NwConstants.PSNAT_TABLE, routerName);
 
         if (addordel == NatConstants.ADD_FLOW) {
             List<ActionInfo> actionsInfo = new ArrayList<>();
-            long tunnelId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, natOverVxlanUtil, elanManager,
+            Uint64 tunnelId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, natOverVxlanUtil, elanManager,
                     idManager, routerVpnId, routerName);
-            actionsInfo.add(new ActionSetFieldTunnelId(BigInteger.valueOf(tunnelId)));
+            actionsInfo.add(new ActionSetFieldTunnelId(tunnelId));
             LOG.debug("buildSnatFlowEntity : Setting the tunnel to the list of action infos {}", actionsInfo);
             actionsInfo.add(new ActionGroup(groupId));
             List<InstructionInfo> instructions = new ArrayList<>();
@@ -827,12 +830,13 @@ public class NaptSwitchHA {
         return flowEntity;
     }
 
-    public FlowEntity buildSnatFlowEntityForNaptSwitch(BigInteger dpId, String routerName,
-                                                       long routerVpnId, int addordel) {
+    public FlowEntity buildSnatFlowEntityForNaptSwitch(Uint64 dpId, String routerName,
+                                                       Uint32 routerVpnId, int addordel) {
         FlowEntity flowEntity;
         List<MatchInfo> matches = new ArrayList<>();
         matches.add(MatchEthernetType.IPV4);
-        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerVpnId), MetaDataUtil.METADATA_MASK_VRFID));
+        matches.add(new MatchMetadata(MetaDataUtil.getVpnIdMetadata(routerVpnId.longValue()),
+                MetaDataUtil.METADATA_MASK_VRFID));
 
         String flowRef = getFlowRefSnat(dpId, NwConstants.PSNAT_TABLE, routerName);
 
@@ -852,13 +856,13 @@ public class NaptSwitchHA {
         return flowEntity;
     }
 
-    private String getFlowRefSnat(BigInteger dpnId, short tableId, String routerID) {
+    private String getFlowRefSnat(Uint64 dpnId, short tableId, String routerID) {
         return NatConstants.SNAT_FLOWID_PREFIX + dpnId + NatConstants.FLOWID_SEPARATOR + tableId + NatConstants
                 .FLOWID_SEPARATOR + routerID;
     }
 
-    protected void installSnatFlows(String routerName, Long routerId, BigInteger naptSwitch, Long routerVpnId,
-                                    TypedReadWriteTransaction<Configuration> confTx) {
+    protected void installSnatFlows(String routerName, Uint32 routerId, Uint64 naptSwitch, Uint32 routerVpnId,
+        Uuid networkId, String vpnName, TypedReadWriteTransaction<Configuration> confTx) {
 
         if (routerId.equals(routerVpnId)) {
             LOG.debug("installSnatFlows : Installing flows for router with internalvpnId");
@@ -912,34 +916,40 @@ public class NaptSwitchHA {
             externalRouterListener.installNaptPfibEntryWithBgpVpn(naptSwitch, routerId, routerVpnId, confTx);
         }
 
-        Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
-        String vpnName = getExtNetworkVpnName(routerName, networkId);
         if (vpnName != null) {
             //NAPT PFIB point to FIB table for outbound traffic
-            long vpnId = NatUtil.getVpnId(dataBroker, vpnName);
-            boolean shouldInstallNaptPfibWithExtNetworkVpnId = true;
-            Collection<Uuid> externalSubnetIds = NatUtil.getExternalSubnetIdsForRouter(dataBroker, routerName);
-            if (!externalSubnetIds.isEmpty()) {
-                //NAPT PFIB point to FIB table for outbound traffic - using external subnetID as vpnID.
-                for (Uuid externalSubnetId : externalSubnetIds) {
-                    long externalSubnetVpnId = NatUtil.getExternalSubnetVpnId(dataBroker, externalSubnetId);
-                    if (externalSubnetVpnId != NatConstants.INVALID_ID) {
-                        shouldInstallNaptPfibWithExtNetworkVpnId = false;
-                        LOG.debug("installSnatFlows : installNaptPfibEntry fin naptswitch with dpnId {} for "
-                                + "BgpVpnId {}", naptSwitch, externalSubnetVpnId);
-                        externalRouterListener.installNaptPfibEntry(naptSwitch, externalSubnetVpnId, confTx);
+            Uint32 vpnId = NatUtil.getVpnId(dataBroker, vpnName);
+            if (vpnName.equals(networkId.getValue())) {
+                // below condition valid only for flat/vlan use-case
+                boolean shouldInstallNaptPfibWithExtNetworkVpnId = true;
+                Collection<Uuid> externalSubnetIds = NatUtil
+                    .getExternalSubnetIdsForRouter(dataBroker, routerName);
+                if (!externalSubnetIds.isEmpty()) {
+                    //NAPT PFIB point to FIB table for outbound traffic - using external subnetID as vpnID.
+                    for (Uuid externalSubnetId : externalSubnetIds) {
+                        Uint32 externalSubnetVpnId = NatUtil
+                            .getExternalSubnetVpnId(dataBroker, externalSubnetId);
+                        if (externalSubnetVpnId != NatConstants.INVALID_ID) {
+                            shouldInstallNaptPfibWithExtNetworkVpnId = false;
+                            LOG.debug(
+                                "installSnatFlows : installNaptPfibEntry fin naptswitch with dpnId {} for "
+                                    + "BgpVpnId {}", naptSwitch, externalSubnetVpnId);
+                            externalRouterListener
+                                .installNaptPfibEntry(naptSwitch, externalSubnetVpnId, confTx);
+                        }
                     }
                 }
+                if (vpnId != NatConstants.INVALID_ID && shouldInstallNaptPfibWithExtNetworkVpnId) {
+                    //NAPT PFIB table point to FIB table for outbound traffic - using external networkID as vpnID.
+                    LOG.debug(
+                        "installSnatFlows : installNaptPfibEntry fin naptswitch with dpnId {} for "
+                            + "BgpVpnId {}", naptSwitch, vpnId);
+                    externalRouterListener.installNaptPfibEntry(naptSwitch, vpnId, confTx);
+                } else if (vpnId != NatConstants.INVALID_ID) {
+                    LOG.debug("installSnatFlows : Associated BgpvpnId not found for router {}",
+                        routerId);
+                }
             }
-            if (vpnId != NatConstants.INVALID_ID && shouldInstallNaptPfibWithExtNetworkVpnId) {
-                //NAPT PFIB table point to FIB table for outbound traffic - using external networkID as vpnID.
-                LOG.debug("installSnatFlows : installNaptPfibEntry fin naptswitch with dpnId {} for "
-                    + "BgpVpnId {}", naptSwitch, vpnId);
-                externalRouterListener.installNaptPfibEntry(naptSwitch, vpnId, confTx);
-            } else if (vpnId != NatConstants.INVALID_ID) {
-                LOG.debug("installSnatFlows : Associated BgpvpnId not found for router {}", routerId);
-            }
-
             //Install Fib entries for ExternalIps & program 36 -> 44
             Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
             String rd = NatUtil.getVpnRd(dataBroker, vpnName);
@@ -957,7 +967,7 @@ public class NaptSwitchHA {
         }
     }
 
-    protected void bestEffortDeletion(long routerId, String routerName, Map<String, Long> externalIpLabel,
+    protected void bestEffortDeletion(Uint32 routerId, String routerName, Map<String, Uint32> externalIpLabel,
                                       TypedReadWriteTransaction<Configuration> confTx)
             throws ExecutionException, InterruptedException {
         Collection<String> newExternalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
@@ -975,8 +985,8 @@ public class NaptSwitchHA {
                 LOG.error("bestEffortDeletion : Vpn is not associated to externalN/w of router {}", routerName);
                 return;
             }
-            BigInteger naptSwitch = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
-            if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
+            Uint64 naptSwitch = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
+            if (naptSwitch == null || naptSwitch.equals(Uint64.ZERO)) {
                 LOG.error("bestEffortDeletion : No naptSwitch is selected for router {}", routerName);
                 return;
             }
@@ -996,8 +1006,8 @@ public class NaptSwitchHA {
             if (extNwProvType == ProviderTypes.VXLAN) {
                 for (String externalIp : removedExternalIps) {
                     externalRouterListener.clearBgpRoutes(externalIp, vpnName);
-                    externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerId, externalIp, vpnName,
-                        networkId, NatConstants.DEFAULT_LABEL_VALUE, gwMacAddress, true, confTx);
+                    externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerName, routerId, externalIp,
+                        vpnName, networkId, NatConstants.DEFAULT_LABEL_VALUE, gwMacAddress, true, confTx);
                     LOG.debug("bestEffortDeletion : Successfully removed fib entry for externalIp {} for routerId {} "
                                     + "on NAPT switch {} ", externalIp, routerId, naptSwitch);
                 }
@@ -1006,7 +1016,7 @@ public class NaptSwitchHA {
                     LOG.error("bestEffortDeletion : ExternalIpLabel map is empty for router {}", routerName);
                     return;
                 }
-                Long label;
+                Uint32 label;
                 for (String externalIp : removedExternalIps) {
                     if (externalIpLabel.containsKey(externalIp)) {
                         label = externalIpLabel.get(externalIp);
@@ -1018,8 +1028,8 @@ public class NaptSwitchHA {
                         continue;
                     }
                     externalRouterListener.clearBgpRoutes(externalIp, vpnName);
-                    externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerId, externalIp, vpnName,
-                        networkId, label, gwMacAddress, true, confTx);
+                    externalRouterListener.delFibTsAndReverseTraffic(naptSwitch, routerName, routerId, externalIp,
+                            vpnName, networkId, label, gwMacAddress, true, confTx);
                     LOG.debug("bestEffortDeletion : Successfully removed fib entries in switch {} for router {} "
                             + "and externalIps {}", naptSwitch, routerId, externalIp);
                 }
@@ -1041,7 +1051,7 @@ public class NaptSwitchHA {
         }
     }
 
-    protected void subnetRegisterMapping(Routers routerEntry, Long segmentId) {
+    protected void subnetRegisterMapping(Routers routerEntry, Uint32 segmentId) {
         externalRouterListener.subnetRegisterMapping(routerEntry, segmentId);
     }
 }