DS READ Optimization in NAT
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / NaptSwitchHA.java
index cdb1fea05ad72bb826662e4df16dcbe26ea87cf2..3caca65175198d26409700c5ffa7e64fdecb2bc1 100644 (file)
@@ -141,20 +141,21 @@ public class NaptSwitchHA {
         this.natOverVxlanUtil = natOverVxlanUtil;
     }
 
-    protected void removeSnatFlowsInOldNaptSwitch(String routerName, Uint32 routerId, Uint64 naptSwitch,
+    protected void removeSnatFlowsInOldNaptSwitch(Routers extRouter, 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 extNetworkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
-        String vpnName = getExtNetworkVpnName(routerName, extNetworkId);
+        String routerName = extRouter.getRouterName();
+        Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
+        String vpnName = getExtNetworkVpnName(routerName, networkId);
         if (vpnName == null) {
             LOG.error("removeSnatFlowsInOldNaptSwitch : Vpn is not associated to externalN/w of router {}",
                 routerName);
             return;
         }
-        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, extNetworkId);
+        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, networkId);
         if (extNwProvType == null) {
             LOG.error("removeSnatFlowsInOldNaptSwitch : Unable to retrieve the External Network Provider Type "
                 + "for Router {}", routerName);
@@ -233,6 +234,7 @@ 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 = extRouter.getNetworkId();
         if (extNetworkId != null && !NatUtil.checkForRoutersWithSameExtNetAndNaptSwitch(
             dataBroker, extNetworkId, routerName, naptSwitch)) {
             List<String> routerNamesAssociated = getRouterIdsForExtNetwork(extNetworkId);
@@ -370,21 +372,22 @@ public class NaptSwitchHA {
         return routerUuidsAsString;
     }
 
-    public boolean isNaptSwitchDown(String routerName, Uint32 routerId, Uint64 dpnId, Uint64 naptSwitch,
+    public boolean isNaptSwitchDown(Routers extRouter, 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,
+        return isNaptSwitchDown(extRouter, routerId, dpnId, naptSwitch, routerVpnId, externalIpCache, true,
                 confTx);
     }
 
     // TODO Clean up the exception handling
     @SuppressWarnings("checkstyle:IllegalCatch")
-    public boolean isNaptSwitchDown(String routerName, Uint32 routerId, Uint64 dpnId, Uint64 naptSwitch,
+    public boolean isNaptSwitchDown(Routers extRouter, Uint32 routerId, Uint64 dpnId, Uint64 naptSwitch,
                                     Uint32 routerVpnId, Collection<String> externalIpCache, boolean isClearBgpRts,
                                     TypedReadWriteTransaction<Configuration> confTx)
             throws ExecutionException, InterruptedException {
         externalIpsCache = externalIpCache;
+        String routerName = extRouter.getRouterName();
         if (!naptSwitch.equals(dpnId)) {
             LOG.debug("isNaptSwitchDown : DpnId {} is not a naptSwitch {} for Router {}",
                     dpnId, naptSwitch, routerName);
@@ -395,7 +398,7 @@ public class NaptSwitchHA {
             LOG.error("isNaptSwitchDown : Invalid routerId returned for routerName {}", routerName);
             return true;
         }
-        Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
+        Uuid networkId = extRouter.getNetworkId();
         String vpnName = getExtNetworkVpnName(routerName, networkId);
         //elect a new NaptSwitch
         naptSwitch = naptSwitchSelector.selectNewNAPTSwitch(routerName, Arrays.asList(naptSwitch));
@@ -918,7 +921,13 @@ public class NaptSwitchHA {
 
         if (vpnName != null) {
             //NAPT PFIB point to FIB table for outbound traffic
-            Uint32 vpnId = NatUtil.getVpnId(dataBroker, vpnName);
+            org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn
+                .instance.to.vpn.id.VpnInstance vpnInstance = NatUtil.getVpnIdToVpnInstance(dataBroker, vpnName);
+            if (vpnInstance == null) {
+                LOG.error("NAT Service : installNaptPfibEntry vpnInstance not found for {}", vpnName);
+                return;
+            }
+            Uint32 vpnId = vpnInstance.getVpnId();
             if (vpnName.equals(networkId.getValue())) {
                 // below condition valid only for flat/vlan use-case
                 boolean shouldInstallNaptPfibWithExtNetworkVpnId = true;
@@ -952,7 +961,7 @@ public class NaptSwitchHA {
             }
             //Install Fib entries for ExternalIps & program 36 -> 44
             Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
-            String rd = NatUtil.getVpnRd(dataBroker, vpnName);
+            String rd = vpnInstance.getVrfId();
             for (String externalIp : externalIps) {
                 removeFibEntry(rd, externalIp);
                 LOG.debug("installSnatFlows : advToBgpAndInstallFibAndTsFlows in naptswitch id {} "