Example usage for com.google.common.util.concurrent Futures immediateFailedCheckedFuture

List of usage examples for com.google.common.util.concurrent Futures immediateFailedCheckedFuture

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures immediateFailedCheckedFuture.

Prototype

@GwtIncompatible("TODO")
@CheckReturnValue
public static <V, X extends Exception> CheckedFuture<V, X> immediateFailedCheckedFuture(X exception) 

Source Link

Document

Returns a CheckedFuture which has an exception set immediately upon construction.

Usage

From source file:diskCacheV111.srm.dcache.Storage.java

@Override
public CheckedFuture<String, ? extends SRMException> unPinFileBySrmRequestId(SRMUser user, String fileId,
        String requestToken) {/*from ww  w .  j  a v a2  s  . c  o m*/
    try {
        return Futures.makeChecked(UnpinCompanion.unpinFileBySrmRequestId(asDcacheUser(user).getSubject(),
                new PnfsId(fileId), requestToken, _pinManagerStub, _executor), new ToSRMException());
    } catch (SRMAuthorizationException e) {
        return Futures.immediateFailedCheckedFuture(e);
    }
}

From source file:diskCacheV111.srm.dcache.Storage.java

@Override
public CheckedFuture<String, ? extends SRMException> unPinFile(SRMUser user, String fileId) {
    try {/*from   w  w w. j  a  v a  2  s .c  o m*/
        return Futures.makeChecked(UnpinCompanion.unpinFile(asDcacheUser(user).getSubject(), new PnfsId(fileId),
                _pinManagerStub, _executor), new ToSRMException());
    } catch (SRMAuthorizationException e) {
        return Futures.immediateFailedCheckedFuture(e);
    }
}

From source file:org.opendaylight.centinel.impl.CentinelStreamImpl.java

