MRI version bumpup for Aluminium
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / L2GatewayConnectionUtils.java
index a96358f6b711ec3e2edb93f1181a8901984cba3c..663b78a0bfabca52f00ee6f6a7bf8493a6667f15 100644 (file)
@@ -8,44 +8,51 @@
 
 package org.opendaylight.netvirt.elan.l2gw.utils;
 
-import static java.util.Collections.emptyList;
+import static java.util.Collections.emptyMap;
 import static org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.FluentFuture;
+import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.MoreExecutors;
-import com.google.common.util.concurrent.SettableFuture;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ExecutionException;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
-import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.RetryingManagedNewTransactionRunner;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
 import org.opendaylight.netvirt.elan.l2gw.ha.listeners.HAOpClusteredListener;
 import org.opendaylight.netvirt.elan.l2gw.jobs.AssociateHwvtepToElanJob;
 import org.opendaylight.netvirt.elan.l2gw.jobs.DisAssociateHwvtepFromElanJob;
-import org.opendaylight.netvirt.elan.l2gw.listeners.HwvtepLogicalSwitchListener;
 import org.opendaylight.netvirt.elan.l2gw.listeners.LocalUcastMacListener;
+import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler;
 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
 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.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.DevicesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.L2gatewayConnections;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.connections.attributes.l2gatewayconnections.L2gatewayConnection;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateways.attributes.L2gateways;
@@ -54,6 +61,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev15071
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacs;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LocalUcastMacsKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
@@ -75,13 +83,20 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
     private final List<AutoCloseable> closeables = new CopyOnWriteArrayList<>();
     private final HwvtepNodeHACache hwvtepNodeHACache;
     private final HAOpClusteredListener haOpClusteredListener;
+    private final ElanRefUtil elanRefUtil;
+    private final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler;
+    private final ServiceRecoveryRegistry serviceRecoveryRegistry;
+    private final ManagedNewTransactionRunner txRunner;
 
     @Inject
-    public L2GatewayConnectionUtils(DataBroker dataBroker,
-            ElanClusterUtils elanClusterUtils, ElanL2GatewayUtils elanL2GatewayUtils,
-            JobCoordinator jobCoordinator, ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
-            L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
-            ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache) {
+    public L2GatewayConnectionUtils(DataBroker dataBroker, ElanClusterUtils elanClusterUtils,
+                                    ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator,
+                                    ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
+                                    L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
+                                    ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache,
+                                    ElanRefUtil elanRefUtil,
+                                    L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler,
+                                    ServiceRecoveryRegistry serviceRecoveryRegistry) {
         this.broker = dataBroker;
         this.elanL2GatewayUtils = elanL2GatewayUtils;
         this.elanClusterUtils = elanClusterUtils;
@@ -91,6 +106,10 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         this.haOpClusteredListener = haOpClusteredListener;
         this.elanInstanceCache = elanInstanceCache;
         this.hwvtepNodeHACache = hwvtepNodeHACache;
+        this.elanRefUtil = elanRefUtil;
+        this.l2GatewayServiceRecoveryHandler = l2GatewayServiceRecoveryHandler;
+        this.serviceRecoveryRegistry = serviceRecoveryRegistry;
+        this.txRunner = new RetryingManagedNewTransactionRunner(dataBroker);
     }
 
     @Override
@@ -115,22 +134,40 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
         InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
                 .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
-        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).orNull();
+        try {
+            return SingleTransactionDataBroker.syncReadOptional(broker, LogicalDatastoreType.CONFIGURATION, inst)
+                    .orElse(null);
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS for the ID {}", l2GatewayId, e);
+        }
+        return null;
     }
 
     @NonNull
     public static List<L2gateway> getL2gatewayList(DataBroker broker) {
         InstanceIdentifier<L2gateways> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class);
