use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutputBuilder in project genius by opendaylight.
the class AlivenessMonitor method monitorProfileCreate.
@Override
public Future<RpcResult<MonitorProfileCreateOutput>> monitorProfileCreate(final MonitorProfileCreateInput input) {
LOG.debug("Monitor Profile Create operation - {}", input.getProfile());
final SettableFuture<RpcResult<MonitorProfileCreateOutput>> returnFuture = SettableFuture.create();
Profile profile = input.getProfile();
final Long failureThreshold = profile.getFailureThreshold();
final Long monitorInterval = profile.getMonitorInterval();
final Long monitorWindow = profile.getMonitorWindow();
final EtherTypes ethType = profile.getProtocolType();
String idKey = getUniqueProfileKey(failureThreshold, monitorInterval, monitorWindow, ethType);
final Long profileId = (long) getUniqueId(idKey);
final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
ListenableFuture<Optional<MonitorProfile>> readFuture = tx.read(LogicalDatastoreType.OPERATIONAL, getMonitorProfileId(profileId));
ListenableFuture<RpcResult<MonitorProfileCreateOutput>> resultFuture = Futures.transformAsync(readFuture, optProfile -> {
if (optProfile.isPresent()) {
tx.cancel();
MonitorProfileCreateOutput output = new MonitorProfileCreateOutputBuilder().setProfileId(profileId).build();
String msg = String.format("Monitor profile %s already present for the given input", input);
LOG.warn(msg);
returnFuture.set(RpcResultBuilder.success(output).withWarning(ErrorType.PROTOCOL, "profile-exists", msg).build());
} else {
final MonitorProfile monitorProfile = new MonitorProfileBuilder().setId(profileId).setFailureThreshold(failureThreshold).setMonitorInterval(monitorInterval).setMonitorWindow(monitorWindow).setProtocolType(ethType).build();
tx.put(LogicalDatastoreType.OPERATIONAL, getMonitorProfileId(profileId), monitorProfile, CREATE_MISSING_PARENT);
Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
@Override
public void onFailure(Throwable error) {
String msg = String.format("Error when storing monitorprofile %s in datastore", monitorProfile);
LOG.error("Error when storing monitorprofile {} in datastore", monitorProfile, error);
returnFuture.set(RpcResultBuilder.<MonitorProfileCreateOutput>failed().withError(ErrorType.APPLICATION, msg, error).build());
}
@Override
public void onSuccess(Void noarg) {
MonitorProfileCreateOutput output = new MonitorProfileCreateOutputBuilder().setProfileId(profileId).build();
returnFuture.set(RpcResultBuilder.success(output).build());
}
}, callbackExecutorService);
}
return returnFuture;
}, callbackExecutorService);
Futures.addCallback(resultFuture, new FutureCallback<RpcResult<MonitorProfileCreateOutput>>() {
@Override
public void onFailure(Throwable error) {
// This would happen when any error happens during reading for
// monitoring profile
String msg = String.format("Error in creating monitorprofile - %s", input);
returnFuture.set(RpcResultBuilder.<MonitorProfileCreateOutput>failed().withError(ErrorType.APPLICATION, msg, error).build());
LOG.error("Error in creating monitorprofile - {} ", input, error);
}
@Override
public void onSuccess(RpcResult<MonitorProfileCreateOutput> result) {
LOG.debug("Successfully created monitor Profile {} ", input);
}
}, callbackExecutorService);
return returnFuture;
}
Aggregations