Remove use of JdkFutureAdapters
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / commons / AlivenessMonitorUtils.java
index e139b7b571fb89d25e784ae9f84599fb10e9554e..c360afec4b8af092c77f6a5e39b0eededb7ae3bb 100644 (file)
@@ -8,14 +8,22 @@
 package org.opendaylight.genius.interfacemanager.commons;
 
 import com.google.common.base.Optional;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
+import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.interfacemanager.IfmUtil;
-import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes;
@@ -24,6 +32,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileDeleteInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileDeleteInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileDeleteOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileGetInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileGetInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileGetOutput;
@@ -32,6 +41,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStopInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStopInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStopOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitoringMode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.params.SourceBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profile.create.input.Profile;
@@ -52,18 +62,23 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Singleton
 public final class AlivenessMonitorUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(AlivenessMonitorUtils.class);
     private static final long FAILURE_THRESHOLD = 4;
-    private static final long MONITORING_INTERVAL = 10000;
     private static final long MONITORING_WINDOW = 4;
 
-    private AlivenessMonitorUtils() {
+    private final AlivenessMonitorService alivenessMonitorService;
+    private final ManagedNewTransactionRunner txRunner;
+
+    @Inject
+    public AlivenessMonitorUtils(AlivenessMonitorService alivenessMonitor, DataBroker dataBroker) {
+        this.alivenessMonitorService = alivenessMonitor;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
     }
 
-    public static void startLLDPMonitoring(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
-            IfTunnel ifTunnel, String trunkInterfaceName) {
+    public void startLLDPMonitoring(IfTunnel ifTunnel, String trunkInterfaceName) {
         // LLDP monitoring for the tunnel interface
         if (lldpMonitoringEnabled(ifTunnel)) {
             MonitorStartInput lldpMonitorInput = new MonitorStartInputBuilder()
@@ -73,19 +88,20 @@ public final class AlivenessMonitorUtils {
                                             getInterfaceForMonitoring(trunkInterfaceName, ifTunnel.getTunnelSource()))
                                     .build())
                             .setMode(MonitoringMode.OneOne)
-                            .setProfileId(allocateProfile(alivenessMonitorService, FAILURE_THRESHOLD,
+                            .setProfileId(allocateProfile(FAILURE_THRESHOLD,
                                     ifTunnel.getMonitorInterval(), MONITORING_WINDOW, EtherTypes.Lldp))
                             .build())
                     .build();
             try {
                 Future<RpcResult<MonitorStartOutput>> result = alivenessMonitorService.monitorStart(lldpMonitorInput);
                 RpcResult<MonitorStartOutput> rpcResult = result.get();
-                long monitorId;
                 if (rpcResult.isSuccessful()) {
-                    monitorId = rpcResult.getResult().getMonitorId();
-                    createOrUpdateInterfaceMonitorIdMap(dataBroker, trunkInterfaceName, monitorId);
-                    createOrUpdateMonitorIdInterfaceMap(dataBroker, trunkInterfaceName, monitorId);
-                    LOG.trace("Started LLDP monitoring with id {}", monitorId);
+                    long monitorId = rpcResult.getResult().getMonitorId();
+                    ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+                        createOrUpdateInterfaceMonitorIdMap(tx, trunkInterfaceName, monitorId);
+                        createOrUpdateMonitorIdInterfaceMap(tx, trunkInterfaceName, monitorId);
+                        LOG.trace("Started LLDP monitoring with id {}", monitorId);
+                    }), LOG, "Error starting monitoring");
                 } else {
                     LOG.warn("RPC Call to start monitoring returned with Errors {}", rpcResult.getErrors());
                 }
@@ -95,58 +111,63 @@ public final class AlivenessMonitorUtils {
         }
     }
 
-    public static void stopLLDPMonitoring(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
-            IfTunnel ifTunnel, String trunkInterface) {
+    public void stopLLDPMonitoring(IfTunnel ifTunnel, String trunkInterface) {
         if (!lldpMonitoringEnabled(ifTunnel)) {
             return;
         }
         LOG.debug("stop LLDP monitoring for {}", trunkInterface);
-        List<Long> monitorIds = getMonitorIdForInterface(dataBroker, trunkInterface);
-        if (monitorIds == null) {
-            LOG.error("Monitor Id doesn't exist for Interface {}", trunkInterface);
-            return;
-        }
-        for (Long monitorId : monitorIds) {
-            String interfaceName = getInterfaceFromMonitorId(dataBroker, monitorId);
-            if (interfaceName != null) {
-                MonitorStopInput input = new MonitorStopInputBuilder().setMonitorId(monitorId).build();
-                alivenessMonitorService.monitorStop(input);
-                removeMonitorIdInterfaceMap(dataBroker, monitorId);
-                removeMonitorIdFromInterfaceMonitorIdMap(dataBroker, interfaceName, monitorId);
+        ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+            List<Long> monitorIds = getMonitorIdForInterface(tx, trunkInterface);
+            if (monitorIds == null) {
+                LOG.error("Monitor Id doesn't exist for Interface {}", trunkInterface);
                 return;
             }
-        }
+            for (Long monitorId : monitorIds) {
+                String interfaceName = getInterfaceFromMonitorId(tx, monitorId);
+                if (interfaceName != null) {
+                    MonitorStopInput input = new MonitorStopInputBuilder().setMonitorId(monitorId).build();
+
+                    ListenableFuture<RpcResult<MonitorStopOutput>> future = alivenessMonitorService.monitorStop(input);
+                    ListenableFutures.addErrorLogging(future, LOG, "Stop LLDP monitoring for {}", trunkInterface);
+
+                    removeMonitorIdInterfaceMap(tx, monitorId);
+                    removeMonitorIdFromInterfaceMonitorIdMap(tx, interfaceName, monitorId);
+                    return;
+                }
+            }
+        }), LOG, "Error stopping LLDP monitoring for {}", trunkInterface);
     }
 
