Bulk merge of l2gw changes
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / L2GatewayConnectionUtils.java
index a8ce5b713c6ffcf490b4650d6674355634d495e5..4182d038596bec078baf0b56b993ed8b6d84747a 100644 (file)
@@ -8,36 +8,39 @@
 
 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.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.utils.hwvtep.HwvtepNodeHACache;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+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.LocalUcastMacListener;
 import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
+import org.opendaylight.netvirt.elan.utils.Scheduler;
 import org.opendaylight.netvirt.elanmanager.utils.ElanL2GwCacheUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
@@ -67,31 +70,33 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
     private final ElanL2GatewayUtils elanL2GatewayUtils;
     private final ElanClusterUtils elanClusterUtils;
     private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
+    private final ElanL2GatewayBcGroupUtils elanL2GatewayBcGroupUtils;
+    private final Scheduler scheduler;
     private final JobCoordinator jobCoordinator;
     private final L2GatewayCache l2GatewayCache;
     private final ElanInstanceCache elanInstanceCache;
     private final List<AutoCloseable> closeables = new CopyOnWriteArrayList<>();
-    private final HwvtepNodeHACache hwvtepNodeHACache;
-    private final HAOpClusteredListener haOpClusteredListener;
-    private final ElanRefUtil elanRefUtil;
+    private final LocalUcastMacListener localUcastMacListener;
 
     @Inject