-        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
-                L2gateways::getL2gateway).orElse(emptyList());
+        try {
+            return new ArrayList<>((SingleTransactionDataBroker.syncReadOptional(broker,
+                    LogicalDatastoreType.CONFIGURATION, inst).map(L2gateways::getL2gateway)
+                    .orElse(emptyMap())).values());
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS", e);
+        }
+        return null;
     }
 
     @NonNull
     public static List<L2gatewayConnection> getAllL2gatewayConnections(DataBroker broker) {
         InstanceIdentifier<L2gatewayConnections> inst = InstanceIdentifier.create(Neutron.class)
                 .child(L2gatewayConnections.class);
-        return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, inst).toJavaUtil().map(
-                L2gatewayConnections::getL2gatewayConnection).orElse(emptyList());
+        try {
+            return new ArrayList<>((SingleTransactionDataBroker.syncReadOptional(broker,
+                    LogicalDatastoreType.CONFIGURATION, inst).map(L2gatewayConnections::getL2gatewayConnection)
+                    .orElse(emptyMap())).values());
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS", e);
+        }
+        return null;
     }
 
     /**
@@ -197,7 +234,7 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         // comes we need to wait for elaninstance to resolve. Hence updating the map with the runnable .
         // When elanInstance add comes , it look in to the map and run the associated runnable associated with it.
         ElanInstance elanInstance = elanInstanceCache.get(networkUuid.getValue(),
-            () -> addL2GatewayConnection(input, l2GwDeviceName)).orNull();
+            () -> addL2GatewayConnection(input, l2GwDeviceName)).orElse(null);
         if (elanInstance == null) {
             return;
         }
@@ -242,7 +279,7 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
                 LOG.error("Failed to find the l2gateway for the connection {}", input.getUuid());
                 return;
             } else if (l2Gateway.getDevices() != null) {
-                l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices());
+                l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices().values());
             }
         }
         for (Devices l2Device : l2gwDevicesToBeDeleted) {
@@ -281,7 +318,7 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         String elanName = elanInstance.getElanInstanceName();
         Integer defaultVlan = input.getSegmentId();
         Uuid l2GwConnId = input.key().getUuid();
-        List<Devices> l2Devices = l2Gateway.getDevices();
+        Map<DevicesKey, Devices> l2Devices = l2Gateway.getDevices();
 
         LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
                 l2Devices);
@@ -290,7 +327,7 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
             return;
         }
 
-        for (Devices l2Device : l2Devices) {
+        for (Devices l2Device : l2Devices.values()) {
             String l2DeviceName = l2Device.getDeviceName();
             // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
             // only on the switch which has come up just now and exclude all other devices from
@@ -310,22 +347,11 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
 
                 // Add L2 Gateway device to 'ElanL2GwDevice' cache
                 boolean createLogicalSwitch;
-                LogicalSwitches logicalSwitch = HwvtepUtils.getLogicalSwitch(broker, LogicalDatastoreType.CONFIGURATION,
-                        hwvtepNodeId, elanName);
-                if (logicalSwitch == null) {
-                    HwvtepLogicalSwitchListener hwVTEPLogicalSwitchListener = new HwvtepLogicalSwitchListener(
-                            elanInstanceCache, elanL2GatewayUtils, elanClusterUtils, elanL2GatewayMulticastUtils,
-                            this, l2GatewayDevice, elanName, l2Device, defaultVlan, l2GwConnId, hwvtepNodeHACache);
-                    hwVTEPLogicalSwitchListener.registerListener(LogicalDatastoreType.OPERATIONAL, broker);
-                    closeables.add(hwVTEPLogicalSwitchListener);
-                    createLogicalSwitch = true;
-                } else {
-                    addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
-                    createLogicalSwitch = false;
-                }
+                addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
+
                 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
                         elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanInstanceCache, l2GatewayDevice,
-                        elanInstance, l2Device, defaultVlan, createLogicalSwitch);
+                        elanInstance, l2Device, defaultVlan, elanRefUtil);
 
                 elanClusterUtils.runOnlyInOwnerNode(associateHwvtepToElanJob.getJobKey(),
                         "create logical switch in hwvtep topo", associateHwvtepToElanJob);
@@ -373,35 +399,38 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         final InstanceIdentifier<Node> nodeIid = HwvtepSouthboundUtils.createInstanceIdentifier(
                 new NodeId(l2GatewayDevice.getHwvtepNodeId()));
         jobCoordinator.enqueueJob(elanName + ":" + l2GatewayDevice.getDeviceName(), () -> {
-            final SettableFuture settableFuture = SettableFuture.create();
-            Futures.addCallback(broker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL,
-                    nodeIid), new SettableFutureCallback<Optional<Node>>(settableFuture) {
-                        @Override
-                        public void onSuccess(@NonNull Optional<Node> resultNode) {
+            FluentFuture<Optional<Node>> fluentFuture = broker.newReadOnlyTransaction().read(
+                    LogicalDatastoreType.OPERATIONAL, nodeIid);
+            Futures.addCallback(fluentFuture, new FutureCallback<Optional<Node>>() {
+                @Override
+                public void onSuccess(Optional<Node> nodeOptional) {
+                    if (nodeOptional.isPresent()) {
+                        Node node = nodeOptional.get();
+                        if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
+                            Map<LocalUcastMacsKey, LocalUcastMacs> localUcastMacs =
+                                    node.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
+                            if (localUcastMacs == null) {
+                                return;
+                            }
                             LocalUcastMacListener localUcastMacListener =
                                     new LocalUcastMacListener(broker, haOpClusteredListener,
-                                            elanL2GatewayUtils, jobCoordinator, elanInstanceCache, hwvtepNodeHACache);
-                            settableFuture.set(resultNode);
-                            Optional<Node> nodeOptional = resultNode;
-                            if (nodeOptional.isPresent()) {
-                                Node node = nodeOptional.get();
-                                if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
-                                    List<LocalUcastMacs> localUcastMacs =
-                                            node.augmentation(HwvtepGlobalAugmentation.class).getLocalUcastMacs();
-                                    if (localUcastMacs == null) {
-                                        return;
-                                    }
-                                    localUcastMacs.stream()
-                                            .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
-                                            .forEach((mac) -> {
-                                                InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
-                                                localUcastMacListener.added(macIid, mac);
-                                            });
-                                }
-                            }
+                                            elanL2GatewayUtils, jobCoordinator, elanInstanceCache, hwvtepNodeHACache,
+                                            l2GatewayServiceRecoveryHandler, serviceRecoveryRegistry);
+                            localUcastMacs.values().stream()
+                                    .filter((mac) -> macBelongsToLogicalSwitch(mac, elanName))
+                                    .forEach((mac) -> {
+                                        InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
+                                        localUcastMacListener.added(macIid, mac);
+                                    });
                         }
-                    }, MoreExecutors.directExecutor());
-            return Lists.newArrayList(settableFuture);
+                    }
+                }
+
+                @Override
+                public void onFailure(Throwable throwable) {
+                }
+            }, MoreExecutors.directExecutor());
+            return Lists.newArrayList(fluentFuture);
         } , 5);
     }
 
@@ -423,6 +452,8 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         return l2GwConnections;
     }
 
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private static boolean macBelongsToLogicalSwitch(LocalUcastMacs mac, String logicalSwitchName) {
         InstanceIdentifier<LogicalSwitches> iid = (InstanceIdentifier<LogicalSwitches>)
                 mac.getLogicalSwitchRef().getValue();