Search in sources :

Example 1 with MonitoringState

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState in project genius by opendaylight.

the class AlivenessMonitor method sendMonitorPacket.

private void sendMonitorPacket(final MonitoringInfo monitoringInfo) {
    // TODO: Handle interrupts
    final Long monitorId = monitoringInfo.getId();
    final String monitorKey = monitorIdKeyCache.getUnchecked(monitorId);
    if (monitorKey == null) {
        LOG.warn("No monitor Key associated with id {} to send the monitor packet", monitorId);
        return;
    } else {
        LOG.debug("Sending monitoring packet for key: {}", monitorKey);
    }
    final MonitorProfile profile;
    Optional<MonitorProfile> optProfile = getMonitorProfile(monitoringInfo.getProfileId());
    if (optProfile.isPresent()) {
        profile = optProfile.get();
    } else {
        LOG.warn("No monitor profile associated with id {}. " + "Could not send Monitor packet for monitor-id {}", monitoringInfo.getProfileId(), monitorId);
        return;
    }
    final Semaphore lock = lockMap.get(monitorKey);
    LOG.debug("Acquiring lock for monitor key : {} to send monitor packet", monitorKey);
    acquireLock(lock);
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<MonitoringState>> readResult = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey));
    ListenableFuture<Void> writeResult = Futures.transformAsync(readResult, optState -> {
        if (optState.isPresent()) {
            MonitoringState state = optState.get();
            // Increase the request count
            Long requestCount = state.getRequestCount() + 1;
            // Check with the monitor window
            LivenessState currentLivenessState = state.getState();
            // Increase the pending response count
            long responsePendingCount = state.getResponsePendingCount();
            if (responsePendingCount < profile.getMonitorWindow()) {
                responsePendingCount = responsePendingCount + 1;
            }
            // Check with the failure threshold
            if (responsePendingCount >= profile.getFailureThreshold()) {
                // Change the state to down and notify
                if (currentLivenessState != LivenessState.Down) {
                    LOG.debug("Response pending Count: {}, Failure threshold: {} for monitorId {}", responsePendingCount, profile.getFailureThreshold(), state.getMonitorId());
                    LOG.info("Sending notification for monitor Id : {} with State: {}", state.getMonitorId(), LivenessState.Down);
                    publishNotification(monitorId, LivenessState.Down);
                    currentLivenessState = LivenessState.Down;
                    // Reset requestCount when state changes
                    // from UP to DOWN
                    requestCount = INITIAL_COUNT;
                }
            }
            // Update the ODS with state
            MonitoringState updatedState = new MonitoringStateBuilder().setMonitorKey(state.getMonitorKey()).setRequestCount(requestCount).setResponsePendingCount(responsePendingCount).setState(currentLivenessState).build();
            tx.merge(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(state.getMonitorKey()), updatedState);
            return tx.submit();
        } else {
            // Close the transaction
            tx.submit();
            String errorMsg = String.format("Monitoring State associated with id %d is not present to send packet out.", monitorId);
            return Futures.immediateFailedFuture(new RuntimeException(errorMsg));
        }
    }, callbackExecutorService);
    Futures.addCallback(writeResult, new FutureCallback<Void>() {

        @Override
        public void onSuccess(Void noarg) {
            // invoke packetout on protocol handler
            AlivenessProtocolHandler<?> handler = alivenessProtocolHandlerRegistry.getOpt(profile.getProtocolType());
            if (handler != null) {
                LOG.debug("Sending monitoring packet {}", monitoringInfo);
                handler.startMonitoringTask(monitoringInfo);
            }
            releaseLock(lock);
        }

        @Override
        public void onFailure(Throwable error) {
            LOG.warn("Updating monitoring state for key: {} failed. Monitoring packet is not sent", monitorKey, error);
            releaseLock(lock);
        }
    }, callbackExecutorService);
}
Also used : Optional(com.google.common.base.Optional) MonitoringStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder) MonitorProfile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile) Semaphore(java.util.concurrent.Semaphore) AlivenessProtocolHandler(org.opendaylight.genius.alivenessmonitor.protocols.AlivenessProtocolHandler) MonitoringState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState) LivenessState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.LivenessState) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)

