Search in sources :

Example 6 with MonitorId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev171207.MonitorId in project genius by opendaylight.

the class AlivenessMonitorUtils method createOrUpdateInterfaceMonitorIdMap.

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 = tx.read(LogicalDatastoreType.OPERATIONAL, id).checkedGet();
    if (interfaceMonitorIdMap.isPresent()) {
        interfaceMonitorIdInstance = interfaceMonitorIdMap.get();
        existingMonitorIds = interfaceMonitorIdInstance.getMonitorId();
        if (existingMonitorIds == null) {
            existingMonitorIds = new ArrayList<>();
        }
        if (!existingMonitorIds.contains(monitorId)) {
            existingMonitorIds.add(monitorId);
            interfaceMonitorIdInstance = interfaceMonitorIdBuilder.setKey(new InterfaceMonitorIdKey(infName)).setMonitorId(existingMonitorIds).build();
            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();
        tx.merge(LogicalDatastoreType.OPERATIONAL, id, interfaceMonitorIdInstance, WriteTransaction.CREATE_MISSING_PARENTS);
    }
}
Also used : InterfaceMonitorId(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.monitor.id.map.InterfaceMonitorId) InterfaceMonitorIdKey(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.monitor.id.map.InterfaceMonitorIdKey) InterfaceMonitorIdBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.monitor.id.map.InterfaceMonitorIdBuilder)

Example 7 with MonitorId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev171207.MonitorId 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 8 with MonitorId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev171207.MonitorId in project genius by opendaylight.

the class AlivenessMonitor method publishNotification.

void publishNotification(final Long monitorId, final LivenessState state) {
    LOG.debug("Sending notification for id {}  - state {}", monitorId, state);
    EventData data = new EventDataBuilder().setMonitorId(monitorId).setMonitorState(state).build();
    MonitorEvent event = new MonitorEventBuilder().setEventData(data).build();
    final ListenableFuture<?> eventFuture = notificationPublishService.offerNotification(event);
    Futures.addCallback(eventFuture, new FutureCallback<Object>() {

        @Override
        public void onFailure(Throwable error) {
            LOG.warn("Error in notifying listeners for id {} - state {}", monitorId, state, error);
        }

        @Override
        public void onSuccess(Object arg) {
            LOG.trace("Successful in notifying listeners for id {} - state {}", monitorId, state);
        }
    }, callbackExecutorService);
}
Also used : MonitorEvent(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorEvent) EventDataBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.event.EventDataBuilder) MonitorEventBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorEventBuilder) EventData(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.event.EventData)

Example 9 with MonitorId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev171207.MonitorId in project genius by opendaylight.

the class AlivenessMonitor method associateMonitorIdWithInterface.

private void associateMonitorIdWithInterface(final Long monitorId, final String interfaceName) {
    LOG.debug("associate monitor Id {} with interface {}", monitorId, interfaceName);
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<InterfaceMonitorEntry>> readFuture = tx.read(LogicalDatastoreType.OPERATIONAL, getInterfaceMonitorMapId(interfaceName));
    ListenableFuture<Void> updateFuture = Futures.transformAsync(readFuture, optEntry -> {
        if (optEntry.isPresent()) {
            InterfaceMonitorEntry entry = optEntry.get();
            List<Long> monitorIds1 = entry.getMonitorIds();
            monitorIds1.add(monitorId);
            InterfaceMonitorEntry newEntry1 = new InterfaceMonitorEntryBuilder().setKey(new InterfaceMonitorEntryKey(interfaceName)).setMonitorIds(monitorIds1).build();
            tx.merge(LogicalDatastoreType.OPERATIONAL, getInterfaceMonitorMapId(interfaceName), newEntry1);
        } else {
            // Create new monitor entry
            LOG.debug("Adding new interface-monitor association for interface {} with id {}", interfaceName, monitorId);
            List<Long> monitorIds2 = new ArrayList<>();
            monitorIds2.add(monitorId);
            InterfaceMonitorEntry newEntry2 = new InterfaceMonitorEntryBuilder().setInterfaceName(interfaceName).setMonitorIds(monitorIds2).build();
            tx.put(LogicalDatastoreType.OPERATIONAL, getInterfaceMonitorMapId(interfaceName), newEntry2, CREATE_MISSING_PARENT);
        }
        return tx.submit();
    }, callbackExecutorService);
    Futures.addCallback(updateFuture, new FutureCallbackImpl(String.format("Association of monitorId %d with Interface %s", monitorId, interfaceName)), MoreExecutors.directExecutor());
}
Also used : Optional(com.google.common.base.Optional) InterfaceMonitorEntry(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411._interface.monitor.map.InterfaceMonitorEntry) InterfaceMonitorEntryBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411._interface.monitor.map.InterfaceMonitorEntryBuilder) ArrayList(java.util.ArrayList) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction) InterfaceMonitorEntryKey(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411._interface.monitor.map.InterfaceMonitorEntryKey)

Example 10 with MonitorId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev171207.MonitorId in project genius by opendaylight.

the class AlivenessMonitor method releaseIdForMonitoringInfo.

private void releaseIdForMonitoringInfo(MonitoringInfo info) {
    Long monitorId = info.getId();
    EndpointType source = info.getSource().getEndpointType();
    String interfaceName = getInterfaceName(source);
    if (!Strings.isNullOrEmpty(interfaceName)) {
        Optional<MonitorProfile> optProfile = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, getMonitorProfileId(info.getProfileId()));
        if (optProfile.isPresent()) {
            EtherTypes ethType = optProfile.get().getProtocolType();
            EndpointType destination = info.getDestination() != null ? info.getDestination().getEndpointType() : null;
            String idKey = getUniqueKey(interfaceName, ethType.toString(), source, destination);
            releaseId(idKey);
        } else {
            LOG.warn("Could not release monitorId {}. No profile associated with it", monitorId);
        }
    }
}
Also used : MonitorProfile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile) EndpointType(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.endpoint.EndpointType) EtherTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes)

Aggregations

Optional (com.google.common.base.Optional)10 ReadWriteTransaction (org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)8 EtherTypes (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes)7 MonitorProfile (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile)7 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)6 ArrayList (java.util.ArrayList)5 ExecutionException (java.util.concurrent.ExecutionException)5 MonitorStartInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartInput)5 MonitorStartOutput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStartOutput)5 MonitorStopInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorStopInput)5 FutureCallback (com.google.common.util.concurrent.FutureCallback)4 JdkFutureAdapters (com.google.common.util.concurrent.JdkFutureAdapters)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 List (java.util.List)4 Future (java.util.concurrent.Future)4 Nonnull (javax.annotation.Nonnull)4 Inject (javax.inject.Inject)4 Singleton (javax.inject.Singleton)4 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)4 ReadOnlyTransaction (org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)4