Migrate elanmanager to use LoggingFutures
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / ElanL2GatewayMulticastUtils.java
index 62cc8e7b438eacde479d61f0e61cfe7555b0800a..6188724f9db29d2c49634560af3950de7df967b2 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netvirt.elan.l2gw.utils;
 
 import static java.util.Collections.emptyList;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 import static org.opendaylight.netvirt.elan.utils.ElanUtils.isVxlanNetworkOrVxlanSegment;
 
 import com.google.common.util.concurrent.ListenableFuture;
@@ -18,15 +19,18 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentMap;
-import javax.annotation.Nonnull;
 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.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
+import org.opendaylight.genius.infra.Datastore;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.actions.ActionGroup;
@@ -35,7 +39,7 @@ import org.opendaylight.genius.utils.batching.ResourceBatchingManager;
 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.genius.utils.hwvtep.HwvtepUtils;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
-import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
+import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
 import org.opendaylight.netvirt.elan.l2gw.jobs.HwvtepDeviceMcastMacUpdateJob;
 import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.netvirt.elan.utils.ElanItmUtils;
@@ -112,14 +116,13 @@ public class ElanL2GatewayMulticastUtils {
      * Handle mcast for elan l2 gw device add.
      * @param elanName the elan name
      * @param device the device
-     * @return the listenable future
      */
-    public ListenableFuture<Void> handleMcastForElanL2GwDeviceAdd(String elanName, L2GatewayDevice device) {
+    public void handleMcastForElanL2GwDeviceAdd(String elanName, L2GatewayDevice device) {
         InstanceIdentifier<ExternalTeps> tepPath = buildExternalTepPath(elanName, device.getTunnelIp());
-        JdkFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-            tx.put(LogicalDatastoreType.CONFIGURATION, tepPath, buildExternalTeps(device));
-        }), LOG, "Failed to write to config external tep {}", tepPath);
-        return updateMcastMacsForAllElanDevices(elanName, device, true/* updateThisDevice */);
+        LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+            tx -> tx.put(tepPath, buildExternalTeps(device))), LOG, "Failed to write to config external tep {}",
+            tepPath);
+        updateMcastMacsForAllElanDevices(elanName, device, true/* updateThisDevice */);
     }
 
     public static InstanceIdentifier<ExternalTeps> buildExternalTepPath(String elan, IpAddress tepIp) {
@@ -138,15 +141,11 @@ public class ElanL2GatewayMulticastUtils {
      *
      * @param elanName
      *            the elan to be updated
-     * @return the listenable future
      */
-    @SuppressWarnings("checkstyle:IllegalCatch")
-    public ListenableFuture<Void> updateRemoteMcastMacOnElanL2GwDevices(String elanName) {
-        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-            for (L2GatewayDevice device : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).values()) {
-                prepareRemoteMcastMacUpdateOnDevice(elanName, device);
-            }
-        });
+    public void updateRemoteMcastMacOnElanL2GwDevices(String elanName) {
+        for (L2GatewayDevice device : ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName).values()) {
+            prepareRemoteMcastMacUpdateOnDevice(elanName, device);
+        }
     }
 
     public void scheduleMcastMacUpdateJob(String elanName, L2GatewayDevice device) {
@@ -161,11 +160,9 @@ public class ElanL2GatewayMulticastUtils {
      *            the elan name
      * @param device
      *            the device
-     * @return the listenable future
      */
-    public ListenableFuture<Void> updateRemoteMcastMacOnElanL2GwDevice(String elanName, L2GatewayDevice device) {
-        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(
-            tx -> prepareRemoteMcastMacUpdateOnDevice(elanName, device));
+    public void updateRemoteMcastMacOnElanL2GwDevice(String elanName, L2GatewayDevice device) {
+        prepareRemoteMcastMacUpdateOnDevice(elanName, device);
     }
 
     public void prepareRemoteMcastMacUpdateOnDevice(String elanName,
@@ -175,7 +172,7 @@ public class ElanL2GatewayMulticastUtils {
         List<DpnInterfaces> dpns = elanUtils.getElanDPNByName(elanName);
         List<IpAddress> dpnsTepIps = getAllTepIpsOfDpns(device, dpns);
         List<IpAddress> l2GwDevicesTepIps = getAllTepIpsOfL2GwDevices(elanL2gwDevices);
-        preapareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
+        prepareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
     }
 
     /**
@@ -189,9 +186,8 @@ public class ElanL2GatewayMulticastUtils {
      *            the device
      * @param updateThisDevice
      *            the update this device
-     * @return the listenable future
      */
-    private ListenableFuture<Void> updateMcastMacsForAllElanDevices(String elanName, L2GatewayDevice device,
+    private void updateMcastMacsForAllElanDevices(String elanName, L2GatewayDevice device,
                                                                     boolean updateThisDevice) {
 
         SettableFuture<Void> ft = SettableFuture.create();
@@ -209,39 +205,40 @@ public class ElanL2GatewayMulticastUtils {
         // return ft;
         // }
 
-        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-            if (updateThisDevice) {
-                preapareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
-            }
+        if (updateThisDevice) {
+            prepareRemoteMcastMacEntry(elanName, device, dpnsTepIps, l2GwDevicesTepIps);
+        }
 
-            // TODO: Need to revisit below logic as logical switches might not be
-            // present to configure RemoteMcastMac entry
-            for (L2GatewayDevice otherDevice : devices.values()) {
-                if (!otherDevice.getDeviceName().equals(device.getDeviceName())) {
-                    preapareRemoteMcastMacEntry(elanName, otherDevice, dpnsTepIps, l2GwDevicesTepIps);
-                }
+        // TODO: Need to revisit below logic as logical switches might not be
+        // present to configure RemoteMcastMac entry
+        for (L2GatewayDevice otherDevice : devices.values()) {
+            if (!otherDevice.getDeviceName().equals(device.getDeviceName())) {
+                prepareRemoteMcastMacEntry(elanName, otherDevice, dpnsTepIps, l2GwDevicesTepIps);
             }
-        });
-
+        }
     }
 
-    public void updateRemoteBroadcastGroupForAllElanDpns(ElanInstance elanInfo) {
+    public void updateRemoteBroadcastGroupForAllElanDpns(ElanInstance elanInfo,
+            TypedWriteTransaction<Datastore.Configuration> confTx) {
         List<DpnInterfaces> dpns = elanUtils.getInvolvedDpnsInElan(elanInfo.getElanInstanceName());
         for (DpnInterfaces dpn : dpns) {
-            setupElanBroadcastGroups(elanInfo, dpn.getDpId());
+            setupElanBroadcastGroups(elanInfo, dpn.getDpId(), confTx);
         }
     }
 
-    public void setupElanBroadcastGroups(ElanInstance elanInfo, BigInteger dpnId) {
-        setupElanBroadcastGroups(elanInfo, null, dpnId);
+    public void setupElanBroadcastGroups(ElanInstance elanInfo, BigInteger dpnId,
+            TypedWriteTransaction<Datastore.Configuration> confTx) {
+        setupElanBroadcastGroups(elanInfo, null, dpnId, confTx);
     }
 
-    public void setupElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
-        setupStandardElanBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
-        setupLeavesEtreeBroadcastGroups(elanInfo, dpnInterfaces, dpnId);
+    public void setupElanBroadcastGroups(ElanInstance elanInfo, @Nullable DpnInterfaces dpnInterfaces, BigInteger dpnId,
+            TypedWriteTransaction<Datastore.Configuration> confTx) {
+        setupStandardElanBroadcastGroups(elanInfo, dpnInterfaces, dpnId, confTx);
+        setupLeavesEtreeBroadcastGroups(elanInfo, dpnInterfaces, dpnId, confTx);
     }
 
-    public void setupStandardElanBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
+    public void setupStandardElanBroadcastGroups(ElanInstance elanInfo, @Nullable DpnInterfaces dpnInterfaces,
+            BigInteger dpnId, TypedWriteTransaction<Datastore.Configuration> confTx) {
         List<Bucket> listBucket = new ArrayList<>();
         int bucketId = 0;
         int actionKey = 0;
@@ -257,11 +254,12 @@ public class ElanL2GatewayMulticastUtils {
         Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
                 MDSALUtil.buildBucketLists(listBucket));
         LOG.trace("Installing the remote BroadCast Group:{}", group);
-        mdsalManager.syncInstallGroup(dpnId, group);
+        mdsalManager.addGroup(confTx, dpnId, group);
     }
 
-    public void setupLeavesEtreeBroadcastGroups(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId) {
-        EtreeInstance etreeInstance = elanInfo.getAugmentation(EtreeInstance.class);
+    public void setupLeavesEtreeBroadcastGroups(ElanInstance elanInfo, @Nullable DpnInterfaces dpnInterfaces,
+            BigInteger dpnId, TypedWriteTransaction<Datastore.Configuration> confTx) {
+        EtreeInstance etreeInstance = elanInfo.augmentation(EtreeInstance.class);
         if (etreeInstance != null) {
             long etreeLeafTag = etreeInstance.getEtreeLeafTagVal().getValue();
             List<Bucket> listBucket = new ArrayList<>();
@@ -279,14 +277,15 @@ public class ElanL2GatewayMulticastUtils {
             Group group = MDSALUtil.buildGroup(groupId, elanInfo.getElanInstanceName(), GroupTypes.GroupAll,
                     MDSALUtil.buildBucketLists(listBucket));
             LOG.trace("Installing the remote BroadCast Group:{}", group);
-            mdsalManager.syncInstallGroup(dpnId, group);
+            mdsalManager.addGroup(confTx, dpnId, group);
         }
     }
 
+    @Nullable
     private DpnInterfaces getDpnInterfaces(ElanDpnInterfacesList elanDpns, BigInteger dpnId) {
         if (elanDpns != null) {
-            for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
-                if (dpnInterface.getDpId().equals(dpnId)) {
+            for (DpnInterfaces dpnInterface : elanDpns.nonnullDpnInterfaces()) {
+                if (Objects.equals(dpnInterface.getDpId(), dpnId)) {
                     return dpnInterface;
                 }
             }
@@ -315,16 +314,16 @@ public class ElanL2GatewayMulticastUtils {
         return listBucketInfo;
     }
 
-    @Nonnull
-    public List<Bucket> getRemoteBCGroupBuckets(ElanInstance elanInfo, DpnInterfaces dpnInterfaces, BigInteger dpnId,
-                                                int bucketId, long elanTag) {
+    @NonNull
+    public List<Bucket> getRemoteBCGroupBuckets(ElanInstance elanInfo, @Nullable DpnInterfaces dpnInterfaces,
+                                                BigInteger dpnId, int bucketId, long elanTag) {
         List<Bucket> listBucketInfo = new ArrayList<>();
         ElanDpnInterfacesList elanDpns = elanUtils.getElanDpnInterfacesList(elanInfo.getElanInstanceName());
 
         if (isVxlanNetworkOrVxlanSegment(elanInfo)) {
             listBucketInfo.addAll(getRemoteBCGroupTunnelBuckets(elanDpns, dpnId, bucketId,
                     elanUtils.isOpenstackVniSemanticsEnforced()
-                            ? elanUtils.getVxlanSegmentationId(elanInfo) : elanTag));
+                            ? ElanUtils.getVxlanSegmentationId(elanInfo) : elanTag));
         }
         listBucketInfo.addAll(getRemoteBCGroupExternalPortBuckets(elanDpns, dpnInterfaces, dpnId,
                 getNextAvailableBucketId(listBucketInfo.size())));
@@ -359,7 +358,7 @@ public class ElanL2GatewayMulticastUtils {
         try {
             operElanInstance = new SingleTransactionDataBroker(broker).syncReadOptional(
                 LogicalDatastoreType.OPERATIONAL,
-                InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, elanInfo.getKey())
+                InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, elanInfo.key())
                     .build()).orNull();
         } catch (ReadFailedException e) {
             LOG.error("Failed to read elan instance operational path {}", elanInfo, e);
@@ -382,7 +381,7 @@ public class ElanL2GatewayMulticastUtils {
                 continue;
             }
             List<Action> listActionInfo = elanItmUtils.buildTunnelItmEgressActions(interfaceName,
-                    elanUtils.getVxlanSegmentationId(elanInfo), false);
+                    ElanUtils.getVxlanSegmentationId(elanInfo), false);
             listBucketInfo.add(MDSALUtil.buildBucket(listActionInfo, MDSALUtil.GROUP_WEIGHT, bucketId,
                     MDSALUtil.WATCH_PORT, MDSALUtil.WATCH_GROUP));
             bucketId++;
@@ -399,7 +398,7 @@ public class ElanL2GatewayMulticastUtils {
             long elanTagOrVni) {
         List<Bucket> listBucketInfo = new ArrayList<>();
         if (elanDpns != null) {
-            for (DpnInterfaces dpnInterface : elanDpns.getDpnInterfaces()) {
+            for (DpnInterfaces dpnInterface : elanDpns.nonnullDpnInterfaces()) {
                 if (elanUtils.isDpnPresent(dpnInterface.getDpId()) && !Objects.equals(dpnInterface.getDpId(), dpnId)
                         && dpnInterface.getInterfaces() != null && !dpnInterface.getInterfaces().isEmpty()) {
                     try {
@@ -434,7 +433,7 @@ public class ElanL2GatewayMulticastUtils {
      *            the l2 gw devices tep ips
      * @return the write transaction
      */
-    private void preapareRemoteMcastMacEntry(String elanName,
+    private void prepareRemoteMcastMacEntry(String elanName,
                                              L2GatewayDevice device, List<IpAddress> dpnsTepIps,
                                              List<IpAddress> l2GwDevicesTepIps) {
         NodeId nodeId = new NodeId(device.getHwvtepNodeId());
@@ -450,17 +449,17 @@ public class ElanL2GatewayMulticastUtils {
                 remoteTepIps.add(dhcpDesignatedSwitchTepIp);
 
                 HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
-                        .createHwvtepPhysicalLocatorAugmentation(String.valueOf(dhcpDesignatedSwitchTepIp.getValue()));
+                        .createHwvtepPhysicalLocatorAugmentation(dhcpDesignatedSwitchTepIp);
                 InstanceIdentifier<TerminationPoint> iid =
                         HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug);
                 TerminationPoint terminationPoint = new TerminationPointBuilder()
-                                .setKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocatorAug))
+                                .withKey(HwvtepSouthboundUtils.getTerminationPointKey(phyLocatorAug))
                                 .addAugmentation(HwvtepPhysicalLocatorAugmentation.class, phyLocatorAug).build();
                 ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
                         iid, terminationPoint);
                 LOG.info("Adding PhysicalLocator for node: {} with Dhcp designated switch Tep Ip {} "
                         + "as physical locator, elan {}", device.getHwvtepNodeId(),
-                        String.valueOf(dhcpDesignatedSwitchTepIp.getValue()), elanName);
+                        dhcpDesignatedSwitchTepIp.stringValue(), elanName);
             } else {
                 LOG.warn("Dhcp designated switch Tep Ip not found for l2 gw node {} and elan {}",
                         device.getHwvtepNodeId(), elanName);
@@ -490,7 +489,7 @@ public class ElanL2GatewayMulticastUtils {
         List<LocatorSet> locators = new ArrayList<>();
         for (IpAddress tepIp : tepIps) {
             HwvtepPhysicalLocatorAugmentation phyLocatorAug = HwvtepSouthboundUtils
-                    .createHwvtepPhysicalLocatorAugmentation(String.valueOf(tepIp.getValue()));
+                    .createHwvtepPhysicalLocatorAugmentation(tepIp);
             HwvtepPhysicalLocatorRef phyLocRef = new HwvtepPhysicalLocatorRef(
                     HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, phyLocatorAug));
             locators.add(new LocatorSetBuilder().setLocatorRef(phyLocRef).build());
@@ -502,7 +501,7 @@ public class ElanL2GatewayMulticastUtils {
                 .setMacEntryKey(new MacAddress(ElanConstants.UNKNOWN_DMAC)).setLogicalSwitchRef(lsRef)
                 .setLocatorSet(locators).build();
         InstanceIdentifier<RemoteMcastMacs> iid = HwvtepSouthboundUtils.createRemoteMcastMacsInstanceIdentifier(nodeId,
-                remoteMcastMac.getKey());
+                remoteMcastMac.key());
         ResourceBatchingManager.getInstance().put(ResourceBatchingManager.ShardResource.CONFIG_TOPOLOGY,
                 iid, remoteMcastMac);
 
@@ -558,15 +557,13 @@ public class ElanL2GatewayMulticastUtils {
      */
     public List<ListenableFuture<Void>> handleMcastForElanL2GwDeviceDelete(String elanName,
                                                                            L2GatewayDevice l2GatewayDevice) {
-        ListenableFuture<Void> deleteTepFuture = txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
-            tx.delete(LogicalDatastoreType.CONFIGURATION,
-                    buildExternalTepPath(elanName, l2GatewayDevice.getTunnelIp()));
-        });
-        ListenableFuture<Void> updateMcastMacsFuture = updateMcastMacsForAllElanDevices(
-                elanName, l2GatewayDevice, false/* updateThisDevice */);
+        ListenableFuture<Void> deleteTepFuture =
+            txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                tx -> tx.delete(buildExternalTepPath(elanName, l2GatewayDevice.getTunnelIp())));
+        updateMcastMacsForAllElanDevices(elanName, l2GatewayDevice, false/* updateThisDevice */);
         ListenableFuture<Void> deleteRemoteMcastMacFuture = deleteRemoteMcastMac(
                 new NodeId(l2GatewayDevice.getHwvtepNodeId()), elanName);
-        return Arrays.asList(updateMcastMacsFuture, deleteRemoteMcastMacFuture, deleteTepFuture);
+        return Arrays.asList(deleteRemoteMcastMacFuture, deleteTepFuture);
     }
 
     /**