Example 2 with MonitoringState

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState in project genius by opendaylight.

the class AlivenessMonitor method monitorStart.

@Override
public Future<RpcResult<MonitorStartOutput>> monitorStart(MonitorStartInput input) {
    RpcResultBuilder<MonitorStartOutput> rpcResultBuilder;
    final Config in = input.getConfig();
    Long profileId = in.getProfileId();
    LOG.debug("Monitor Start invoked with Config: {}, Profile Id: {}", in, profileId);
    try {
        if (in.getMode() != MonitoringMode.OneOne) {
            throw new UnsupportedConfigException("Unsupported Monitoring mode. Currently one-one mode is supported");
        }
        Optional<MonitorProfile> optProfile = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, getMonitorProfileId(profileId));
        final MonitorProfile profile;
        if (!optProfile.isPresent()) {
            String errMsg = String.format("No monitoring profile associated with Id: %d", profileId);
            LOG.error("Monitor start failed. {}", errMsg);
            throw new RuntimeException(errMsg);
        } else {
            profile = optProfile.get();
        }
        final EtherTypes ethType = profile.getProtocolType();
        String interfaceName = null;
        EndpointType srcEndpointType = in.getSource().getEndpointType();
        if (srcEndpointType instanceof Interface) {
            Interface endPoint = (Interface) srcEndpointType;
            interfaceName = endPoint.getInterfaceName();
        } else {
            throw new UnsupportedConfigException("Unsupported source Endpoint type. Only Interface Endpoint currently supported for monitoring");
        }
        if (Strings.isNullOrEmpty(interfaceName)) {
            throw new RuntimeException("Interface Name not defined in the source Endpoint");
        }
        // Initially the support is for one monitoring per interface.
        // Revisit the retrieving monitor id logic when the multiple
        // monitoring for same interface is needed.
        EndpointType destEndpointType = null;
        if (in.getDestination() != null) {
            destEndpointType = in.getDestination().getEndpointType();
        }
        String idKey = getUniqueKey(interfaceName, ethType.toString(), srcEndpointType, destEndpointType);
        final long monitorId = getUniqueId(idKey);
        Optional<MonitoringInfo> optKey = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, getMonitoringInfoId(monitorId));
        final AlivenessProtocolHandler<?> handler;
        if (optKey.isPresent()) {
            String message = String.format("Monitoring for the interface %s with this configuration " + "is already registered.", interfaceName);
            LOG.warn("Monitoring for the interface {} with this configuration is already registered.", interfaceName);
            MonitorStartOutput output = new MonitorStartOutputBuilder().setMonitorId(monitorId).build();
            rpcResultBuilder = RpcResultBuilder.success(output).withWarning(ErrorType.APPLICATION, "config-exists", message);
            return Futures.immediateFuture(rpcResultBuilder.build());
        } else {
            // Construct the monitor key
            final MonitoringInfo monitoringInfo = new MonitoringInfoBuilder().setId(monitorId).setMode(in.getMode()).setProfileId(profileId).setDestination(in.getDestination()).setSource(in.getSource()).build();
            // Construct the initial monitor state
            handler = alivenessProtocolHandlerRegistry.get(ethType);
            final String monitoringKey = handler.getUniqueMonitoringKey(monitoringInfo);
            MonitoringStateBuilder monitoringStateBuilder = new MonitoringStateBuilder().setMonitorKey(monitoringKey).setMonitorId(monitorId).setState(LivenessState.Unknown).setStatus(MonitorStatus.Started);
            if (ethType != EtherTypes.Bfd) {
                monitoringStateBuilder.setRequestCount(INITIAL_COUNT).setResponsePendingCount(INITIAL_COUNT);
            }
            MonitoringState monitoringState = monitoringStateBuilder.build();
            Futures.addCallback(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
                tx.put(LogicalDatastoreType.OPERATIONAL, getMonitoringInfoId(monitorId), monitoringInfo, CREATE_MISSING_PARENT);
                LOG.debug("adding oper monitoring info {}", monitoringInfo);
                tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitoringKey), monitoringState, CREATE_MISSING_PARENT);
                LOG.debug("adding oper monitoring state {}", monitoringState);
                MonitoridKeyEntry mapEntry = new MonitoridKeyEntryBuilder().setMonitorId(monitorId).setMonitorKey(monitoringKey).build();
                tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorMapId(monitorId), mapEntry, CREATE_MISSING_PARENT);
                LOG.debug("adding oper map entry {}", mapEntry);
            }), new FutureCallback<Void>() {

                @Override
                public void onFailure(Throwable error) {
                    String errorMsg = String.format("Adding Monitoring info: %s in Datastore failed", monitoringInfo);
                    LOG.warn("Adding Monitoring info: {} in Datastore failed", monitoringInfo, error);
                    throw new RuntimeException(errorMsg, error);
                }

                @Override
                public void onSuccess(Void noarg) {
                    lockMap.put(monitoringKey, new Semaphore(1, true));
                    if (ethType == EtherTypes.Bfd) {
                        handler.startMonitoringTask(monitoringInfo);
                        return;
                    }
                    // Schedule task
                    LOG.debug("Scheduling monitor task for config: {}", in);
                    scheduleMonitoringTask(monitoringInfo, profile.getMonitorInterval());
                }
            }, callbackExecutorService);
        }
        associateMonitorIdWithInterface(monitorId, interfaceName);
        MonitorStartOutput output = new MonitorStartOutputBuilder().setMonitorId(monitorId).build();
        rpcResultBuilder = RpcResultBuilder.success(output);
    } catch (UnsupportedConfigException e) {
        LOG.error("Start Monitoring Failed. ", e);
        rpcResultBuilder = RpcResultBuilder.<MonitorStartOutput>failed().withError(ErrorType.APPLICATION, e.getMessage(), e);
    }
    return Futures.immediateFuture(rpcResultBuilder.build());
}
Also used : IpAddress(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.endpoint.type.IpAddress) LoadingCache(com.google.common.cache.LoadingCache) EventData(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.event.EventData) PacketInReason(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketInReason) EventDataBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.event.EventDataBuilder) Future(java.util.concurrent.Future) MonitoringInfo(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfo) Optional(com.google.common.base.Optional) MonitoringState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState) CreateIdPoolInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput) ReleaseIdInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput) Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) InterfaceMonitorEntry(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411._interface.monitor.map.InterfaceMonitorEntry) ErrorType(org.opendaylight.yangtools.yang.common.RpcError.ErrorType) AlivenessProtocolHandler(org.opendaylight.genius.alivenessmonitor.protocols.AlivenessProtocolHandler) Executors(java.util.concurrent.Executors) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) MonitorProfileCreateOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutput) Predicate(com.google.common.base.Predicate) AlivenessMonitorService(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService) MonitorUnpauseInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorUnpauseInput) InterfaceMonitorEntryKey(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411._interface.monitor.map.InterfaceMonitorEntryKey) InterfaceMonitorEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411._interface.monitor.map.InterfaceMonitorEntryBuilder) MonitorProfileGetOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileGetOutput) MonitoringStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder) Interface(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.endpoint.type.Interface) MonitorProfileDeleteInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileDeleteInput) PacketReceived(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction) AllocateIdOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput) MonitorStatus(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStatus) ArrayList(java.util.ArrayList) AlivenessProtocolHandlerRegistry(org.opendaylight.genius.alivenessmonitor.protocols.AlivenessProtocolHandlerRegistry) Strings(com.google.common.base.Strings) MonitorProfileCreateOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutputBuilder) MonitorEventBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorEventBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MonitorPauseInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorPauseInput) EtherTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes) AlivenessMonitorUtil.getInterfaceMonitorMapId(org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitorUtil.getInterfaceMonitorMapId) FutureCallback(com.google.common.util.concurrent.FutureCallback) ExecutionException(java.util.concurrent.ExecutionException) MonitorStopInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStopInput) Futures(com.google.common.util.concurrent.Futures) MonitorStartInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartInput) MonitoringMode(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitoringMode) Preconditions(com.google.common.base.Preconditions) MonitorProfileGetInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileGetInput) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) AlivenessMonitorUtil.getMonitorProfileId(org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitorUtil.getMonitorProfileId) MonitorProfileCreateInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateInput) SingleTransactionDataBroker(org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker) ScheduledFuture(java.util.concurrent.ScheduledFuture) MonitoridKeyEntry(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntry) LoggerFactory(org.slf4j.LoggerFactory) NotificationPublishService(org.opendaylight.controller.md.sal.binding.api.NotificationPublishService) SettableFuture(com.google.common.util.concurrent.SettableFuture) MonitorEvent(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorEvent) ThreadFactoryProvider(org.opendaylight.infrautils.utils.concurrent.ThreadFactoryProvider) PreDestroy(javax.annotation.PreDestroy) MonitoringInfoBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfoBuilder) ManagedNewTransactionRunner(org.opendaylight.genius.infra.ManagedNewTransactionRunner) AlivenessMonitorUtil.getMonitoringInfoId(org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitorUtil.getMonitoringInfoId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) Packet(org.opendaylight.openflowplugin.libraries.liblldp.Packet) AlivenessMonitorUtil.getMonitorMapId(org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitorUtil.getMonitorMapId) AllocateIdInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) EndpointType(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.EndpointType) CacheBuilder(com.google.common.cache.CacheBuilder) MonitorStartOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartOutputBuilder) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) PacketProcessingListener(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener) LivenessState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.LivenessState) MonitorProfileGetOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileGetOutputBuilder) Singleton(javax.inject.Singleton) JdkFutureAdapters(com.google.common.util.concurrent.JdkFutureAdapters) NetUtils(org.opendaylight.openflowplugin.libraries.liblldp.NetUtils) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) SendToController(org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.SendToController) CreateIdPoolInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder) Profile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profile.create.input.Profile) ManagedNewTransactionRunnerImpl(org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl) Config(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.start.input.Config) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) MonitoridKeyEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) MonitorStartOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartOutput) ReleaseIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder) MonitorProfile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile) IdManagerService(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService) TimeUnit(java.util.concurrent.TimeUnit) MonitorProfileBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfileBuilder) RpcResultBuilder(org.opendaylight.yangtools.yang.common.RpcResultBuilder) AllocateIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder) Collections(java.util.Collections) AlivenessMonitorUtil.getMonitorStateId(org.opendaylight.genius.alivenessmonitor.internal.AlivenessMonitorUtil.getMonitorStateId) Config(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.start.input.Config) MonitoringStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder) MonitoringInfoBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfoBuilder) Semaphore(java.util.concurrent.Semaphore) EtherTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes) MonitoringState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState) MonitorStartOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartOutputBuilder) EndpointType(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.EndpointType) MonitorStartOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartOutput) MonitoringInfo(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfo) MonitorProfile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile) MonitoridKeyEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder) Interface(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.endpoint.type.Interface) MonitoridKeyEntry(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntry)

