use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder 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());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder in project genius by opendaylight.
the class AlivenessMonitorTest method testMonitorStop.
@Test
public void testMonitorStop() throws InterruptedException, ExecutionException {
MonitorStopInput input = new MonitorStopInputBuilder().setMonitorId(2L).build();
Optional<MonitoringInfo> optInfo = Optional.of(new MonitoringInfoBuilder().setSource(new SourceBuilder().setEndpointType(getInterface("testInterface", "10.1.1.1")).build()).build());
CheckedFuture<Optional<MonitoringInfo>, ReadFailedException> outFuture = Futures.immediateCheckedFuture(optInfo);
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringInfo.class)))).thenReturn(outFuture);
Optional<MonitoridKeyEntry> optMap = Optional.of(new MonitoridKeyEntryBuilder().setMonitorId(2L).setMonitorKey("Test monitor Key").build());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoridKeyEntry.class)))).thenReturn(Futures.immediateCheckedFuture(optMap));
Optional<MonitorProfile> optProfile = Optional.of(getTestMonitorProfile());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitorProfile.class)))).thenReturn(Futures.immediateCheckedFuture(optProfile));
Optional<InterfaceMonitorEntry> optEntry = Optional.of(getInterfaceMonitorEntry());
when(readWriteTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(InterfaceMonitorEntry.class)))).thenReturn(Futures.immediateCheckedFuture(optEntry));
RpcResult<Void> result = alivenessMonitor.monitorStop(input).get();
verify(idManager).releaseId(any(ReleaseIdInput.class));
verify(writeTx, times(2)).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
assertTrue("Monitor stop rpc result", result.isSuccessful());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder in project genius by opendaylight.
the class AlivenessMonitorTest method testMonitorUnpause.
@Test
public void testMonitorUnpause() throws InterruptedException, ExecutionException {
MonitorUnpauseInput input = new MonitorUnpauseInputBuilder().setMonitorId(2L).build();
Optional<MonitoringState> optState = Optional.of(new MonitoringStateBuilder().setStatus(MonitorStatus.Paused).build());
when(readWriteTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringState.class)))).thenReturn(Futures.immediateCheckedFuture(optState));
Optional<MonitoringInfo> optInfo = Optional.of(new MonitoringInfoBuilder().setId(2L).setProfileId(1L).build());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringInfo.class)))).thenReturn(Futures.immediateCheckedFuture(optInfo));
Optional<MonitorProfile> optProfile = Optional.of(getTestMonitorProfile());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitorProfile.class)))).thenReturn(Futures.immediateCheckedFuture(optProfile));
Optional<MonitoridKeyEntry> optMap = Optional.of(new MonitoridKeyEntryBuilder().setMonitorId(2L).setMonitorKey("Test monitor Key").build());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoridKeyEntry.class)))).thenReturn(Futures.immediateCheckedFuture(optMap));
RpcResult<Void> result = alivenessMonitor.monitorUnpause(input).get();
verify(readWriteTx).merge(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringState.class)), stateCaptor.capture());
assertEquals(MonitorStatus.Started, stateCaptor.getValue().getStatus());
assertTrue("Monitor unpause rpc result", result.isSuccessful());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitorid.key.map.MonitoridKeyEntryBuilder in project genius by opendaylight.
the class AlivenessMonitorTest method testMonitorPause.
@Test
public void testMonitorPause() throws InterruptedException, ExecutionException {
MonitorPauseInput input = new MonitorPauseInputBuilder().setMonitorId(2L).build();
Optional<MonitorProfile> optProfile = Optional.of(getTestMonitorProfile());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitorProfile.class)))).thenReturn(Futures.immediateCheckedFuture(optProfile));
Optional<MonitoringInfo> optInfo = Optional.of(new MonitoringInfoBuilder().setId(2L).setProfileId(1L).build());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringInfo.class)))).thenReturn(Futures.immediateCheckedFuture(optInfo));
Optional<MonitoringState> optState = Optional.of(new MonitoringStateBuilder().setStatus(MonitorStatus.Started).build());
when(readWriteTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringState.class)))).thenReturn(Futures.immediateCheckedFuture(optState));
Optional<MonitoridKeyEntry> optMap = Optional.of(new MonitoridKeyEntryBuilder().setMonitorId(2L).setMonitorKey("Test monitor Key").build());
when(readTx.read(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoridKeyEntry.class)))).thenReturn(Futures.immediateCheckedFuture(optMap));
alivenessMonitor.monitorPause(input).get();
verify(readWriteTx).merge(eq(LogicalDatastoreType.OPERATIONAL), argThat(isType(MonitoringState.class)), stateCaptor.capture());
assertEquals(MonitorStatus.Paused, stateCaptor.getValue().getStatus());
}
Aggregations