-    public static String getInterfaceFromMonitorId(DataBroker broker, Long monitorId) {
+    public static String getInterfaceFromMonitorId(ReadTransaction tx, Long monitorId) throws ReadFailedException {
         InstanceIdentifier<MonitorIdInterface> id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class)
                 .child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build();
-        return IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker)
+        return tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet()
                 .toJavaUtil().map(MonitorIdInterface::getInterfaceName).orElse(null);
     }
 
-    private static void removeMonitorIdInterfaceMap(DataBroker broker, long monitorId) {
+    private void removeMonitorIdInterfaceMap(ReadWriteTransaction tx, long monitorId) throws ReadFailedException {
         InstanceIdentifier<MonitorIdInterface> id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class)
                 .child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build();
-        Optional<MonitorIdInterface> monitorIdInterfaceMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
-        if (monitorIdInterfaceMap.isPresent()) {
-            MDSALUtil.syncDelete(broker, LogicalDatastoreType.OPERATIONAL, id);
+        if (tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet().isPresent()) {
+            tx.delete(LogicalDatastoreType.OPERATIONAL, id);
         }
     }
 
-    private static void removeMonitorIdFromInterfaceMonitorIdMap(DataBroker broker, String infName, long monitorId) {
+    private void removeMonitorIdFromInterfaceMonitorIdMap(ReadWriteTransaction tx, String infName, long monitorId)
+            throws ReadFailedException {
         InstanceIdentifier<InterfaceMonitorId> id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class)
                 .child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build();
-        Optional<InterfaceMonitorId> interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
+        Optional<InterfaceMonitorId> interfaceMonitorIdMap = tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet();
         if (interfaceMonitorIdMap.isPresent()) {
             InterfaceMonitorId interfaceMonitorIdInstance = interfaceMonitorIdMap.get();
             List<Long> existingMonitorIds = interfaceMonitorIdInstance.getMonitorId();
             if (existingMonitorIds != null && existingMonitorIds.contains(monitorId)) {
                 existingMonitorIds.remove(monitorId);
                 InterfaceMonitorIdBuilder interfaceMonitorIdBuilder = new InterfaceMonitorIdBuilder();
-                interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setKey(new InterfaceMonitorIdKey(infName))
+                interfaceMonitorIdInstance = interfaceMonitorIdBuilder.withKey(new InterfaceMonitorIdKey(infName))
                         .setMonitorId(existingMonitorIds).build();
-                MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance);
+                tx.merge(LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance,
+                        WriteTransaction.CREATE_MISSING_PARENTS);
             }
         }
     }
@@ -161,39 +182,43 @@ public final class AlivenessMonitorUtils {
                 .setInterfaceIp(ipAddress).setInterfaceName(interfaceName).build();
     }
 