Example 3 with MonitoringState

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState in project genius by opendaylight.

the class AlivenessMonitor method updateMonitorStatusTo.

private void updateMonitorStatusTo(final Long monitorId, final MonitorStatus newStatus, final Predicate<MonitorStatus> isValidStatus) {
    final String monitorKey = monitorIdKeyCache.getUnchecked(monitorId);
    if (monitorKey == null) {
        LOG.warn("No monitor Key associated with id {} to change the monitor status to {}", monitorId, newStatus);
        return;
    }
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<MonitoringState>> readResult = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey));
    ListenableFuture<Void> writeResult = Futures.transformAsync(readResult, optState -> {
        if (optState.isPresent()) {
            MonitoringState state = optState.get();
            if (isValidStatus.apply(state.getStatus())) {
                MonitoringState updatedState = new MonitoringStateBuilder().setMonitorKey(monitorKey).setStatus(newStatus).build();
                tx.merge(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey), updatedState);
            } else {
                LOG.warn("Invalid Monitoring status {}, cannot be updated to {} for monitorId {}", state.getStatus(), newStatus, monitorId);
            }
        } else {
            LOG.warn("No associated monitoring state data available to update the status to {} for {}", newStatus, monitorId);
        }
        return tx.submit();
    }, MoreExecutors.directExecutor());
    Futures.addCallback(writeResult, new FutureCallbackImpl(String.format("Monitor status update for %d to %s", monitorId, newStatus.toString())), MoreExecutors.directExecutor());
}
Also used : MonitoringState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState) Optional(com.google.common.base.Optional) MonitoringStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)

