Search in sources :

Example 1 with MonitorProfileBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfileBuilder 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;
}
Also used : Optional(com.google.common.base.Optional) MonitorProfile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) MonitorProfileBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfileBuilder) EtherTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes) Profile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profile.create.input.Profile) MonitorProfile(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile) MonitorProfileCreateOutputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutputBuilder) MonitorProfileCreateOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutput) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)

Aggregations

Optional (com.google.common.base.Optional)1 ReadWriteTransaction (org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)1 EtherTypes (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.EtherTypes)1 MonitorProfileCreateOutput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutput)1 MonitorProfileCreateOutputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.MonitorProfileCreateOutputBuilder)1 Profile (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profile.create.input.Profile)1 MonitorProfile (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfile)1 MonitorProfileBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.monitor.profiles.MonitorProfileBuilder)1 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)1