Moving vpn-interface yang from VPNMgr > NeutronVPN
[netvirt.git] / natservice / impl / src / main / java / org / opendaylight / netvirt / natservice / internal / UpgradeStateListener.java
index 8abb6187282f54a8a70fca8974a628e6ebc9e092..29de7588e4bb4b1195c334c9576f7e03de99ec1c 100644 (file)
@@ -9,19 +9,14 @@
 package org.opendaylight.netvirt.natservice.internal;
 
 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
-import static org.opendaylight.netvirt.natservice.internal.NatUtil.requireNonNullElse;
 
 import com.google.common.base.Optional;
-
-import java.math.BigInteger;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-
-import javax.annotation.Nonnull;
 import javax.inject.Inject;
 import javax.inject.Singleton;
-
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -46,6 +41,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.RouterToNaptSwitch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.upgrade.rev180702.UpgradeConfig;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -77,6 +74,8 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.centralizedSwitchScheduler = centralizedSwitchScheduler;
         this.defaultRouteProgrammer = defaultRouteProgrammer;
+        this.mdsalManager = mdsalManager;
+        this.idManager = idManager;
         this.coordinator = coordinator;
         this.naptSwitchHA = naptSwitchHA;
         if (config != null) {
@@ -88,11 +87,11 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
     }
 
     @Override
-    public void add(@Nonnull UpgradeConfig newDataObject) {
+    public void add(@NonNull UpgradeConfig newDataObject) {
     }
 
     @Override
-    public void remove(@Nonnull UpgradeConfig removedDataObject) {
+    public void remove(@NonNull UpgradeConfig removedDataObject) {
         if (natMode == NatserviceConfig.NatMode.Conntrack) {
             return;
         }
@@ -100,14 +99,13 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
     }
 
     @Override
-    public void update(@Nonnull UpgradeConfig original, UpgradeConfig updated) {
+    public void update(@NonNull UpgradeConfig original, UpgradeConfig updated) {
         if (natMode == NatserviceConfig.NatMode.Controller) {
             if (original.isUpgradeInProgress() && !updated.isUpgradeInProgress()) {
                 Optional<NaptSwitches> npatSwitches = NatUtil.getAllPrimaryNaptSwitches(dataBroker);
                 if (npatSwitches.isPresent()) {
-                    for (RouterToNaptSwitch routerToNaptSwitch : requireNonNullElse(
-                            npatSwitches.get().getRouterToNaptSwitch(), Collections.<RouterToNaptSwitch>emptyList())) {
-                        BigInteger primaryNaptDpnId = routerToNaptSwitch.getPrimarySwitchId();
+                    for (RouterToNaptSwitch routerToNaptSwitch : npatSwitches.get().nonnullRouterToNaptSwitch()) {
+                        Uint64 primaryNaptDpnId = routerToNaptSwitch.getPrimarySwitchId();
                         if (!NatUtil.getSwitchStatus(dataBroker, routerToNaptSwitch.getPrimarySwitchId())) {
                             String routerUuid = routerToNaptSwitch.getRouterName();
                             coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + routerUuid,
@@ -137,7 +135,7 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
             return;
         }
 
-        for (Routers router : requireNonNullElse(routers.getRouters(), Collections.<Routers>emptyList())) {
+        for (Routers router : routers.nonnullRouters()) {
             List<ExternalIps> externalIps = router.getExternalIps();
             if (router.isEnableSnat() && externalIps != null && !externalIps.isEmpty()) {
                 centralizedSwitchScheduler.scheduleCentralizedSwitch(router);
@@ -145,7 +143,7 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
         }
     }
 
-    private void reElectNewNaptSwitch(String routerName, BigInteger primaryNaptDpnId,
+    private void reElectNewNaptSwitch(String routerName, Uint64 primaryNaptDpnId,
             TypedReadWriteTransaction<Configuration> confTx) throws ExecutionException, InterruptedException {
         // Check if this is externalRouter else ignore
         InstanceIdentifier<Routers> extRoutersId = NatUtil.buildRouterIdentifier(routerName);
@@ -157,16 +155,17 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
                     routerName);
             return;
         }
-        Uuid networkId = routerData.get().getNetworkId();
+        Routers extRouters = routerData.get();
+        Uuid networkId = extRouters.getNetworkId();
         if (networkId == null) {
             LOG.error("hndlTepDelForSnatInEachRtr : SNAT -> Ignoring Re-election  with Napt {} for router {}"
                     + "as external network configuraton is missing", primaryNaptDpnId, routerName);
             return;
         }
-        long routerId = NatUtil.getVpnId(dataBroker, routerName);
+        Uint32 routerId = NatUtil.getVpnId(dataBroker, routerName);
         LOG.debug("hndlTepDelForSnatInEachRtr : SNAT->Router {} is associated with ext nw {}", routerId, networkId);
         Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
-        Long bgpVpnId;
+        Uint32 bgpVpnId;
         if (bgpVpnUuid == null) {
             LOG.debug("hndlTepDelForSnatInEachRtr : SNAT->Internal VPN-ID {} associated to router {}",
                     routerId, routerName);
@@ -183,7 +182,7 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
         if (routerData.get().isEnableSnat()) {
             LOG.info("hndlTepDelForSnatInEachRtr : SNAT enabled for router {}", routerId);
 
-            long routerVpnId = routerId;
+            Uint32 routerVpnId = routerId;
             if (bgpVpnId != NatConstants.INVALID_ID) {
                 LOG.debug("hndlTepDelForSnatInEachRtr : SNAT -> Private BGP VPN ID (Internal BGP VPN ID) {} "
                         + "associated to the router {}", bgpVpnId, routerName);
@@ -195,11 +194,12 @@ public class UpgradeStateListener extends AbstractClusteredSyncDataTreeChangeLis
             //Re-elect the other available switch as the NAPT switch and program the NAT flows.
             ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker,
                     routerName, networkId);
+            String externalVpnName = NatUtil.getAssociatedVPN(dataBroker,extRouters.getNetworkId());
             if (extNwProvType == null) {
                 return;
             }
-            NatUtil.removeSNATFromDPN(dataBroker, mdsalManager, idManager, naptSwitchHA, primaryNaptDpnId, routerName,
-                    routerId, routerVpnId, extNwProvType, confTx);
+            NatUtil.removeSNATFromDPN(dataBroker, mdsalManager, idManager, naptSwitchHA, primaryNaptDpnId, extRouters,
+                    routerId, routerVpnId, externalVpnName, extNwProvType, confTx);
 
         } else {
             LOG.info("hndlTepDelForSnatInEachRtr : SNAT is not enabled for router {} to handle addDPN event {}",