@Override
public Future<RpcResult<UpdateStreamOutput>> updateStream(final UpdateStreamInput input) {
    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<UpdateStreamOutput>> futureResult = SettableFuture.create();
    boolean idMatches = false;
    if (input.getStreamID() == null || input.getStreamID().isEmpty() || input.getStreamID().trim().isEmpty()) {
        LOG.debug("STREAM ID CANNOT BE NULL");
        return Futures.immediateFailedCheckedFuture(
                new TransactionCommitFailedException("inalid-input", streamIdcannotbenullError()));
    }//from  ww  w. j  av a 2 s .  c  o m
    final UpdateStreamOutputBuilder updateStreamRuleOutputBuilder = new UpdateStreamOutputBuilder();
    updateStreamRuleOutputBuilder.setTitle(input.getTitle());
    updateStreamRuleOutputBuilder.setDescription(input.getDescription());
    ListenableFuture<Optional<StreamRecord>> readFutureOperational = tx.read(LogicalDatastoreType.OPERATIONAL,
            streamRecordId);

    ListenableFuture<Optional<StreamRecord>> readFutureConfigure = tx.read(LogicalDatastoreType.CONFIGURATION,
            streamRecordId);

    String configId = null;

    try {
        Optional<StreamRecord> record = readFutureOperational.get();
        if (record.isPresent()) {
            StreamRecord operationalRecord = readFutureOperational.get().get();
            List<StreamList> streamRulesList = new ArrayList<StreamList>();
            if (!operationalRecord.getStreamList().isEmpty()) {
                streamRulesList = operationalRecord.getStreamList();
                Iterator<StreamList> iterator = streamRulesList.iterator();
                while (iterator.hasNext()) {
                    StreamList operationalObject = iterator.next();
                    if (operationalObject.getStreamID().equals(input.getStreamID())) {
                        configId = operationalObject.getConfigID();
                        idMatches = true;
                    }
                }
                if (!idMatches) {

                    return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException(
                            "invalid-input", RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                                    "Invalid Stream id or The stream is not present in operational data store")));
                }
            }
        } else {
            return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("invalid-input",
                    RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                            "Record is not present in operational data store")));
        }
    } catch (InterruptedException | ExecutionException e) {

        futureResult.set(RpcResultBuilder.<UpdateStreamOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
        return futureResult;
    }
    final String confId = configId;

    final ListenableFuture<Void> commitFuture = Futures.transform(readFutureConfigure,
            new AsyncFunction<Optional<StreamRecord>, Void>() {

                @Override
                public ListenableFuture<Void> apply(final Optional<StreamRecord> streamRulesRecord)
                        throws Exception {

                    List<StreamList> streamRuleList = new ArrayList<StreamList>();
                    List<StreamList> updatedStreamRuleList = new ArrayList<StreamList>();

                    if (streamRulesRecord.isPresent()) {

                        streamRuleList = streamRulesRecord.get().getStreamList();
                        StreamList configObject = null;
                        Iterator<StreamList> iterator = streamRuleList.iterator();

                        while (iterator.hasNext()) {

                            configObject = iterator.next();

                            if (configObject.getConfigID().equalsIgnoreCase(confId)) {

                                updatedStreamRuleList.add(buildUpdateStreamListRecord(input, configObject));

                                tx.merge(LogicalDatastoreType.CONFIGURATION, streamRecordId,
                                        new StreamRecordBuilder().setStreamList(updatedStreamRuleList).build());
                            }
                        }
                    }
                    return tx.submit();
                }

            });
    Futures.addCallback(commitFuture, new FutureCallback<Void>() {
        @Override
        public void onSuccess(final Void result) {

            futureResult.set(RpcResultBuilder.<UpdateStreamOutput>success(updateStreamRuleOutputBuilder.build())
                    .build());
        }

        @Override
        public void onFailure(final Throwable ex) {

            LOG.debug("Failed to commit Rule", ex);

            futureResult.set(RpcResultBuilder.<UpdateStreamOutput>failed()
                    .withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());
        }
    });
    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelAlertConditionImpl.java

@Override
public Future<RpcResult<SetAlertMessageCountRuleOutput>> setAlertMessageCountRule(
        final SetAlertMessageCountRuleInput input) {

    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<SetAlertMessageCountRuleOutput>> futureResult = SettableFuture.create();
    final String configId = generateConfigId();
    final SetAlertMessageCountRuleOutputBuilder setAlertMessageCountRuleOutputBuilder = new SetAlertMessageCountRuleOutputBuilder();
    setAlertMessageCountRuleOutputBuilder.setMessageCountOperator(input.getMessageCountOperator());
    setAlertMessageCountRuleOutputBuilder.setMessageCountCount(input.getMessageCountCount());
    setAlertMessageCountRuleOutputBuilder.setMessageCountGrace(input.getMessageCountGrace());
    setAlertMessageCountRuleOutputBuilder.setMessageCountBacklog(input.getMessageCountBacklog());
    setAlertMessageCountRuleOutputBuilder.setRuleID(input.getRuleID());
    setAlertMessageCountRuleOutputBuilder.setNodeType(input.getNodeType());
    setAlertMessageCountRuleOutputBuilder.setRuleTypeClassifier(input.getRuleTypeClassifier());
    setAlertMessageCountRuleOutputBuilder.setStreamID(input.getStreamID());
    setAlertMessageCountRuleOutputBuilder.setTimeStamp(input.getTimeStamp());
    setAlertMessageCountRuleOutputBuilder.setAlertTypeClassifier(input.getAlertTypeClassifier());
    setAlertMessageCountRuleOutputBuilder.setAlertName(input.getAlertName());
    setAlertMessageCountRuleOutputBuilder.setConfigID(configId);

    List<StreamAlertMessageCountRuleList> streamAlertRuleList = new ArrayList<StreamAlertMessageCountRuleList>();
    if (input.getAlertTypeClassifier() == null
            || input.getMessageCountCount() != null && input.getMessageCountCount() <= 0
            || input.getMessageCountCount() == null || input.getMessageCountOperator() == null
            || input.getMessageCountOperator().isEmpty() || input.getMessageCountOperator().trim().isEmpty()
            || input.getTimeStamp() != null && input.getTimeStamp() <= 0 || input.getTimeStamp() == null
            || input.getMessageCountBacklog() == null
            || input.getMessageCountBacklog() != null && input.getMessageCountBacklog() <= 0
            || input.getMessageCountGrace() == null
            || input.getMessageCountGrace() != null && input.getMessageCountGrace() <= 0) {
        LOG.debug("Invalid Parameters for SetAlertMessageCountRule ");
        return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("inalid-input",
                RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                        "StreamId,Classifier,Count,Operator,Grace,Backlog and Timestamp are mandatory fields")));

    }/*from  w w  w. j ava2s. com*/
    streamAlertRuleList.add(buildAlertMessageCountRuleRecord(input, configId));

    try {
        tx.merge(LogicalDatastoreType.CONFIGURATION, alertMessageCountRuleRecordId,
                new AlertMessageCountRuleRecordBuilder().setStreamAlertMessageCountRuleList(streamAlertRuleList)
                        .build(),
                true);
        tx.submit();
        futureResult.set(RpcResultBuilder
                .<SetAlertMessageCountRuleOutput>success(setAlertMessageCountRuleOutputBuilder.build())
                .build());
    }

    catch (Exception e) {
        LOG.info("Failed to commit Rule", e);
        futureResult.set(RpcResultBuilder.<SetAlertMessageCountRuleOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
    }

    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelStreamImpl.java

@Override
public Future<RpcResult<SetStreamOutput>> setStream(final SetStreamInput input) {

    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<SetStreamOutput>> futureResult = SettableFuture.create();
    final String configId = generateRandomId();
    if (input.getDescription() == null || input.getTitle() == null || input.getDescription().isEmpty()
            || input.getTitle().isEmpty() || input.getDescription().trim().isEmpty()
            || input.getTitle().trim().isEmpty()) {
        LOG.debug("Title and Description cannot be null");
        return Futures.immediateFailedCheckedFuture(
                new TransactionCommitFailedException("invalid-input", RpcResultBuilder.newError(
                        ErrorType.APPLICATION, "invalid-input", "Title and Description cannot be null")));
    }/*  w  w  w  .ja  v a  2  s. com*/
    final SetStreamOutputBuilder setStreamRuleOutputBuilder = new SetStreamOutputBuilder();

    setStreamRuleOutputBuilder.setDescription(input.getDescription());
    setStreamRuleOutputBuilder.setTitle(input.getTitle());
    setStreamRuleOutputBuilder.setConfigID(configId);
    List<StreamList> streamList = new ArrayList<StreamList>();
    streamList.add(buildstreamRecord(input, configId));
    try {
        tx.merge(LogicalDatastoreType.CONFIGURATION, streamRecordId,
                new StreamRecordBuilder().setStreamList(streamList).build(), true);
        tx.submit();

        futureResult.set(RpcResultBuilder.<SetStreamOutput>success(setStreamRuleOutputBuilder.build()).build());
    }

    catch (Exception e) {
        LOG.info("Failed to commit Rule", e);

        futureResult.set(RpcResultBuilder.<SetStreamOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
    }

    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelAlertConditionImpl.java

@Override
public Future<RpcResult<SetAlertFieldContentRuleOutput>> setAlertFieldContentRule(
        final SetAlertFieldContentRuleInput input) {
    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<SetAlertFieldContentRuleOutput>> futureResult = SettableFuture.create();
    final String configId = generateConfigId();
    final SetAlertFieldContentRuleOutputBuilder setAlertFieldContentRuleOutputBuilder = new SetAlertFieldContentRuleOutputBuilder();
    setAlertFieldContentRuleOutputBuilder.setAlertTypeClassifier(input.getAlertTypeClassifier());
    setAlertFieldContentRuleOutputBuilder.setFieldContentBacklog(input.getFieldContentBacklog());
    setAlertFieldContentRuleOutputBuilder.setFieldContentCompareToValue(input.getFieldContentCompareToValue());
    setAlertFieldContentRuleOutputBuilder.setFieldContentField(input.getFieldContentField());
    setAlertFieldContentRuleOutputBuilder.setFieldContentGrace(input.getFieldContentGrace());
    setAlertFieldContentRuleOutputBuilder.setNodeType(input.getNodeType());
    setAlertFieldContentRuleOutputBuilder.setRuleID(input.getRuleID());
    setAlertFieldContentRuleOutputBuilder.setStreamID(input.getStreamID());
    setAlertFieldContentRuleOutputBuilder.setRuleTypeClassifier(input.getRuleTypeClassifier());
    setAlertFieldContentRuleOutputBuilder.setTimeStamp(input.getTimeStamp());
    setAlertFieldContentRuleOutputBuilder.setAlertName(input.getAlertName());
    setAlertFieldContentRuleOutputBuilder.setConfigID(configId);

    List<StreamAlertFieldContentRuleList> streamAlertRuleList = new ArrayList<StreamAlertFieldContentRuleList>();
    if (input.getAlertTypeClassifier() == null || input.getFieldContentField() == null
            || input.getFieldContentField().isEmpty() || input.getFieldContentField().trim().isEmpty()
            || input.getFieldContentBacklog() == null
            || input.getFieldContentBacklog() != null && input.getFieldContentBacklog() <= 0
            || input.getFieldContentGrace() == null
            || input.getFieldContentGrace() != null && input.getFieldContentGrace() <= 0
            || input.getFieldContentCompareToValue() == null || input.getFieldContentCompareToValue().isEmpty()
            || input.getFieldContentCompareToValue().trim().isEmpty()) {
        LOG.debug("Invalid Parameters for SetAlertFieldContentRule ");
        return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("inalid-input",
                RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                        "StreamId,AlertTypeClassifier,FieldContentField,FieldContentGrace,FieldContentBacklog and FieldContentValue are mandatory fields")));
    }/*from   ww w  .  j a  v  a  2  s . co m*/
    streamAlertRuleList.add(buildAlertFieldContentRuleRecord(input, configId));

    try {
        tx.merge(LogicalDatastoreType.CONFIGURATION, alertFeildContentRuleRecordId,
                new AlertFieldContentRuleRecordBuilder().setStreamAlertFieldContentRuleList(streamAlertRuleList)
                        .build(),
                true);
        tx.submit();
        futureResult.set(RpcResultBuilder
                .<SetAlertFieldContentRuleOutput>success(setAlertFieldContentRuleOutputBuilder.build())
                .build());
    }

    catch (Exception e) {
        LOG.info("Failed to commit Rule", e);
        futureResult.set(RpcResultBuilder.<SetAlertFieldContentRuleOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
    }

    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelStreamImpl.java

@Override
public Future<RpcResult<PauseStreamOutput>> pauseStream(final PauseStreamInput input) {
    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<PauseStreamOutput>> futureResult = SettableFuture.create();
    boolean idMatches = false;
    if (input.getStreamID() == null || input.getStreamID().isEmpty() || input.getStreamID().trim().isEmpty()) {
        LOG.debug("STREAM ID CANNOT BE NULL");
        return Futures.immediateFailedCheckedFuture(
                new TransactionCommitFailedException("invalid-input", streamIdcannotbenullError()));
    }/* w  ww . j a  v  a  2s.  co m*/
    final PauseStreamOutputBuilder pauseStreamOutputBuilder = new PauseStreamOutputBuilder();
    pauseStreamOutputBuilder.setDisabled("true");
    ListenableFuture<Optional<StreamRecord>> readFutureOperational = tx.read(LogicalDatastoreType.OPERATIONAL,
            streamRecordId);

    ListenableFuture<Optional<StreamRecord>> readFutureConfigure = tx.read(LogicalDatastoreType.CONFIGURATION,
            streamRecordId);

    String configId = null;
    String opStreamId = null;

    try {
        Optional<StreamRecord> record = readFutureOperational.get();
        if (record.isPresent()) {
            StreamRecord operationalRecord = readFutureOperational.get().get();
            List<StreamList> streamRulesList = new ArrayList<StreamList>();

            if (!operationalRecord.getStreamList().isEmpty()) {

                streamRulesList = operationalRecord.getStreamList();
                Iterator<StreamList> iterator = streamRulesList.iterator();

                while (iterator.hasNext()) {

                    StreamList operationalObject = iterator.next();

                    if (operationalObject.getStreamID().equals(input.getStreamID())) {
                        configId = operationalObject.getConfigID();
                        opStreamId = operationalObject.getStreamID();
                        idMatches = true;

                    }
                }
                if (!idMatches) {
                    return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException(
                            "invalid-input", RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                                    "Invalid Stream id or The stream is not present in operational data store")));

                }

            }
        } else {
            return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("invalid-input",
                    RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                            "Record is not present in operational data store")));
        }
    }

    catch (InterruptedException | ExecutionException e) {

        futureResult.set(RpcResultBuilder.<PauseStreamOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
        return futureResult;

    }

    final String confID = configId;
    final String opStrmId = opStreamId;

    final ListenableFuture<Void> commitFuture = Futures.transform(readFutureConfigure,
            new AsyncFunction<Optional<StreamRecord>, Void>() {

                @Override
                public ListenableFuture<Void> apply(final Optional<StreamRecord> streamRulesRecord)
                        throws Exception {

                    List<StreamList> streamRuleList = new ArrayList<StreamList>();
                    List<StreamList> updatedStreamRuleList = new ArrayList<StreamList>();

                    if (streamRulesRecord.isPresent()) {

                        streamRuleList = streamRulesRecord.get().getStreamList();
                        StreamList configObject = null;
                        Iterator<StreamList> iterator = streamRuleList.iterator();

                        while (iterator.hasNext()) {

                            configObject = iterator.next();

                            if (configObject.getConfigID().equalsIgnoreCase(confID)) {

                                updatedStreamRuleList
                                        .add(buildPausedStreamListRecord(configObject, opStrmId, input));

                                tx.merge(LogicalDatastoreType.CONFIGURATION, streamRecordId,
                                        new StreamRecordBuilder().setStreamList(updatedStreamRuleList).build());

                            }
                        }

                    }

                    return tx.submit();
                }

            });
    Futures.addCallback(commitFuture, new FutureCallback<Void>() {
        @Override
        public void onSuccess(final Void result) {

            futureResult
                    .set(RpcResultBuilder.<PauseStreamOutput>success(pauseStreamOutputBuilder.build()).build());

        }

        @Override
        public void onFailure(final Throwable ex) {

            LOG.debug("Failed to commit Rule", ex);

            futureResult.set(RpcResultBuilder.<PauseStreamOutput>failed()
                    .withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());

        }
    });

    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelAlertConditionImpl.java

@Override
public Future<RpcResult<SetAlertFieldValueRuleOutput>> setAlertFieldValueRule(
        final SetAlertFieldValueRuleInput input) {

    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<SetAlertFieldValueRuleOutput>> futureResult = SettableFuture.create();
    final String configId = generateConfigId();
    final SetAlertFieldValueRuleOutputBuilder setAlertFieldValueRuleOutputBuilder = new SetAlertFieldValueRuleOutputBuilder();

    setAlertFieldValueRuleOutputBuilder.setAlertTypeClassifier(input.getAlertTypeClassifier());
    setAlertFieldValueRuleOutputBuilder.setFieldValueBacklog(input.getFieldValueBacklog());
    setAlertFieldValueRuleOutputBuilder.setFieldValueField(input.getFieldValueField());
    setAlertFieldValueRuleOutputBuilder.setFieldValueGrace(input.getFieldValueGrace());
    setAlertFieldValueRuleOutputBuilder.setFieldValueThreshhold(input.getFieldValueThreshhold());
    setAlertFieldValueRuleOutputBuilder.setFieldValueThreshholdType(input.getFieldValueThreshholdType());
    setAlertFieldValueRuleOutputBuilder.setFieldValueType(input.getFieldValueType());
    setAlertFieldValueRuleOutputBuilder.setNodeType(input.getNodeType());
    setAlertFieldValueRuleOutputBuilder.setRuleID(input.getRuleID());
    setAlertFieldValueRuleOutputBuilder.setRuleTypeClassifier(input.getRuleTypeClassifier());
    setAlertFieldValueRuleOutputBuilder.setStreamID(input.getStreamID());
    setAlertFieldValueRuleOutputBuilder.setTimeStamp(input.getTimeStamp());
    setAlertFieldValueRuleOutputBuilder.setAlertName(input.getAlertName());
    setAlertFieldValueRuleOutputBuilder.setConfigID(configId);

    List<StreamAlertFieldValueRuleList> streamAlertRuleList = new ArrayList<StreamAlertFieldValueRuleList>();
    if (input.getAlertTypeClassifier() == null || input.getFieldValueThreshhold() == null
            || input.getFieldValueThreshhold() != null && input.getFieldValueThreshhold() <= 0
            || input.getFieldValueThreshholdType() == null || input.getFieldValueThreshholdType().isEmpty()
            || input.getFieldValueThreshholdType().trim().isEmpty() || input.getTimeStamp() == null
            || input.getTimeStamp() != null && input.getTimeStamp() <= 0 || input.getFieldValueField() == null
            || input.getFieldValueField().isEmpty() || input.getFieldValueField().trim().isEmpty()
            || input.getFieldValueBacklog() == null
            || input.getFieldValueBacklog() != null && input.getFieldValueBacklog() <= 0
            || input.getFieldValueGrace() == null
            || input.getFieldValueGrace() != null && input.getFieldValueGrace() <= 0
            || input.getFieldValueType().isEmpty() || input.getFieldValueType().trim().isEmpty()
            || input.getFieldValueType() == null) {
        LOG.debug("Invalid Parameters for SetAlertFieldValueRule");
        return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("inalid-input",
                RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                        "StreamId,AlertTypeClassifier,FieldValueField,FieldValueThreshholdType,FieldValueThreshhold,FieldValueGrace,FieldValueBacklog and FieldValueTimestamp ,FieldValueType are mandatory fields")));
    }/*from  w w w .  j a  va  2  s  .  com*/
    streamAlertRuleList.add(buildAlertFieldValueRuleRecord(input, configId));

    try {
        tx.merge(LogicalDatastoreType.CONFIGURATION, alertFieldValueRuleRecordId,
                new AlertFieldValueRuleRecordBuilder().setStreamAlertFieldValueRuleList(streamAlertRuleList)
                        .build(),
                true);
        tx.submit();
        futureResult.set(RpcResultBuilder
                .<SetAlertFieldValueRuleOutput>success(setAlertFieldValueRuleOutputBuilder.build()).build());
    }

    catch (Exception e) {
        LOG.info("Failed to commit Rule", e);
        futureResult.set(RpcResultBuilder.<SetAlertFieldValueRuleOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
    }

    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelAlertConditionImpl.java

@Override
public Future<RpcResult<UpdateAlertMessageCountRuleOutput>> updateAlertMessageCountRule(
        final UpdateAlertMessageCountRuleInput input) {
    boolean idMatches = false;
    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<UpdateAlertMessageCountRuleOutput>> futureResult = SettableFuture.create();
    if (input.getStreamID() == null || input.getStreamID().isEmpty() || input.getStreamID().trim().isEmpty()
            || input.getRuleID() == null || input.getRuleID().isEmpty() || input.getRuleID().trim().isEmpty()) {
        LOG.debug("Invalid Parameters for UpdateAlertMessageCountRule");
        return Futures.immediateFailedCheckedFuture(
                new TransactionCommitFailedException("inalid-input", RpcResultBuilder.newError(
                        ErrorType.APPLICATION, "invalid-input", "StreamId and RuleId are mandatory fields")));
    }//from   w  w w .  ja  v  a 2  s.co  m
    final UpdateAlertMessageCountRuleOutputBuilder updateAlertMessageCountRuleOutputBuilder = new UpdateAlertMessageCountRuleOutputBuilder();
    updateAlertMessageCountRuleOutputBuilder.setMessageCountOperator(input.getMessageCountOperator());
    updateAlertMessageCountRuleOutputBuilder.setMessageCountCount(input.getMessageCountCount());
    updateAlertMessageCountRuleOutputBuilder.setMessageCountGrace(input.getMessageCountGrace());
    updateAlertMessageCountRuleOutputBuilder.setMessageCountBacklog(input.getMessageCountBacklog());
    updateAlertMessageCountRuleOutputBuilder.setRuleID(input.getRuleID());
    updateAlertMessageCountRuleOutputBuilder.setNodeType(input.getNodeType());
    updateAlertMessageCountRuleOutputBuilder.setRuleTypeClassifier(input.getRuleTypeClassifier());
    updateAlertMessageCountRuleOutputBuilder.setStreamID(input.getStreamID());
    updateAlertMessageCountRuleOutputBuilder.setTimeStamp(input.getTimeStamp());
    updateAlertMessageCountRuleOutputBuilder.setAlertTypeClassifier(input.getAlertTypeClassifier());
    updateAlertMessageCountRuleOutputBuilder.setAlertName(input.getAlertName());

    ListenableFuture<Optional<AlertMessageCountRuleRecord>> readFutureOperational = tx
            .read(LogicalDatastoreType.OPERATIONAL, alertMessageCountRuleRecordId);

    ListenableFuture<Optional<AlertMessageCountRuleRecord>> readFutureConfigure = tx
            .read(LogicalDatastoreType.CONFIGURATION, alertMessageCountRuleRecordId);

    String configId = null;

    try {

        Optional<AlertMessageCountRuleRecord> record = readFutureOperational.get();
        if (record.isPresent()) {
            AlertMessageCountRuleRecord operationalRecord = readFutureOperational.get().get();
            List<StreamAlertMessageCountRuleList> streamAlertRuleList = new ArrayList<StreamAlertMessageCountRuleList>();

            if (!operationalRecord.getStreamAlertMessageCountRuleList().isEmpty()) {

                streamAlertRuleList = operationalRecord.getStreamAlertMessageCountRuleList();
                Iterator<StreamAlertMessageCountRuleList> itearator = streamAlertRuleList.iterator();

                StreamAlertMessageCountRuleList operationalObject = null;

                while (itearator.hasNext()) {

                    operationalObject = itearator.next();
                    if (operationalObject.getRuleID().equalsIgnoreCase(input.getRuleID())
                            && operationalObject.getStreamID().equalsIgnoreCase(input.getStreamID())) {

                        configId = operationalObject.getConfigID();
                        idMatches = true;

                    }
                }
                if (!idMatches) {
                    return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException(
                            "invalid-input", RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                                    "Invalid Stream/Rule id or The stream/Rule is not present in operational data store")));

                }

            }

        } else {
            return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("invalid-input",
                    RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                            "Record is not present in operational data store")));
        }
    } catch (InterruptedException | ExecutionException e) {

        futureResult.set(RpcResultBuilder.<UpdateAlertMessageCountRuleOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
        return futureResult;

    }
    final String configID = configId;

    final ListenableFuture<Void> commitFuture = Futures.transform(readFutureConfigure,
            new AsyncFunction<Optional<AlertMessageCountRuleRecord>, Void>() {

                @Override
                public ListenableFuture<Void> apply(
                        final Optional<AlertMessageCountRuleRecord> alertMessageCountRuleRecord)
                        throws Exception {

                    List<StreamAlertMessageCountRuleList> streamAlertRuleList = new ArrayList<StreamAlertMessageCountRuleList>();
                    List<StreamAlertMessageCountRuleList> updatedStreamAlertRuleList = new ArrayList<StreamAlertMessageCountRuleList>();

                    if (alertMessageCountRuleRecord.isPresent()) {

                        streamAlertRuleList = alertMessageCountRuleRecord.get()
                                .getStreamAlertMessageCountRuleList();
                        StreamAlertMessageCountRuleList configObject = null;
                        Iterator<StreamAlertMessageCountRuleList> iterator = streamAlertRuleList.iterator();

                        while (iterator.hasNext()) {

                            configObject = iterator.next();

                            if (configObject.getConfigID().equalsIgnoreCase(configID)) {

                                updatedStreamAlertRuleList
                                        .add(buildUpdateAlertMessageCountRuleRecord(input, configObject));
                                tx.merge(LogicalDatastoreType.CONFIGURATION, alertMessageCountRuleRecordId,
                                        new AlertMessageCountRuleRecordBuilder()
                                                .setStreamAlertMessageCountRuleList(updatedStreamAlertRuleList)
                                                .build());

                            }
                        }

                    }

                    return tx.submit();
                }

            });
    Futures.addCallback(commitFuture, new FutureCallback<Void>() {
        @Override
        public void onSuccess(final Void result) {

            futureResult.set(RpcResultBuilder.<UpdateAlertMessageCountRuleOutput>success(
                    updateAlertMessageCountRuleOutputBuilder.build()).build());
        }

        @Override
        public void onFailure(final Throwable ex) {

            LOG.info("Failed to commit Rule", ex);

            futureResult.set(RpcResultBuilder.<UpdateAlertMessageCountRuleOutput>failed()
                    .withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());

        }
    });

    return futureResult;
}

