NETVIRT-1037: Upstream Fixes --- Review-1.
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / TunnelInterfaceStateListener.java
index 7c9e8ab74e693e120b203b0f99a15970bf570370..4ad3998ab2ee14d0831d96c1d42be4ce484b07c2 100644 (file)
@@ -16,9 +16,11 @@ import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.Future;
 import javax.annotation.PostConstruct;
@@ -34,6 +36,7 @@ import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
 import org.opendaylight.netvirt.vpnmanager.api.VpnExtraRouteHelper;
+import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
@@ -320,12 +323,15 @@ public class TunnelInterfaceStateListener extends AsyncDataTreeChangeListenerBas
             String intfName = null;
             List<Uuid> subnetList = new ArrayList<>();
             Map<Long, String> vpnIdRdMap = new HashMap<>();
+            Set<String> listVpnName = new HashSet<String>();
 
             while (interfacelistIter.hasNext()) {
                 intfName = interfacelistIter.next();
                 VpnInterface vpnInterface =
                      VpnUtil.getConfiguredVpnInterface(dataBroker, intfName);
                 if (vpnInterface != null) {
+                    listVpnName.addAll(VpnHelper
+                        .getVpnInterfaceVpnInstanceNamesString(vpnInterface.getVpnInstanceNames()));
                     handleTunnelEventForDPNVpn(stateTunnelList, action, vpnIdRdMap,
                                          tunnelAction, isTepDeletedOnDpn,
                                          subnetList, TunnelEventProcessingMethod.POPULATESUBNETS,
@@ -353,26 +359,26 @@ public class TunnelInterfaceStateListener extends AsyncDataTreeChangeListenerBas
             for (Map.Entry<Long, String> entry : vpnIdRdMap.entrySet()) {
                 Long vpnId = entry.getKey();
                 rd = entry.getValue();
-                if ((tunnelAction == TunnelAction.TUNNEL_EP_ADD)
-                    && (tunTypeVal == VpnConstants.ITMTunnelLocType.External.getValue())) {
+                if (tunnelAction == TunnelAction.TUNNEL_EP_ADD
+                    && tunTypeVal == VpnConstants.ITMTunnelLocType.External.getValue()) {
                     fibManager.populateExternalRoutesOnDpn(srcDpnId, vpnId, rd, srcTepIp, destTepIp);
-                } else if ((tunnelAction == TunnelAction.TUNNEL_EP_DELETE)
-                    && (tunTypeVal == VpnConstants.ITMTunnelLocType.External.getValue())) {
+                } else if (tunnelAction == TunnelAction.TUNNEL_EP_DELETE
+                    && tunTypeVal == VpnConstants.ITMTunnelLocType.External.getValue()) {
                     fibManager.cleanUpExternalRoutesOnDpn(srcDpnId, vpnId, rd, srcTepIp, destTepIp);
                 }
             }
-
-            if (tunnelAction == TunnelAction.TUNNEL_EP_ADD) {
-                for (Uuid subnetId : subnetList) {
-                    // Populate the List of subnets
-                    vpnSubnetRouteHandler.updateSubnetRouteOnTunnelUpEvent(subnetId, srcDpnId);
+            if (listVpnName.size() >= 1) {
+                if (tunnelAction == TunnelAction.TUNNEL_EP_ADD) {
+                    for (Uuid subnetId : subnetList) {
+                        // Populate the List of subnets
+                        vpnSubnetRouteHandler.updateSubnetRouteOnTunnelUpEvent(subnetId, srcDpnId);
+                    }
                 }
-            }
-
-            if ((tunnelAction == TunnelAction.TUNNEL_EP_DELETE) && isTepDeletedOnDpn) {
-                for (Uuid subnetId : subnetList) {
-                    // Populate the List of subnets
-                    vpnSubnetRouteHandler.updateSubnetRouteOnTunnelDownEvent(subnetId, srcDpnId);
+                if ((tunnelAction == TunnelAction.TUNNEL_EP_DELETE) && isTepDeletedOnDpn) {
+                    for (Uuid subnetId : subnetList) {
+                        // Populate the List of subnets
+                        vpnSubnetRouteHandler.updateSubnetRouteOnTunnelDownEvent(subnetId, srcDpnId);
+                    }
                 }
             }
         } catch (RuntimeException e) {
@@ -407,7 +413,7 @@ public class TunnelInterfaceStateListener extends AsyncDataTreeChangeListenerBas
             if (method == TunnelEventProcessingMethod.POPULATESUBNETS) {
                 Optional<VpnInterfaceOpDataEntry> opVpnInterface = VpnUtil
                            .getVpnInterfaceOpDataEntry(dataBroker, intfName, vpnName);
-                if (opVpnInterface.isPresent()) {
+                if (opVpnInterface.isPresent() && !opVpnInterface.get().isScheduledForRemove()) {
                     VpnInterfaceOpDataEntry vpnInterface  = opVpnInterface.get();
                     jobCoordinator.enqueueJob("VPNINTERFACE-" + intfName,
                             new UpdateVpnInterfaceOnTunnelEvent(tunnelAction,
@@ -467,31 +473,6 @@ public class TunnelInterfaceStateListener extends AsyncDataTreeChangeListenerBas
                 }
             }
         }
-        //Iterate over the VpnId-to-Rd map.
-        for (Map.Entry<Long, String> entry : vpnIdRdMap.entrySet()) {
-            Long vpnId = entry.getKey();
-            rd = entry.getValue();
-            if (tunnelAction == TunnelAction.TUNNEL_EP_ADD
-                && tunTypeVal == VpnConstants.ITMTunnelLocType.External.getValue()) {
-                fibManager.populateExternalRoutesOnDpn(srcDpnId, vpnId, rd, srcTepIp, destTepIp);
-            } else if (tunnelAction == TunnelAction.TUNNEL_EP_DELETE
-                && tunTypeVal == VpnConstants.ITMTunnelLocType.External.getValue()) {
-                fibManager.cleanUpExternalRoutesOnDpn(srcDpnId, vpnId, rd, srcTepIp, destTepIp);
-            }
-        }
-
-        if (tunnelAction == TunnelAction.TUNNEL_EP_ADD) {
-            for (Uuid subnetId : subnetList) {
-                // Populate the List of subnets
-                vpnSubnetRouteHandler.updateSubnetRouteOnTunnelUpEvent(subnetId, srcDpnId);
-            }
-        }
-        if (tunnelAction == TunnelAction.TUNNEL_EP_DELETE && isTepDeletedOnDpn) {
-            for (Uuid subnetId : subnetList) {
-                // Populate the List of subnets
-                vpnSubnetRouteHandler.updateSubnetRouteOnTunnelDownEvent(subnetId, srcDpnId);
-            }
-        }
     }
 
     private class UpdateVpnInterfaceOnTunnelEvent implements Callable {