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);
}
}
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);
}
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);
}
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());
}
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);
}
}
}
Aggregations