Example 4 with MonitoringState

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState in project genius by opendaylight.

the class AlivenessMonitor method processReceivedMonitorKey.

private void processReceivedMonitorKey(final String monitorKey) {
    Preconditions.checkNotNull(monitorKey, "Monitor Key required to process the state");
    LOG.debug("Processing monitorKey: {} for received packet", monitorKey);
    final Semaphore lock = lockMap.get(monitorKey);
    LOG.debug("Acquiring lock for monitor key : {} to process monitor packet", monitorKey);
    acquireLock(lock);
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<MonitoringState>> stateResult = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey));
    // READ Callback
    Futures.addCallback(stateResult, new FutureCallback<Optional<MonitoringState>>() {

        @Override
        public void onSuccess(@Nonnull Optional<MonitoringState> optState) {
            if (optState.isPresent()) {
                final MonitoringState currentState = optState.get();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("OnPacketReceived : Monitoring state from ODS : {} ", currentState);
                }
                // Long responsePendingCount = currentState.getResponsePendingCount();
                // 
                // Need to relook at the pending count logic to support N
                // out of M scenarios
                // if (currentState.getState() != LivenessState.Up) {
                // //Reset responsePendingCount when state changes from DOWN
                // to UP
                // responsePendingCount = INITIAL_COUNT;
                // }
                // 
                // if (responsePendingCount > INITIAL_COUNT) {
                // responsePendingCount =
                // currentState.getResponsePendingCount() - 1;
                // }
                Long responsePendingCount = INITIAL_COUNT;
                final boolean stateChanged = currentState.getState() == LivenessState.Down || currentState.getState() == LivenessState.Unknown;
                final MonitoringState state = new MonitoringStateBuilder().setMonitorKey(monitorKey).setState(LivenessState.Up).setResponsePendingCount(responsePendingCount).build();
                tx.merge(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey), state);
                ListenableFuture<Void> writeResult = tx.submit();
                // WRITE Callback
                Futures.addCallback(writeResult, new FutureCallback<Void>() {

                    @Override
                    public void onSuccess(Void noarg) {
                        releaseLock(lock);
                        if (stateChanged) {
                            // send notifications
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Sending notification for monitor Id : {} with Current State: {}", currentState.getMonitorId(), LivenessState.Up);
                            }
                            publishNotification(currentState.getMonitorId(), LivenessState.Up);
                        } else {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Successful in writing monitoring state {} to ODS", state);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Throwable error) {
                        releaseLock(lock);
                        LOG.warn("Error in writing monitoring state : {} to Datastore", monitorKey, error);
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Error in writing monitoring state: {} to Datastore", state);
                        }
                    }
                }, callbackExecutorService);
            } else {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Monitoring State not available for key: {} to process the Packet received", monitorKey);
                }
                // Complete the transaction
                tx.submit();
                releaseLock(lock);
            }
        }

        @Override
        public void onFailure(Throwable error) {
            LOG.error("Error when reading Monitoring State for key: {} to process the Packet received", monitorKey, error);
            // FIXME: Not sure if the transaction status is valid to cancel
            tx.cancel();
            releaseLock(lock);
        }
    }, callbackExecutorService);
}
Also used : Optional(com.google.common.base.Optional) MonitoringStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder) Semaphore(java.util.concurrent.Semaphore) MonitoringState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Example 5 with MonitoringState

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState in project genius by opendaylight.