From source file:org.opendaylight.centinel.impl.CentinelStreamImpl.java

@Override
public Future<RpcResult<ResumeStreamOutput>> resumeStream(final ResumeStreamInput input) {
    final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction();
    final SettableFuture<RpcResult<ResumeStreamOutput>> futureResult = SettableFuture.create();
    boolean idMatches = false;
    if (input.getStreamID() == null || input.getStreamID().isEmpty() || input.getStreamID().trim().isEmpty()) {
        LOG.debug("STREAM ID CANNOT BE NULL");
        return Futures.immediateFailedCheckedFuture(
                new TransactionCommitFailedException("invalid-input", streamIdcannotbenullError()));
    }/*from w  ww.  j  a va 2  s .  co m*/
    final ResumeStreamOutputBuilder resumeStreamOutputBuilder = new ResumeStreamOutputBuilder();
    resumeStreamOutputBuilder.setDisabled("false");
    ListenableFuture<Optional<StreamRecord>> readFutureOperational = tx.read(LogicalDatastoreType.OPERATIONAL,
            streamRecordId);

    ListenableFuture<Optional<StreamRecord>> readFutureConfigure = tx.read(LogicalDatastoreType.CONFIGURATION,
            streamRecordId);

    String configId = null;
    String opStrmId = null;

    try {
        Optional<StreamRecord> record = readFutureOperational.get();
        if (record.isPresent()) {
            StreamRecord operationalRecord = readFutureOperational.get().get();
            List<StreamList> streamRulesList = new ArrayList<StreamList>();

            if (!operationalRecord.getStreamList().isEmpty()) {

                streamRulesList = operationalRecord.getStreamList();
                Iterator<StreamList> iterator = streamRulesList.iterator();

                while (iterator.hasNext()) {

                    StreamList operationalObject = iterator.next();

                    if (operationalObject.getStreamID().equals(input.getStreamID())) {
                        configId = operationalObject.getConfigID();
                        opStrmId = operationalObject.getStreamID();
                        idMatches = true;

                    }
                }
                if (!idMatches) {
                    return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException(
                            "invalid-input", RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                                    "Invalid Stream id or The stream is not present in operational data store")));

                }
            }
        } else {
            return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("invalid-input",
                    RpcResultBuilder.newError(ErrorType.APPLICATION, "invalid-input",
                            "Record is not present in operational data store")));
        }

    } catch (InterruptedException | ExecutionException e) {

        futureResult.set(RpcResultBuilder.<ResumeStreamOutput>failed()
                .withRpcErrors(((TransactionCommitFailedException) e).getErrorList()).build());
        return futureResult;
    }
    final String confID = configId;
    final String opStreamId = opStrmId;

    final ListenableFuture<Void> commitFuture = Futures.transform(readFutureConfigure,
            new AsyncFunction<Optional<StreamRecord>, Void>() {

                @Override
                public ListenableFuture<Void> apply(final Optional<StreamRecord> streamRulesRecord)
                        throws Exception {

                    List<StreamList> streamRuleList = new ArrayList<StreamList>();
                    List<StreamList> updatedStreamRuleList = new ArrayList<StreamList>();

                    if (streamRulesRecord.isPresent()) {

                        streamRuleList = streamRulesRecord.get().getStreamList();
                        StreamList configObject = null;
                        Iterator<StreamList> iterator = streamRuleList.iterator();

                        while (iterator.hasNext()) {

                            configObject = iterator.next();

                            if (configObject.getConfigID().equalsIgnoreCase(confID)) {
                                StreamList updatedConfigObj = buildResumedStreamListRecord(configObject,
                                        opStreamId, input);

                                updatedStreamRuleList.add(updatedConfigObj);
                                tx.merge(LogicalDatastoreType.CONFIGURATION, streamRecordId,
                                        new StreamRecordBuilder().setStreamList(updatedStreamRuleList).build());

                            }
                        }

                    }

                    return tx.submit();
                }

            });
    Futures.addCallback(commitFuture, new FutureCallback<Void>() {
        @Override
        public void onSuccess(final Void result) {

            futureResult.set(
                    RpcResultBuilder.<ResumeStreamOutput>success(resumeStreamOutputBuilder.build()).build());
        }

        @Override
        public void onFailure(final Throwable ex) {

            LOG.debug("Failed to commit Rule", ex);

            futureResult.set(RpcResultBuilder.<ResumeStreamOutput>failed()
                    .withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());

        }
    });

    return futureResult;
}