Migrate elanmanager to use LoggingFutures
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / utils / ElanL2GatewayUtils.java
index b345a2ef9dcc836e415b8c1ce273b0df2cc6ac04..bb53847f45060b574491dbabfd2f96b950a196ea 100644 (file)
@@ -31,12 +31,13 @@ import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 import java.util.stream.Collectors;
-import javax.annotation.Nonnull;
 import javax.annotation.PreDestroy;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
+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;
@@ -49,7 +50,7 @@ import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
 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.cache.ElanInstanceCache;
 import org.opendaylight.netvirt.elan.cache.ElanInstanceDpnsCache;
 import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil;
@@ -207,7 +208,7 @@ public class ElanL2GatewayUtils {
                     HwvtepSouthboundUtils.createPhysicalLocatorInstanceIdentifier(nodeId, expectedPhyLocatorAug));
             if (remoteMcastMac.getLocatorSet() != null) {
                 for (LocatorSet locatorSet : remoteMcastMac.getLocatorSet()) {
-                    if (locatorSet.getLocatorRef().equals(expectedPhyLocRef)) {
+                    if (Objects.equals(locatorSet.getLocatorRef(), expectedPhyLocRef)) {
                         LOG.trace("matched phyLocRef: {}", expectedPhyLocRef);
                         return true;
                     }
@@ -274,18 +275,6 @@ public class ElanL2GatewayUtils {
         return HwvtepUtils.deleteRemoteUcastMacs(broker, nodeId, logicalSwitchName, lstMac);
     }
 
-    public ElanInstance getElanInstanceForUcastLocalMac(LocalUcastMacs localUcastMac) {
-        Optional<LogicalSwitches> lsOpc = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL,
-                (InstanceIdentifier<LogicalSwitches>) localUcastMac.getLogicalSwitchRef().getValue());
-        if (lsOpc.isPresent()) {
-            LogicalSwitches ls = lsOpc.get();
-            // Logical switch name is Elan name
-            String elanName = getElanFromLogicalSwitch(ls.getHwvtepNodeName().getValue());
-            return elanInstanceCache.get(elanName).orNull();
-        }
-        return null;
-    }
-
     /**
      * Install external device local macs in dpn.
      *
@@ -361,7 +350,7 @@ public class ElanL2GatewayUtils {
     }
 
     public void installL2GwUcastMacInElan(final ElanInstance elan, final L2GatewayDevice extL2GwDevice,
-            final String macToBeAdded, final LocalUcastMacs localUcastMacs, String interfaceName) {
+            final String macToBeAdded, final LocalUcastMacs localUcastMacs, @Nullable String interfaceName) {
         final String extDeviceNodeId = extL2GwDevice.getHwvtepNodeId();
         final String elanInstanceName = elan.getElanInstanceName();
         final Collection<DpnInterfaces> elanDpns = getElanDpns(elanInstanceName);
@@ -771,6 +760,7 @@ public class ElanL2GatewayUtils {
      *            the interface name
      * @return the dpid from interface
      */
+    @Nullable
     public BigInteger getDpidFromInterface(String interfaceName) {
         BigInteger dpId = null;
         Future<RpcResult<GetDpidFromInterfaceOutput>> output = interfaceManagerRpcService
@@ -807,7 +797,7 @@ public class ElanL2GatewayUtils {
             return Futures.immediateFailedFuture(new RuntimeException(errMsg));
         }
 
-        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
+        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
             for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712
                     .l2gateway.attributes.devices.Interfaces deviceInterface : hwVtepDevice.getInterfaces()) {
                 //Removed the check for checking terminationPoint present in OP or not
@@ -847,7 +837,7 @@ public class ElanL2GatewayUtils {
      */
     public ListenableFuture<Void> updateVlanBindingsInL2GatewayDevice(NodeId nodeId, String psName,
             String interfaceName, List<VlanBindings> vlanBindings) {
-        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
+        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
             HwvtepUtils.mergeVlanBindings(tx, nodeId, psName, interfaceName, vlanBindings);
             LOG.info("Updated Hwvtep VlanBindings in config DS. NodeID: {}", nodeId.getValue());
         });
@@ -873,7 +863,7 @@ public class ElanL2GatewayUtils {
         }
         NodeId physicalSwitchNodeId = HwvtepSouthboundUtils.createManagedNodeId(nodeId, hwVtepDevice.getDeviceName());
 
-        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
+        return txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
             for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712
                     .l2gateway.attributes.devices.Interfaces deviceInterface : hwVtepDevice.getInterfaces()) {
                 String phyPortName = deviceInterface.getInterfaceName();
@@ -892,19 +882,6 @@ public class ElanL2GatewayUtils {
         });
     }
 
-    /**
-     * Gets the elan name from logical switch name.
-     *
-     * @param logicalSwitchName
-     *            the logical switch name
-     * @return the elan name from logical switch name
-     */
-    public static String getElanFromLogicalSwitch(String logicalSwitchName) {
-        // Assuming elan name is same as logical switch name
-        String elanName = logicalSwitchName;
-        return elanName;
-    }
-
     /**
      * Gets the logical switch name from elan name.
      *
@@ -935,6 +912,7 @@ public class ElanL2GatewayUtils {
         return interfaceInstanceIdentifierBuilder.build();
     }
 
+    @Nullable
     public static Interface getInterfaceFromConfigDS(InterfaceKey interfaceKey, DataBroker dataBroker) {
         InstanceIdentifier<Interface> interfaceId = getInterfaceIdentifier(interfaceKey);
         try {
@@ -1004,7 +982,7 @@ public class ElanL2GatewayUtils {
                 return;
             }
             String psNodeId = globalNodeId + HwvtepHAUtil.PHYSICALSWITCH + psName;
-            tzonesoptional.get().getTransportZone().stream()
+            tzonesoptional.get().nonnullTransportZone().stream()
                 .filter(transportZone -> transportZone.getSubnets() != null)
                 .flatMap(transportZone -> transportZone.getSubnets().stream())
                 .filter(subnet -> subnet.getDeviceVteps() != null)
@@ -1044,28 +1022,23 @@ public class ElanL2GatewayUtils {
             if (!optionalElan.isPresent()) {
                 return;
             }
-            JdkFutures.addErrorLogging(
+            LoggingFutures.addErrorLogging(
                 new ManagedNewTransactionRunnerImpl(dataBroker).callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
-                    tx -> {
-                        optionalElan.get().getElanInstance().stream()
-                        .flatMap(elan -> elan.getExternalTeps().stream()
-                                .map(externalTep -> ElanL2GatewayMulticastUtils.buildExternalTepPath(
-                                        elan.getElanInstanceName(), externalTep.getTepIp())))
+                    tx -> optionalElan.get().nonnullElanInstance().stream()
+                        .flatMap(elan -> elan.nonnullExternalTeps().stream()
+                            .map(externalTep -> ElanL2GatewayMulticastUtils.buildExternalTepPath(
+                                elan.getElanInstanceName(), externalTep.getTepIp())))
                         .filter(externalTepIid -> Objects.equals(
-                                deviceVteps.getIpAddress(), externalTepIid.firstKeyOf(ExternalTeps.class).getTepIp()))
+                            deviceVteps.getIpAddress(), externalTepIid.firstKeyOf(ExternalTeps.class).getTepIp()))
                         .peek(externalTepIid -> LOG.info("Deleting stale external tep {}", externalTepIid))
-                        .forEach(externalTepIid -> tx.delete(externalTepIid));
-                    }), LOG, "Failed to delete stale external teps {}", deviceVteps);
+                        .forEach(tx::delete)), LOG,
+                "Failed to delete stale external teps {}", deviceVteps);
             Thread.sleep(10000);//TODO remove the sleep currently it waits for interfacemgr to finish the cleanup
         } catch (ReadFailedException | InterruptedException e) {
             LOG.error("Failed to delete stale l2gw tep {}", deviceVteps, e);
         }
     }
 
-    public static String getNodeIdFromDpnId(BigInteger dpnId) {
-        return MDSALUtil.NODE_PREFIX + MDSALUtil.SEPARATOR + dpnId.toString();
-    }
-
     public void scheduleAddDpnMacInExtDevices(String elanName, BigInteger dpId,
             List<PhysAddress> staticMacAddresses) {
         ConcurrentMap<String, L2GatewayDevice> elanDevices = ElanL2GwCacheUtils.getInvolvedL2GwDevices(elanName);
@@ -1118,7 +1091,7 @@ public class ElanL2GatewayUtils {
         }
     }
 
-    @Nonnull
+    @NonNull
     public Collection<DpnInterfaces> getElanDpns(String elanName) {
         Collection<DpnInterfaces> dpnInterfaces = elanInstanceDpnsCache.get(elanName);
         if (!dpnInterfaces.isEmpty()) {