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:org.opendaylight.netconf.sal.connect.netconf.sal.SchemalessNetconfDeviceRpc.java

@Nonnull
@Override//  w  ww .  j ava  2s. co m
public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath type,
        @Nullable final NormalizedNode<?, ?> input) {
    final MessageTransformer<NetconfMessage> transformer;
    if (input instanceof AnyXmlNode) {
        transformer = schemalessTransformer;
    } else if (isBaseRpc(type)) {
        transformer = baseRpcTransformer;
    } else {
        return Futures.immediateFailedCheckedFuture(
                new DOMRpcImplementationNotAvailableException("Unable to invoke rpc %s", type));
    }
    return handleRpc(type, input, transformer);
}

From source file:org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker.java

@Override
protected CheckedFuture<Void, TransactionCommitFailedException> submit(
        final DOMDataWriteTransaction transaction, final Collection<DOMStoreThreePhaseCommitCohort> cohorts) {
    Preconditions.checkArgument(transaction != null, "Transaction must not be null.");
    Preconditions.checkArgument(cohorts != null, "Cohorts must not be null.");
    LOG.debug("Tx: {} is submitted for execution.", transaction.getIdentifier());

    ListenableFuture<Void> commitFuture = null;
    try {//from w  ww  . j ava2 s  .  c o  m
        commitFuture = executor.submit(new CommitCoordinationTask(transaction, cohorts, commitStatsTracker));
    } catch (RejectedExecutionException e) {
        LOG.error("The commit executor's queue is full - submit task was rejected. \n" + executor, e);
        return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException(
                "Could not submit the commit task - the commit queue capacity has been exceeded.", e));
    }

    return MappingCheckedFuture.create(commitFuture,
            TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER);
}

From source file:org.opendaylight.mdsal.dom.spi.store.SnapshotBackedReadTransaction.java

@Override
public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
    LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
    checkNotNull(path, "Path must not be null.");

    try {/*w  ww. ja  v  a  2  s  .c  o m*/
        return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
    } catch (final ReadFailedException e) {
        return Futures.immediateFailedCheckedFuture(e);
    }
}

From source file:org.opendaylight.controller.sal.core.spi.data.SnapshotBackedReadTransaction.java

@Override
public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
    LOG.debug("Tx: {} Exists: {}", getIdentifier(), path);
    checkNotNull(path, "Path must not be null.");

    try {//from  www .  j av  a 2  s . co  m
        return Futures.immediateCheckedFuture(read(path).checkedGet().isPresent());
    } catch (ReadFailedException e) {
        return Futures.immediateFailedCheckedFuture(e);
    }
}

From source file:org.opendaylight.aaa.authz.srv.AuthzDataReadWriteTransaction.java

@Override
public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
        LogicalDatastoreType logicalDatastoreType, YangInstanceIdentifier yangInstanceIdentifier) {

    if (AuthzServiceImpl.isAuthorized(logicalDatastoreType, yangInstanceIdentifier, ActionType.Read)) {
        return domDataReadWriteTransaction.read(logicalDatastoreType, yangInstanceIdentifier);
    }/*from w w w .j  a v  a2  s . c o  m*/
    ReadFailedException e = new ReadFailedException("Authorization Failed");
    return Futures.immediateFailedCheckedFuture(e);
}

From source file:org.opendaylight.aaa.authz.srv.AuthzWriteOnlyTransaction.java

@Deprecated
@Override/*from   ww w.  j ava  2  s. co m*/
public ListenableFuture<RpcResult<TransactionStatus>> commit() {
    if (AuthzServiceImpl.isAuthorized(ActionType.Commit)) {
        return domDataWriteTransaction.commit();
    }
    TransactionCommitFailedException e = new TransactionCommitFailedException("Unauthorized User");
    return Futures.immediateFailedCheckedFuture(e);
}

From source file:com.ibm.stocator.fs.cos.SemaphoredDelegatingExecutor.java

@Override
public <T> ListenableFuture<T> submit(Callable<T> task) {
    try {/*from w  w w .  ja  v a2  s  .  c o  m*/
        queueingPermits.acquire();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return Futures.immediateFailedCheckedFuture(e);
    }
    return super.submit(new CallableWithPermitRelease<>(task));
}

From source file:org.opendaylight.faas.fabric.general.EndPointRegister.java