-    public L2GatewayConnectionUtils(DataBroker dataBroker, ElanClusterUtils elanClusterUtils,
-                                    ElanL2GatewayUtils elanL2GatewayUtils, JobCoordinator jobCoordinator,
+    public L2GatewayConnectionUtils(DataBroker dataBroker,
+                                    ElanClusterUtils elanClusterUtils, ElanL2GatewayUtils elanL2GatewayUtils,
+                                    JobCoordinator jobCoordinator,
                                     ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
-                                    L2GatewayCache l2GatewayCache, HAOpClusteredListener haOpClusteredListener,
-                                    ElanInstanceCache elanInstanceCache, HwvtepNodeHACache hwvtepNodeHACache,
-                                    ElanRefUtil elanRefUtil) {
+                                    ElanL2GatewayBcGroupUtils elanL2GatewayBcGroupUtils, Scheduler scheduler,
+                                    L2GatewayCache l2GatewayCache,
+                                    ElanInstanceCache elanInstanceCache,
+                                    LocalUcastMacListener localUcastMacListener) {
         this.broker = dataBroker;
         this.elanL2GatewayUtils = elanL2GatewayUtils;
         this.elanClusterUtils = elanClusterUtils;
         this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
+        this.elanL2GatewayBcGroupUtils = elanL2GatewayBcGroupUtils;
+        this.scheduler = scheduler;
         this.jobCoordinator = jobCoordinator;
         this.l2GatewayCache = l2GatewayCache;
-        this.haOpClusteredListener = haOpClusteredListener;
         this.elanInstanceCache = elanInstanceCache;
-        this.hwvtepNodeHACache = hwvtepNodeHACache;
-        this.elanRefUtil = elanRefUtil;
+        this.localUcastMacListener = localUcastMacListener;
     }
 
     @Override
@@ -108,30 +113,53 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
     }
 
     public static boolean isGatewayAssociatedToL2Device(L2GatewayDevice l2GwDevice) {
-        return !l2GwDevice.getL2GatewayIds().isEmpty();
+        return !l2GwDevice.getHwvtepNodeId().isEmpty();
     }
 
     @Nullable
     public static L2gateway getNeutronL2gateway(DataBroker broker, Uuid l2GatewayId) {
-        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();
+        if (l2GatewayId != null) {
+            LOG.debug("getNeutronL2gateway for {}", l2GatewayId.getValue());
+            InstanceIdentifier<L2gateway> inst = InstanceIdentifier.create(Neutron.class).child(L2gateways.class)
+                    .child(L2gateway.class, new L2gatewayKey(l2GatewayId));
+            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;
+        }
+
+        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::nonnullL2gateway)
+                    .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::nonnullL2gatewayConnection)
+                    .orElse(emptyMap())).values());
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getNeutronL2gateway: Exception while reading L2gateway DS", e);
+        }
+        return null;
     }
 
     /**
@@ -149,9 +177,15 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
         for (Uuid l2GatewayId : l2GatewayIds) {
             for (L2gatewayConnection l2GwConn : allL2GwConns) {
-                if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
-                    l2GwConnections.add(l2GwConn);
+                if (l2GwConn.getL2gatewayId() != null) {
+                    if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
+                        l2GwConnections.add(l2GwConn);
+                    }
                 }
+                else {
+                    LOG.warn("No l2gatewayId for l2gatewayconnection {} ", l2GwConn.key().getUuid());
+                }
+
             }
         }
         return l2GwConnections;
@@ -190,7 +224,8 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
     public void addL2GatewayConnection(final L2gatewayConnection input,
                                        @Nullable final String l2GwDeviceName ,
                                        @Nullable L2gateway l2Gateway) {
-        LOG.info("Adding L2gateway Connection with ID: {}", input.key().getUuid());
+        LOG.info("Adding L2gateway Connection:{} vlan: {} device name {}",
+            input.key().getUuid(), input.getSegmentId(), l2GwDeviceName);
 
         Uuid networkUuid = input.getNetworkId();
 
@@ -198,7 +233,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;
         }
@@ -211,7 +246,7 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
                 l2Gateway = getNeutronL2gateway(broker, l2GatewayId);
             }
             if (l2Gateway == null) {
-                LOG.error("L2Gateway with id {} is not present", l2GatewayId.getValue());
+                LOG.error("L2Gateway with id {} is not present", l2GatewayId);
             } else {
                 associateHwvtepsToElan(elanInstance, l2Gateway, input, l2GwDeviceName);
             }
@@ -219,7 +254,8 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
     }
 
     public void deleteL2GatewayConnection(L2gatewayConnection input) {
-        LOG.info("Deleting L2gateway Connection with ID: {}", input.key().getUuid());
+        LOG.info("Deleting L2gateway Connection with ID: {} vlan : {}",
+            input.key().getUuid(), input.getSegmentId());
 
         Uuid networkUuid = input.getNetworkId();
         String elanName = networkUuid.getValue();
@@ -242,14 +278,18 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
             if (l2Gateway == null) {
                 LOG.error("Failed to find the l2gateway for the connection {}", input.getUuid());
                 return;
-            } else if (l2Gateway.getDevices() != null) {
-                l2gwDevicesToBeDeleted.addAll(l2Gateway.getDevices());
+            } else if (l2Gateway.nonnullDevices() != null) {
+                l2gwDevicesToBeDeleted.addAll(l2Gateway.nonnullDevices().values());
             }
         }
         for (Devices l2Device : l2gwDevicesToBeDeleted) {
             String l2DeviceName = l2Device.getDeviceName();
             L2GatewayDevice l2GatewayDevice = l2GatewayCache.get(l2DeviceName);
             String hwvtepNodeId = l2GatewayDevice.getHwvtepNodeId();
+            if (hwvtepNodeId == null) {
+                LOG.error("Could not disassociate failed to get node id {}", l2DeviceName);
+                continue;
+            }
             boolean isLastL2GwConnDeleted = false;
             L2GatewayDevice elanL2GwDevice = ElanL2GwCacheUtils.getL2GatewayDeviceFromCache(elanName, hwvtepNodeId);
             if (elanL2GwDevice != null && isLastL2GwConnBeingDeleted(elanL2GwDevice)) {
@@ -270,27 +310,23 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
 
             DisAssociateHwvtepFromElanJob disAssociateHwvtepToElanJob =
                     new DisAssociateHwvtepFromElanJob(elanL2GatewayUtils, elanL2GatewayMulticastUtils,
-                            elanL2GwDevice, elanName,
-                        l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
+                            elanL2GatewayBcGroupUtils, elanClusterUtils, scheduler, jobCoordinator,
+                            elanL2GwDevice, elanName, l2Device, defaultVlan, hwvtepNodeId, isLastL2GwConnDeleted);
             elanClusterUtils.runOnlyInOwnerNode(disAssociateHwvtepToElanJob.getJobKey(), "remove l2gw connection job",
                     disAssociateHwvtepToElanJob);
         }
     }
 
     private void associateHwvtepsToElan(ElanInstance elanInstance,
-            L2gateway l2Gateway, L2gatewayConnection input, @Nullable String l2GwDeviceName) {
+            L2gateway l2Gateway, L2gatewayConnection input, String l2GwDeviceName) {
         String elanName = elanInstance.getElanInstanceName();
         Integer defaultVlan = input.getSegmentId();
         Uuid l2GwConnId = input.key().getUuid();
-        List<Devices> l2Devices = l2Gateway.getDevices();
+        List<Devices> l2Devices =  new ArrayList<>(l2Gateway.nonnullDevices().values());
 
         LOG.trace("Associating ELAN {} with L2Gw Conn Id {} having below L2Gw devices {}", elanName, l2GwConnId,
                 l2Devices);
 
-        if (l2Devices == null) {
-            return;
-        }
-
         for (Devices l2Device : l2Devices) {
             String l2DeviceName = l2Device.getDeviceName();
             // L2gateway can have more than one L2 Gw devices. Configure Logical Switch, VLAN mappings,...
@@ -310,18 +346,17 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
                         ElanL2GatewayUtils.getLogicalSwitchFromElan(elanName));
 
                 // Add L2 Gateway device to 'ElanL2GwDevice' cache
-                boolean createLogicalSwitch;
                 addL2DeviceToElanL2GwCache(elanName, l2GatewayDevice, l2GwConnId, l2Device);
-
                 AssociateHwvtepToElanJob associateHwvtepToElanJob = new AssociateHwvtepToElanJob(broker,
-                        elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanInstanceCache, l2GatewayDevice,
-                        elanInstance, l2Device, defaultVlan, elanRefUtil);
+                        elanL2GatewayUtils, elanL2GatewayMulticastUtils, elanL2GatewayBcGroupUtils,
+                        l2GatewayDevice, elanInstance, l2Device, defaultVlan);
 
                 elanClusterUtils.runOnlyInOwnerNode(associateHwvtepToElanJob.getJobKey(),
                         "create logical switch in hwvtep topo", associateHwvtepToElanJob);
 
             } else {
-                LOG.info("L2GwConn create is not handled for device with id {} as it's not connected", l2DeviceName);
+                LOG.error("L2GwConn create is not handled for device with id {} as it's not connected {}",
+                        l2DeviceName, input);
             }
         }
     }
@@ -347,7 +382,7 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         //while odl is down, pull them now
         readAndCopyLocalUcastMacsToCache(elanName, l2GatewayDevice);
 
-        LOG.trace("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
+        LOG.info("Elan L2GwConn cache updated with below details: {}", elanL2GwDevice);
         return elanL2GwDevice;
     }
 
@@ -364,35 +399,37 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
                 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) {
-                            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);
-                                            });
+            FluentFuture<Optional<Node>> fluentFuture = broker.newReadOnlyTransaction().read(
+                LogicalDatastoreType.OPERATIONAL, nodeIid);
+            Futures.addCallback(fluentFuture, new FutureCallback<Optional<Node>>() {
+                    @Override
+                    public void onSuccess(@NonNull Optional<Node> resultNode) {
+                        Optional<Node> nodeOptional = resultNode;
+                        if (nodeOptional.isPresent()) {
+                            Node node = nodeOptional.get();
+                            if (node.augmentation(HwvtepGlobalAugmentation.class) != null) {
+                                List<LocalUcastMacs> localUcastMacs = new ArrayList<>(
+                                        node.augmentation(HwvtepGlobalAugmentation.class)
+                                            .nonnullLocalUcastMacs().values());
+                                if (localUcastMacs == null) {
+                                    return;
                                 }
+                                localUcastMacs.stream()
+                                        .filter(mac -> macBelongsToLogicalSwitch(mac, elanName))
+                                        .forEach(mac -> {
+                                            InstanceIdentifier<LocalUcastMacs> macIid = getMacIid(nodeIid, mac);
+                                            localUcastMacListener.added(macIid, mac);
+                                        });
                             }
                         }
-                    }, MoreExecutors.directExecutor());
-            return Lists.newArrayList(settableFuture);
-        } , 5);
+                    }
+
+                    @Override
+                    public void onFailure(Throwable throwable) {
+                    }
+                }, MoreExecutors.directExecutor());
+            return Lists.newArrayList(fluentFuture);
+        });
     }
 
     /**
@@ -406,13 +443,15 @@ public class L2GatewayConnectionUtils implements AutoCloseable {
         List<L2gatewayConnection> l2GwConnections = new ArrayList<>();
         List<L2gatewayConnection> allL2GwConns = getAllL2gatewayConnections(broker);
         for (L2gatewayConnection l2GwConn : allL2GwConns) {
-            if (Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
+            if (l2GwConn.getL2gatewayId() != null &&  Objects.equals(l2GwConn.getL2gatewayId(), l2GatewayId)) {
                 l2GwConnections.add(l2GwConn);
             }
         }
         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();