FIP support for Octavia VIPs
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / RouterToVpnListener.java
index de48cc1fdfe8df12f794f1d0b6ee4fb243818ebc..68341c7466a04a8532c4dface7000af2aded1c1b 100644 (file)
@@ -7,14 +7,18 @@
  */
 package org.opendaylight.netvirt.natservice.internal;
 
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+
 import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.util.List;
+import java.util.concurrent.ExecutionException;
 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.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
@@ -33,6 +37,7 @@ import org.slf4j.LoggerFactory;
 public class RouterToVpnListener implements NeutronvpnListener {
     private static final Logger LOG = LoggerFactory.getLogger(RouterToVpnListener.class);
     private final DataBroker dataBroker;
+    private final ManagedNewTransactionRunner txRunner;
     private final FloatingIPListener floatingIpListener;
     private final OdlInterfaceRpcService interfaceManager;
     private final ExternalRoutersListener externalRoutersListener;
@@ -43,6 +48,7 @@ public class RouterToVpnListener implements NeutronvpnListener {
                                final OdlInterfaceRpcService interfaceManager,
                                final ExternalRoutersListener externalRoutersListener) {
         this.dataBroker = dataBroker;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.floatingIpListener = floatingIpListener;
         this.interfaceManager = interfaceManager;
         this.externalRoutersListener = externalRoutersListener;
@@ -55,33 +61,35 @@ public class RouterToVpnListener implements NeutronvpnListener {
     public void onRouterAssociatedToVpn(RouterAssociatedToVpn notification) {
         String routerName = notification.getRouterId().getValue();
         String vpnName = notification.getVpnId().getValue();
-        WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
         //check router is associated to external network
         String extNetwork = NatUtil.getAssociatedExternalNetwork(dataBroker, routerName);
         if (extNetwork != null) {
-            LOG.debug("onRouterAssociatedToVpn : Router {} is associated with ext nw {}", routerName, extNetwork);
-            handleDNATConfigurationForRouterAssociation(routerName, vpnName, extNetwork);
-            Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
-            if (extNetworkUuid == null) {
-                LOG.error("onRouterAssociatedToVpn : Unable to retrieve external network Uuid for router {}",
+            try {
+                LOG.debug("onRouterAssociatedToVpn : Router {} is associated with ext nw {}", routerName, extNetwork);
+                handleDNATConfigurationForRouterAssociation(routerName, vpnName, extNetwork);
+                Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
+                if (extNetworkUuid == null) {
+                    LOG.error("onRouterAssociatedToVpn : Unable to retrieve external network Uuid for router {}",
                         routerName);
-                return;
-            }
-            ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
+                    return;
+                }
+                ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
                     extNetworkUuid);
-            if (extNwProvType == null) {
-                LOG.error("onRouterAssociatedToVpn : External Network Provider Type missing");
-                return;
+                if (extNwProvType == null) {
+                    LOG.error("onRouterAssociatedToVpn : External Network Provider Type missing");
+                    return;
+                }
+                long routerId = NatUtil.getVpnId(dataBroker, routerName);
+                txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                    tx -> externalRoutersListener.changeLocalVpnIdToBgpVpnId(routerName, routerId, vpnName, tx,
+                            extNwProvType)).get();
+            } catch (InterruptedException | ExecutionException e) {
+                LOG.error("Error changling local VPN identifier to BGP VPN identifier", e);
             }
-            long routerId = NatUtil.getVpnId(dataBroker, routerName);
-            externalRoutersListener.changeLocalVpnIdToBgpVpnId(routerName, routerId, vpnName, writeFlowInvTx,
-                    extNwProvType);
         } else {
             LOG.debug("onRouterAssociatedToVpn : Ignoring the Router {} association with VPN {} "
                     + "since it is not external router", routerName, vpnName);
         }
-
-        NatUtil.waitForTransactionToComplete(writeFlowInvTx);
     }
 
     /**
@@ -91,36 +99,40 @@ public class RouterToVpnListener implements NeutronvpnListener {
     public void onRouterDisassociatedFromVpn(RouterDisassociatedFromVpn notification) {
         String routerName = notification.getRouterId().getValue();
         String vpnName = notification.getVpnId().getValue();
-        WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
         //check router is associated to external network
         String extNetwork = NatUtil.getAssociatedExternalNetwork(dataBroker, routerName);
         if (extNetwork != null) {
-            LOG.debug("onRouterDisassociatedFromVpn : Router {} is associated with ext nw {}", routerName, extNetwork);
-            handleDNATConfigurationForRouterDisassociation(routerName, vpnName, extNetwork);
-            Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
-            if (extNetworkUuid == null) {
-                LOG.error("onRouterDisassociatedFromVpn : Unable to retrieve external network Uuid for router {}",
-                        routerName);
-                return;
+            try {
+                LOG.debug("onRouterDisassociatedFromVpn : Router {} is associated with ext nw {}", routerName,
+                    extNetwork);
+                handleDNATConfigurationForRouterDisassociation(routerName, vpnName, extNetwork);
+                Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
+                if (extNetworkUuid == null) {
+                    LOG.error("onRouterDisassociatedFromVpn : Unable to retrieve external network Uuid for router {}",
+                            routerName);
+                    return;
+                }
+                ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
+                        extNetworkUuid);
+                if (extNwProvType == null) {
+                    LOG.error("onRouterDisassociatedFromVpn : External Network Provider Type missing");
+                    return;
+                }
+                long routerId = NatUtil.getVpnId(dataBroker, routerName);
+                txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                    tx -> externalRoutersListener.changeBgpVpnIdToLocalVpnId(routerName, routerId, vpnName, tx,
+                            extNwProvType)).get();
+            } catch (InterruptedException | ExecutionException e) {
+                LOG.error("Error changing BGP VPN identifier to local VPN identifier", e);
             }
-            ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
-                    extNetworkUuid);
-            if (extNwProvType == null) {
-                LOG.error("onRouterDisassociatedFromVpn : External Network Provider Type missing");
-                return;
-            }
-            long routerId = NatUtil.getVpnId(dataBroker, routerName);
-            externalRoutersListener.changeBgpVpnIdToLocalVpnId(routerName, routerId, vpnName, writeFlowInvTx,
-                    extNwProvType);
         } else {
             LOG.debug("onRouterDisassociatedFromVpn : Ignoring the Router {} association with VPN {} "
                     + "since it is not external router", routerName, vpnName);
         }
-
-        NatUtil.waitForTransactionToComplete(writeFlowInvTx);
     }
 
-    void handleDNATConfigurationForRouterAssociation(String routerName, String vpnName, String externalNetwork) {
+    void handleDNATConfigurationForRouterAssociation(String routerName, String vpnName, String externalNetwork)
+            throws ExecutionException, InterruptedException {
         InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerName);
         Optional<RouterPorts> optRouterPorts =
             MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
@@ -154,7 +166,8 @@ public class RouterToVpnListener implements NeutronvpnListener {
         }
     }
 
-    void handleDNATConfigurationForRouterDisassociation(String routerName, String vpnName, String externalNetwork) {
+    void handleDNATConfigurationForRouterDisassociation(String routerName, String vpnName, String externalNetwork)
+            throws ExecutionException, InterruptedException {
         InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerName);
         Optional<RouterPorts> optRouterPorts =
             MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);