Bump version odlparent->6.0.0,mdsal->5.0.3
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / recovery / impl / ItmTepInstanceRecoveryHandler.java
index a4aedf8c2c4b5d4453790d1c933524153ed16f95..8a9cdf236c3dc1ce021c419389644f68ae8104f9 100644 (file)
@@ -9,16 +9,21 @@ package org.opendaylight.genius.itm.recovery.impl;
 
 import java.time.Duration;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.atomic.AtomicInteger;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.itm.cache.DPNTEPsInfoCache;
 import org.opendaylight.genius.itm.cache.DpnTepStateCache;
+import org.opendaylight.genius.itm.cache.OfEndPointCache;
 import org.opendaylight.genius.itm.cache.OvsBridgeEntryCache;
 import org.opendaylight.genius.itm.cache.OvsBridgeRefEntryCache;
 import org.opendaylight.genius.itm.cache.TunnelStateCache;
@@ -32,22 +37,23 @@ import org.opendaylight.genius.itm.impl.ItmUtils;
 import org.opendaylight.genius.itm.impl.TunnelMonitoringConfig;
 import org.opendaylight.genius.itm.itmdirecttunnels.renderer.ovs.utilities.DirectTunnelUtils;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
-import org.opendaylight.genius.srm.ServiceRecoveryInterface;
-import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
 import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryInterface;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.config.rev160406.ItmConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.tunnel.end.points.TzMembership;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.DpnsTeps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.teps.state.dpns.teps.RemoteDpns;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnel.list.InternalTunnel;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelList;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tunnels_state.StateTunnelListKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.types.rev170711.GeniusItmTep;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Vteps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.serviceutils.srm.types.rev180626.GeniusItmTep;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -67,6 +73,9 @@ public class ItmTepInstanceRecoveryHandler implements ServiceRecoveryInterface {
     private final EntityOwnershipUtils entityOwnershipUtils;
     private final IMdsalApiManager imdsalApiManager;
     private final DataTreeEventCallbackRegistrar eventCallbacks;
+    private final ManagedNewTransactionRunner txRunner;
+    private final IInterfaceManager interfaceManager;
+    private final DpnTepStateCache dpnTepStateCache;
 
     @Inject
     public ItmTepInstanceRecoveryHandler(DataBroker dataBroker,
@@ -81,6 +90,7 @@ public class ItmTepInstanceRecoveryHandler implements ServiceRecoveryInterface {
                                          IInterfaceManager interfaceManager,
                                          ServiceRecoveryRegistry serviceRecoveryRegistry,
                                          EntityOwnershipUtils entityOwnershipUtils,
+                                         OfEndPointCache ofEndPointCache,
                                          DataTreeEventCallbackRegistrar eventCallbacks) {
         this.dataBroker = dataBroker;
         this.itmConfig = itmConfig;
@@ -89,15 +99,18 @@ public class ItmTepInstanceRecoveryHandler implements ServiceRecoveryInterface {
         this.dpntePsInfoCache = dpntePsInfoCache;
         this.entityOwnershipUtils = entityOwnershipUtils;
         this.eventCallbacks = eventCallbacks;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.itmInternalTunnelAddWorker = new ItmInternalTunnelAddWorker(dataBroker, jobCoordinator,
-                tunnelMonitoringConfig, itmConfig, directTunnelUtils, interfaceManager, ovsBridgeRefEntryCache);
-        this.itmExternalTunnelAddWorker = new ItmExternalTunnelAddWorker(dataBroker, itmConfig,
+                tunnelMonitoringConfig, itmConfig, directTunnelUtils, interfaceManager,
+                ovsBridgeRefEntryCache, ofEndPointCache, eventCallbacks);
+        this.itmExternalTunnelAddWorker = new ItmExternalTunnelAddWorker(itmConfig,
                 dpntePsInfoCache);
         this.itmInternalTunnelDeleteWorker = new ItmInternalTunnelDeleteWorker(dataBroker, jobCoordinator,
                 tunnelMonitoringConfig, interfaceManager, dpnTepStateCache, ovsBridgeEntryCache,
-                ovsBridgeRefEntryCache, tunnelStateCache,
-                directTunnelUtils);
+                ovsBridgeRefEntryCache, tunnelStateCache, directTunnelUtils, ofEndPointCache, itmConfig);
         serviceRecoveryRegistry.registerServiceRecoveryRegistry(getServiceRegistryKey(), this);
+        this.interfaceManager = interfaceManager;
+        this.dpnTepStateCache = dpnTepStateCache;
     }
 
     private String getServiceRegistryKey() {
@@ -142,15 +155,31 @@ public class ItmTepInstanceRecoveryHandler implements ServiceRecoveryInterface {
             if (oldTz != null) {
                 LOG.trace("Deleting transportzone {}", tzName);
                 ItmTepRemoveWorker tepRemoveWorker = new ItmTepRemoveWorker(tepsToRecover, null, oldTz,
-                        dataBroker, imdsalApiManager, itmInternalTunnelDeleteWorker, dpntePsInfoCache);
+                        imdsalApiManager, itmInternalTunnelDeleteWorker, dpntePsInfoCache, txRunner, itmConfig);
                 jobCoordinator.enqueueJob(tzName, tepRemoveWorker);
                 AtomicInteger eventCallbackCount = new AtomicInteger(0);
                 AtomicInteger eventRegistrationCount = new AtomicInteger(0);
-                tunnelList.stream().filter(internalTunnel -> internalTunnel
-                        .getDestinationDPN().equals(dpnTepsToRecover.getDPNID()) || internalTunnel.getSourceDPN()
-                        .equals(dpnTepsToRecover.getDPNID())).forEach(internalTunnel -> {
-                            eventRegistrationCount.incrementAndGet();
-                            interfaceListToRecover.add(String.valueOf(internalTunnel.getTunnelInterfaceNames())); });
+                if (interfaceManager.isItmDirectTunnelsEnabled()) {
+                    Collection<DpnsTeps> dpnsTeps = dpnTepStateCache.getAllPresent();
+                    for (DpnsTeps dpnTep : dpnsTeps) {
+                        List<RemoteDpns> rmtdpns = dpnTep.getRemoteDpns();
+                        for (RemoteDpns remoteDpn : rmtdpns) {
+                            if (remoteDpn.getDestinationDpnId().equals(dpnTepsToRecover.getDPNID())
+                                    || dpnTep.getSourceDpnId().equals(dpnTepsToRecover.getDPNID())) {
+                                eventRegistrationCount.incrementAndGet();
+                                interfaceListToRecover.add(remoteDpn.getTunnelName());
+                            }
+                        }
+                    }
+                    LOG.trace("List of tunnels to be recovered : {}", interfaceListToRecover);
+                } else {
+                    tunnelList.stream().filter(internalTunnel -> Objects.equals(internalTunnel
+                            .getDestinationDPN(), dpnTepsToRecover.getDPNID()) || Objects.equals(
+                            internalTunnel.getSourceDPN(), dpnTepsToRecover.getDPNID())).forEach(internalTunnel -> {
+                                eventRegistrationCount.incrementAndGet();
+                                interfaceListToRecover.add(String.valueOf(internalTunnel.getTunnelInterfaceNames()));
+                            });
+                }
 
                 if (!interfaceListToRecover.isEmpty()) {
                     interfaceListToRecover.forEach(interfaceName -> {
@@ -174,18 +203,13 @@ public class ItmTepInstanceRecoveryHandler implements ServiceRecoveryInterface {
         }
     }
 
-
     private void recreateTEP(String tzName, List tepts, AtomicInteger eventCallbackCount, int registeredEventSize) {
         eventCallbackCount.incrementAndGet();
         if (eventCallbackCount.intValue() == registeredEventSize || registeredEventSize == 0) {
             LOG.info("Re-creating TEP {}", tzName);
-            ItmTepAddWorker tepAddWorker = new ItmTepAddWorker(tepts, null, dataBroker,
-                    imdsalApiManager, itmConfig, itmInternalTunnelAddWorker, itmExternalTunnelAddWorker,
-                    dpntePsInfoCache);
+            ItmTepAddWorker tepAddWorker = new ItmTepAddWorker(tepts, null, dataBroker, imdsalApiManager,
+                    itmInternalTunnelAddWorker, itmExternalTunnelAddWorker);
             jobCoordinator.enqueueJob(tzName, tepAddWorker);
-        } else {
-            LOG.trace("{} call back events registered for {} tunnel interfaces",
-                    registeredEventSize, eventCallbackCount);
         }
     }
 
@@ -196,29 +220,27 @@ public class ItmTepInstanceRecoveryHandler implements ServiceRecoveryInterface {
             return null;
         }
 
-        for (Subnets sub : transportZone.getSubnets()) {
-            if (sub.getVteps() == null || sub.getVteps().isEmpty()) {
-                LOG.error("Transport Zone {} subnet {} has no vteps", transportZone.getZoneName(), sub.getPrefix());
-            }
-            for (Vteps vtep : sub.getVteps()) {
-                if (ipAddress.equals(String.valueOf(vtep.getIpAddress().getValue()))) {
-
-                    List<TzMembership> zones = ItmUtils.createTransportZoneMembership(tzName);
-                    LOG.trace("Transportzone {} found match for tep {} to be recovered", transportZone.getZoneName(),
-                            ipAddress);
-
-                    //OfTunnels is false byDefault
-                    TunnelEndPoints tunnelEndPoints = ItmUtils.createTunnelEndPoints(vtep.getDpnId(),
-                        IpAddressBuilder.getDefaultInstance(ipAddress), vtep.getPortname(), false, sub.getVlanId(),
-                            sub.getPrefix(), sub.getGatewayIp(), zones,transportZone.getTunnelType(),
-                            itmConfig.getDefaultTunnelTos());
-
-                    List<TunnelEndPoints> teps = new ArrayList<>();
-                    teps.add(tunnelEndPoints);
-                    return ItmUtils.createDPNTepInfo(vtep.getDpnId(), teps);
-                }
+        String portName = itmConfig.getPortname() == null ? ITMConstants.DUMMY_PORT : itmConfig.getPortname();
+        int vlanId = itmConfig.getVlanId() != null ? itmConfig.getVlanId().toJava() : ITMConstants.DUMMY_VLANID;
+
+        for (Vteps vtep : transportZone.getVteps()) {
+            if (ipAddress.equals(vtep.getIpAddress().stringValue())) {
+
+                List<TzMembership> zones = ItmUtils.createTransportZoneMembership(tzName);
+                LOG.trace("Transportzone {} found match for tep {} to be recovered", transportZone.getZoneName(),
+                        ipAddress);
+
+                //OfTunnels is false byDefault
+                TunnelEndPoints tunnelEndPoints = ItmUtils.createTunnelEndPoints(vtep.getDpnId(),
+                        IpAddressBuilder.getDefaultInstance(ipAddress), portName, false,
+                        vlanId, zones, transportZone.getTunnelType(),
+                        itmConfig.getDefaultTunnelTos());
+
+                List<TunnelEndPoints> teps = new ArrayList<>();
+                teps.add(tunnelEndPoints);
+                return ItmUtils.createDPNTepInfo(vtep.getDpnId(), teps);
             }
         }
         return null;
     }
-}
\ No newline at end of file
+}