@Override
public Future<RpcResult<Void>> unregisterEndpoint(UnregisterEndpointInput input) {

    final RpcResult<Void> result = RpcResultBuilder.<Void>success().build();

    if (input == null) {
        return Futures.immediateFailedCheckedFuture(new IllegalArgumentException("Endpoint can not be empty!"));
    }/*  ww w .  j  a va2  s .c  om*/
    final List<Uuid> toBeDeletedList = input.getIds();

    if (toBeDeletedList == null || toBeDeletedList.isEmpty()) {
        return Futures.immediateFuture(result);
    }

    ReadWriteTransaction trans = dataBroker.newReadWriteTransaction();

    for (Uuid ep : toBeDeletedList) {
        InstanceIdentifier<Endpoint> eppath = Constants.DOM_ENDPOINTS_PATH.child(Endpoint.class,
                new EndpointKey(ep));
        trans.delete(LogicalDatastoreType.OPERATIONAL, eppath);
    }
    CheckedFuture<Void, TransactionCommitFailedException> future = trans.submit();

    return Futures.transform(future, new AsyncFunction<Void, RpcResult<Void>>() {

        @Override
        public ListenableFuture<RpcResult<Void>> apply(Void input) throws Exception {
            return Futures.immediateFuture(result);
        }
    }, executor);
}

From source file:org.opendaylight.topoprocessing.impl.rpc.OverlayRpcImplementation.java

@Override
public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(DOMRpcIdentifier rpc,
        NormalizedNode<?, ?> input) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Invoked rpc: " + rpc);
    }/*from   w  w  w .  ja  va  2 s. co  m*/
    RpcDefinition rpcDefinition = findRpcDefinition(schemaContext, rpc.getType());
    if (null == rpcDefinition) {
        return Futures.immediateFailedCheckedFuture(
                (DOMRpcException) new DOMRpcException("Rpc definition not found") {
                });
    }
    RpcRoutingStrategy routingStrategy = RpcRoutingStrategy.from(rpcDefinition);
    DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerBuilder = ImmutableContainerNodeBuilder
            .create(ImmutableNodes.containerNode(input.getNodeType()));
    Collection<DataContainerChild<? extends PathArgument, ?>> inputChilds = ((ContainerNode) input).getValue();
    for (DataContainerChild<? extends PathArgument, ?> child : inputChilds) {
        if (!(child instanceof AugmentationNode)) {
            if (child.getNodeType().equals(routingStrategy.getLeaf())) {
                containerBuilder.addChild(ImmutableNodes.leafNode(child.getNodeType(), underlayItemIdentifier));
            } else {
                containerBuilder.addChild(child);
            }
        } else {
            containerBuilder.addChild(child);
        }
    }
    ContainerNode underlayRpcInput = containerBuilder.build();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Reinvoking RPC at: {} with input: {}", underlayItemIdentifier, underlayRpcInput);
    }
    return rpcService.invokeRpc(rpc.getType(), underlayRpcInput);
}

From source file:org.opendaylight.controller.clustering.it.provider.CarEntryDataTreeCommitCohort.java

@Override
public CheckedFuture<PostCanCommitStep, DataValidationFailedException> canCommit(final Object txId,
        final DOMDataTreeCandidate candidate, final SchemaContext ctx) {

    // Simple data validation - verify the year, if present, is >= 1990

    final DataTreeCandidateNode rootNode = candidate.getRootNode();
    final Optional<NormalizedNode<?, ?>> dataAfter = rootNode.getDataAfter();

    LOG.info("In canCommit: modificationType: {}, dataBefore: {}, dataAfter: {}",
            rootNode.getModificationType(), rootNode.getDataBefore(), dataAfter);

    // Note: we don't want to process DELETE modifications but we don't need to explicitly check the
    // ModificationType because dataAfter will not be present. Also dataAfter *should* always contain a
    // MapEntryNode but we verify anyway.
    if (dataAfter.isPresent()) {
        final NormalizedNode<?, ?> normalizedNode = dataAfter.get();
        Verify.verify(normalizedNode instanceof DataContainerNode,
                "Expected type DataContainerNode, actual was %s", normalizedNode.getClass());
        DataContainerNode<?> entryNode = (DataContainerNode<?>) normalizedNode;
        final Optional<DataContainerChild<? extends PathArgument, ?>> possibleYear = entryNode
                .getChild(YEAR_NODE_ID);
        if (possibleYear.isPresent()) {
            final Number year = (Number) possibleYear.get().getValue();

            LOG.info("year is {}", year);

            if (!(year.longValue() >= 1990)) {
                return Futures.immediateFailedCheckedFuture(
                        new DataValidationFailedException(DOMDataTreeIdentifier.class, candidate.getRootPath(),
                                String.format("Invalid year %d - year must be >= 1990", year)));
            }/*from w w w  .  j a v a2s . c o m*/
        }
    }

    // Return the noop PostCanCommitStep as we're only validating input data and not participating in the
    // remaining 3PC stages (pre-commit and commit).
    return PostCanCommitStep.NOOP_SUCCESS_FUTURE;
}