X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=interfacemanager%2Finterfacemanager-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fgenius%2Finterfacemanager%2Fcommons%2FAlivenessMonitorUtils.java;h=c360afec4b8af092c77f6a5e39b0eededb7ae3bb;hb=31d4f2256df0894e1fb1585a7cd94b800a9f60d2;hp=e139b7b571fb89d25e784ae9f84599fb10e9554e;hpb=cb84f3a3db823999b86c0c0e52ff9bc3af54a088;p=genius.git diff --git a/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/commons/AlivenessMonitorUtils.java b/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/commons/AlivenessMonitorUtils.java index e139b7b57..c360afec4 100644 --- a/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/commons/AlivenessMonitorUtils.java +++ b/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/commons/AlivenessMonitorUtils.java @@ -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> result = alivenessMonitorService.monitorStart(lldpMonitorInput); RpcResult 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 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 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> 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 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 id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class) .child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build(); - Optional 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 id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class) .child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build(); - Optional interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker); + Optional interfaceMonitorIdMap = tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet(); if (interfaceMonitorIdMap.isPresent()) { InterfaceMonitorId interfaceMonitorIdInstance = interfaceMonitorIdMap.get(); List 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> 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 existingMonitorIds; InterfaceMonitorIdBuilder interfaceMonitorIdBuilder = new InterfaceMonitorIdBuilder(); InstanceIdentifier id = InstanceIdentifier.builder(InterfaceMonitorIdMap.class) .child(InterfaceMonitorId.class, new InterfaceMonitorIdKey(infName)).build(); - Optional interfaceMonitorIdMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker); + Optional 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 id = InstanceIdentifier.builder(MonitorIdInterfaceMap.class) .child(MonitorIdInterface.class, new MonitorIdInterfaceKey(monitorId)).build(); - Optional monitorIdInterfaceMap = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, broker); + Optional 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 getMonitorIdForInterface(DataBroker broker, String infName) { + private static List getMonitorIdForInterface(ReadTransaction tx, String infName) throws ReadFailedException { InstanceIdentifier 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> result = alivenessMonitor + Future> result = alivenessMonitorService .monitorProfileCreate(monitorProfileCreateInput); RpcResult 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> existingProfile = alivenessMonitor.monitorProfileGet( + Future> existingProfile = alivenessMonitorService.monitorProfileGet( buildMonitorGetProfile(createProfile.getMonitorInterval(), createProfile.getMonitorWindow(), createProfile.getFailureThreshold(), createProfile.getProtocolType())); RpcResult 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) {