the class HwVtepTunnelsStateHandler method update.

@Override
public void update(@Nonnull Tunnels oldTunnelInfo, @Nonnull Tunnels updatedTunnelInfo) {
    List<BfdStatus> oldBfdStatus = oldTunnelInfo.getBfdStatus();
    List<BfdStatus> newBfdStatus = updatedTunnelInfo.getBfdStatus();
    LivenessState oldTunnelOpState = getTunnelOpState(oldBfdStatus);
    final LivenessState newTunnelOpState = getTunnelOpState(newBfdStatus);
    if (oldTunnelOpState == newTunnelOpState) {
        LOG.debug("Tunnel state of old tunnel {} and update tunnel {} are same", oldTunnelInfo, updatedTunnelInfo);
        return;
    }
    updatedTunnelInfo.getTunnelUuid();
    String interfaceName = "<TODO>";
    // TODO: find out the corresponding interface using tunnelIdentifier or
    // any attributes of tunnelInfo object
    final String monitorKey = getBfdMonitorKey(interfaceName);
    LOG.debug("Processing monitorKey: {} for received Tunnels update DCN", monitorKey);
    final Semaphore lock = alivenessMonitor.getLock(monitorKey);
    LOG.debug("Acquiring lock for monitor key : {} to process monitor DCN", monitorKey);
    alivenessMonitor.acquireLock(lock);
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<MonitoringState>> stateResult = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey));
    Futures.addCallback(stateResult, new FutureCallback<Optional<MonitoringState>>() {

        @Override
        public void onSuccess(@Nonnull Optional<MonitoringState> optState) {
            if (optState.isPresent()) {
                final MonitoringState currentState = optState.get();
                if (currentState.getState() == newTunnelOpState) {
                    return;
                }
                final boolean stateChanged = true;
                final MonitoringState state = new MonitoringStateBuilder().setMonitorKey(monitorKey).setState(newTunnelOpState).build();
                tx.merge(LogicalDatastoreType.OPERATIONAL, getMonitorStateId(monitorKey), state);
                ListenableFuture<Void> writeResult = tx.submit();
                // WRITE Callback
                Futures.addCallback(writeResult, new FutureCallback<Void>() {

                    @Override
                    public void onSuccess(Void arg0) {
                        alivenessMonitor.releaseLock(lock);
                        if (stateChanged) {
                            // send notifications
                            LOG.info("Sending notification for monitor Id : {} with Current State: {}", currentState.getMonitorId(), newTunnelOpState);
                            alivenessMonitor.publishNotification(currentState.getMonitorId(), newTunnelOpState);
                        } else {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Successful in writing monitoring state {} to ODS", state);
                            }
                        }
                    }

                    @Override
                    public void onFailure(@Nonnull Throwable error) {
                        alivenessMonitor.releaseLock(lock);
                        LOG.warn("Error in writing monitoring state : {} to Datastore", monitorKey, error);
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Error in writing monitoring state: {} to Datastore", state);
                        }
                    }
                }, MoreExecutors.directExecutor());
            } else {
                LOG.warn("Monitoring State not available for key: {} to process the Packet received", monitorKey);
                // Complete the transaction
                tx.submit();
                alivenessMonitor.releaseLock(lock);
            }
        }

        @Override
        public void onFailure(@Nonnull Throwable error) {
            LOG.error("Error when reading Monitoring State for key: {} to process the Packet received", monitorKey, error);
            // FIXME: Not sure if the transaction status is valid to cancel
            tx.cancel();
            alivenessMonitor.releaseLock(lock);
        }
    }, MoreExecutors.directExecutor());
}
Also used : Optional(com.google.common.base.Optional) Nonnull(javax.annotation.Nonnull) MonitoringStateBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder) Semaphore(java.util.concurrent.Semaphore) BfdStatus(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.tunnel.attributes.BfdStatus) MonitoringState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState) LivenessState(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.LivenessState) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Aggregations

MonitoringState (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringState)7 MonitoringStateBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitoring.states.MonitoringStateBuilder)7 Optional (com.google.common.base.Optional)5 ReadWriteTransaction (org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)5 Semaphore (java.util.concurrent.Semaphore)4 MonitorProfile (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile)4 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 LivenessState (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.LivenessState)3 MonitoringInfo (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfo)3 MonitoringInfoBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.configs.MonitoringInfoBuilder)3 MonitoridKeyEntry (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntry)3 MonitoridKeyEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder)3 Nonnull (javax.annotation.Nonnull)2 Test (org.junit.Test)2 MonitorPauseInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorPauseInput)2 MonitorUnpauseInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorUnpauseInput)2 Preconditions (com.google.common.base.Preconditions)1 Predicate (com.google.common.base.Predicate)1 Strings (com.google.common.base.Strings)1