-    public static void handleTunnelMonitorUpdates(AlivenessMonitorService alivenessMonitorService,
-            DataBroker dataBroker, Interface interfaceOld, Interface interfaceNew) {
+    public void handleTunnelMonitorUpdates(Interface interfaceOld, Interface interfaceNew) {
         String interfaceName = interfaceNew.getName();
-        IfTunnel ifTunnelNew = interfaceNew.getAugmentation(IfTunnel.class);
+        IfTunnel ifTunnelNew = interfaceNew.augmentation(IfTunnel.class);
         if (!lldpMonitoringEnabled(ifTunnelNew)) {
             return;
         }
         LOG.debug("handling tunnel monitoring updates for interface {}", interfaceName);
 
-        stopLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnelNew, interfaceOld.getName());
+        stopLLDPMonitoring(ifTunnelNew, interfaceOld.getName());
         if (ifTunnelNew.isMonitorEnabled()) {
-            startLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnelNew, interfaceName);
+            startLLDPMonitoring(ifTunnelNew, interfaceName);
 
             // Delete old profile from Aliveness Manager
-            IfTunnel ifTunnelOld = interfaceOld.getAugmentation(IfTunnel.class);
+            IfTunnel ifTunnelOld = interfaceOld.augmentation(IfTunnel.class);
             if (!ifTunnelNew.getMonitorInterval().equals(ifTunnelOld.getMonitorInterval())) {
                 LOG.debug("deleting older monitor profile for interface {}", interfaceName);
-                long profileId = allocateProfile(alivenessMonitorService, FAILURE_THRESHOLD,
-                        ifTunnelOld.getMonitorInterval(), MONITORING_WINDOW, EtherTypes.Lldp);
+                long profileId = allocateProfile(FAILURE_THRESHOLD, ifTunnelOld.getMonitorInterval(), MONITORING_WINDOW,
+                        EtherTypes.Lldp);
                 MonitorProfileDeleteInput profileDeleteInput = new MonitorProfileDeleteInputBuilder()
                         .setProfileId(profileId).build();
-                alivenessMonitorService.monitorProfileDelete(profileDeleteInput);
+
+                ListenableFuture<RpcResult<MonitorProfileDeleteOutput>> future =
+                        alivenessMonitorService.monitorProfileDelete(profileDeleteInput);
+                ListenableFutures.addErrorLogging(future, LOG, "Delete monitor profile {}", interfaceName);
             }
         }
     }
 
-    public static void createOrUpdateInterfaceMonitorIdMap(DataBroker broker, String infName, long monitorId) {
+    private static void createOrUpdateInterfaceMonitorIdMap(ReadWriteTransaction tx, String infName, long monitorId)
+            throws ReadFailedException {
         InterfaceMonitorId interfaceMonitorIdInstance;
         List<Long> existingMonitorIds;
         InterfaceMonitorIdBuilder interfaceMonitorIdBuilder = new InterfaceMonitorIdBuilder();
         InstanceIdentifier<InterfaceMonitorId> id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class)
                 .child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build();
-        Optional<InterfaceMonitorId> interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
+        Optional<InterfaceMonitorId> interfaceMonitorIdMap =
+                tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet();
         if (interfaceMonitorIdMap.isPresent()) {
             interfaceMonitorIdInstance = interfaceMonitorIdMap.get();
             existingMonitorIds = interfaceMonitorIdInstance.getMonitorId();
@@ -202,52 +227,57 @@ public final class AlivenessMonitorUtils {
             }
             if (!existingMonitorIds.contains(monitorId)) {
                 existingMonitorIds.add(monitorId);
-                interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setKey(new InterfaceMonitorIdKey(infName))
+                interfaceMonitorIdInstance = interfaceMonitorIdBuilder.withKey(new InterfaceMonitorIdKey(infName))
                         .setMonitorId(existingMonitorIds).build();
-                MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance);
+                tx.merge(LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance,
+                        WriteTransaction.CREATE_MISSING_PARENTS);
             }
         } else {
             existingMonitorIds = new ArrayList<>();
             existingMonitorIds.add(monitorId);
             interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setMonitorId(existingMonitorIds)
-                    .setKey(new InterfaceMonitorIdKey(infName)).setInterfaceName(infName).build();
-            MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance);
+                    .withKey(new InterfaceMonitorIdKey(infName)).setInterfaceName(infName).build();
+            tx.merge(LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance,
+                    WriteTransaction.CREATE_MISSING_PARENTS);
         }
     }
 
-    public static void createOrUpdateMonitorIdInterfaceMap(DataBroker broker, String infName, long monitorId) {
+    private static void createOrUpdateMonitorIdInterfaceMap(ReadWriteTransaction tx, String infName, long monitorId)
+            throws ReadFailedException {
         MonitorIdInterface monitorIdInterfaceInstance;
         String existinginterfaceName;
         MonitorIdInterfaceBuilder monitorIdInterfaceBuilder = new MonitorIdInterfaceBuilder();
         InstanceIdentifier<MonitorIdInterface> id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class)
                 .child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build();
-        Optional<MonitorIdInterface> monitorIdInterfaceMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker);
+        Optional<MonitorIdInterface> monitorIdInterfaceMap =
+                tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet();
         if (monitorIdInterfaceMap.isPresent()) {
             monitorIdInterfaceInstance = monitorIdInterfaceMap.get();
             existinginterfaceName = monitorIdInterfaceInstance.getInterfaceName();
             if (!existinginterfaceName.equals(infName)) {
-                monitorIdInterfaceInstance = monitorIdInterfaceBuilder.setKey(new MonitorIdInterfaceKey(monitorId))
+                monitorIdInterfaceInstance = monitorIdInterfaceBuilder.withKey(new MonitorIdInterfaceKey(monitorId))
                         .setInterfaceName(infName).build();
-                MDSALUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, id, monitorIdInterfaceInstance);
+                tx.merge(LogicalDatastoreType.OPERATIONAL, id, monitorIdInterfaceInstance,
+                        WriteTransaction.CREATE_MISSING_PARENTS);
             }
         } else {
             monitorIdInterfaceInstance = monitorIdInterfaceBuilder.setMonitorId(monitorId)
-                    .setKey(new MonitorIdInterfaceKey(monitorId)).setInterfaceName(infName).build();
-            MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id, monitorIdInterfaceInstance);
+                    .withKey(new MonitorIdInterfaceKey(monitorId)).setInterfaceName(infName).build();
+            tx.merge(LogicalDatastoreType.OPERATIONAL, id, monitorIdInterfaceInstance,
+                    WriteTransaction.CREATE_MISSING_PARENTS);
         }
     }
 
-    public static List<Long> getMonitorIdForInterface(DataBroker broker, String infName) {
+    private static List<Long> getMonitorIdForInterface(ReadTransaction tx, String infName) throws ReadFailedException {
         InstanceIdentifier<InterfaceMonitorId> id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class)
                 .child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build();
-        return IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker).toJavaUtil().map(
+        return tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet().toJavaUtil().map(
                 InterfaceMonitorId::getMonitorId).orElse(null);
     }
 
-    public static long createMonitorProfile(AlivenessMonitorService alivenessMonitor,
-            MonitorProfileCreateInput monitorProfileCreateInput) {
+    public long createMonitorProfile(MonitorProfileCreateInput monitorProfileCreateInput) {
         try {
-            Future<RpcResult<MonitorProfileCreateOutput>> result = alivenessMonitor
+            Future<RpcResult<MonitorProfileCreateOutput>> result = alivenessMonitorService
                     .monitorProfileCreate(monitorProfileCreateInput);
             RpcResult<MonitorProfileCreateOutput> rpcResult = result.get();
             if (rpcResult.isSuccessful()) {
@@ -256,7 +286,7 @@ public final class AlivenessMonitorUtils {
                 LOG.warn("RPC Call to Get Profile Id Id returned with Errors {}.. Trying to fetch existing profile ID",
                         rpcResult.getErrors());
                 Profile createProfile = monitorProfileCreateInput.getProfile();
-                Future<RpcResult<MonitorProfileGetOutput>> existingProfile = alivenessMonitor.monitorProfileGet(
+                Future<RpcResult<MonitorProfileGetOutput>> existingProfile = alivenessMonitorService.monitorProfileGet(
                         buildMonitorGetProfile(createProfile.getMonitorInterval(), createProfile.getMonitorWindow(),
                                 createProfile.getFailureThreshold(), createProfile.getProtocolType()));
                 RpcResult<MonitorProfileGetOutput> rpcGetResult = existingProfile.get();
@@ -288,13 +318,13 @@ public final class AlivenessMonitorUtils {
         return buildGetProfile.build();
     }
 
-    public static long allocateProfile(AlivenessMonitorService alivenessMonitor, long failureThreshold,
-            long monitoringInterval, long monitoringWindow, EtherTypes etherTypes) {
+    public long allocateProfile(long failureThreshold, long monitoringInterval, long monitoringWindow,
+            EtherTypes etherTypes) {
         MonitorProfileCreateInput input = new MonitorProfileCreateInputBuilder().setProfile(
                 new ProfileBuilder().setFailureThreshold(failureThreshold).setMonitorInterval(monitoringInterval)
                         .setMonitorWindow(monitoringWindow).setProtocolType(etherTypes).build())
                 .build();
-        return createMonitorProfile(alivenessMonitor, input);
+        return createMonitorProfile(input);
     }
 
     public static boolean lldpMonitoringEnabled(IfTunnel ifTunnel) {