Example usage for com.google.common.util.concurrent CheckedFuture checkedGet

List of usage examples for com.google.common.util.concurrent CheckedFuture checkedGet

Introduction

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

Prototype

V checkedGet() throws X;

Source Link

Document

Exception checking version of Future#get() that will translate InterruptedException , CancellationException and ExecutionException into application-specific exceptions.

Usage

From source file:org.opendaylight.natapp.impl.NatYangStore.java

private <T extends DataObject> T getDataObject(final InstanceIdentifier<T> instanceIdentifier) {
    ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();

    Optional<T> optionalData = null;
    try {/* ww w  .  j a v a2  s  .  c  om*/
        CheckedFuture<Optional<T>, ReadFailedException> readFuture = readOnlyTransaction
                .read(LogicalDatastoreType.CONFIGURATION, instanceIdentifier);
        optionalData = readFuture.checkedGet();
        if (optionalData.isPresent()) {
            return optionalData.get();
        }
    } catch (Exception e) {
        LOG.info("Exception while executing getDataObject: ", e);
    }
    return null;
}

From source file:org.opendaylight.dsbenchmark.txchain.TxchainBaRead.java

@Override
public void executeList() {

    BindingTransactionChain chain = bindingDataBroker.createTransactionChain(this);
    ReadTransaction tx = bindingDataBroker.newReadOnlyTransaction();

    for (long l = 0; l < outerListElem; l++) {

        OuterList outerList;//from w w  w  .j  av  a 2 s .  c  om
        InstanceIdentifier<OuterList> iid = InstanceIdentifier.create(TestExec.class).child(OuterList.class,
                new OuterListKey((int) l));
        Optional<OuterList> optionalDataObject;
        CheckedFuture<Optional<OuterList>, ReadFailedException> submitFuture = tx
                .read(LogicalDatastoreType.CONFIGURATION, iid);

        try {
            optionalDataObject = submitFuture.checkedGet();
            /*if (optionalDataObject != null && optionalDataObject.isPresent()) {
            ret = optionalDataObject.get();
            txOk++;
            }*/
            if (optionalDataObject != null && optionalDataObject.isPresent()) {
                outerList = optionalDataObject.get();

                String[] objectsArray = new String[outerList.getInnerList().size()];

                //LOG.info("innerList element: " + objectsArray );
                for (InnerList innerList : outerList.getInnerList()) {
                    if (objectsArray[innerList.getName()] != null) {
                        LOG.error("innerList: DUPLICATE name: {}, value: {}", innerList.getName(),
                                innerList.getValue());
                    }
                    objectsArray[innerList.getName()] = innerList.getValue();
                    // LOG.info("innerList: name: {}, value: {}", innerList.getName(), innerList.getValue());
                }
                boolean foundAll = true;
                for (int i = 0; i < outerList.getInnerList().size(); i++) {
                    String itemStr = objectsArray[i];
                    if (!itemStr.contentEquals("Item-" + String.valueOf(l) + "-" + String.valueOf(i))) {
                        foundAll = false;
                        LOG.error("innerList: name: {}, value: {}", i, itemStr);
                        break;
                    }
                }
                txOk++;
            } else {
                txError++;
            }
        } catch (ReadFailedException e) {
            LOG.warn("failed to ....", e);
            txError++;
        }
    }
}

From source file:org.opendaylight.hello.impl.HelloWorldImpl.java

private String readFromGreetingRegistry(HelloWorldInput input) {
    String result = "Hello " + input.getName();
    ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
    InstanceIdentifier<GreetingRegistryEntry> iid = toInstanceIdentifier(input);
    CheckedFuture<Optional<GreetingRegistryEntry>, ReadFailedException> future = transaction
            .read(LogicalDatastoreType.CONFIGURATION, iid);
    Optional<GreetingRegistryEntry> optional = Optional.absent();
    try {/*from   w w  w . j  av a 2 s  . co  m*/
        optional = future.checkedGet();
    } catch (ReadFailedException e) {
        LOG.warn("Reading greeting failed:", e);
    }
    if (optional.isPresent()) {
        result = optional.get().getGreeting();
    }

    //LOG.info("1>>> I read from Greeting Registry "+ result +" <<<");
    return result;
}

From source file:org.opendaylight.distributed.tx.it.provider.datawriter.DtxDataStoreAsyncWriter.java

/**
 * Asynchronously write to datastore with distributed-tx API
 *//*from  w  w w  .j  a  v a 2 s. c o  m*/
@Override
public void writeData() {
    int putsPerTx = input.getPutsPerTx();
    int counter = 0;

    InstanceIdentifier<DatastoreTestData> nodeId = InstanceIdentifier.create(DatastoreTestData.class);
    List<ListenableFuture<Void>> putFutures = new ArrayList<ListenableFuture<Void>>((int) putsPerTx);
    List<OuterList> outerLists = dataStoreListBuilder.buildOuterList();

    if (input.getOperation() == OperationType.DELETE) {
        dataStoreListBuilder.buildTestInnerList();
    }

    dtx = dTxProvider.newTx(nodesMap);
    startTime = System.nanoTime();
    for (OuterList outerList : outerLists) {
        for (InnerList innerList : outerList.getInnerList()) {
            InstanceIdentifier<InnerList> innerIid = InstanceIdentifier.create(DatastoreTestData.class)
                    .child(OuterList.class, outerList.getKey()).child(InnerList.class, innerList.getKey());

            CheckedFuture<Void, DTxException> writeFuture;
            if (input.getOperation() == OperationType.PUT) {
                writeFuture = dtx.putAndRollbackOnFailure(DTXLogicalTXProviderType.DATASTORE_TX_PROVIDER,
                        LogicalDatastoreType.CONFIGURATION, innerIid, innerList, nodeId);
            } else if (input.getOperation() == OperationType.MERGE) {
                writeFuture = dtx.mergeAndRollbackOnFailure(DTXLogicalTXProviderType.DATASTORE_TX_PROVIDER,
                        LogicalDatastoreType.CONFIGURATION, innerIid, innerList, nodeId);
            } else {
                writeFuture = dtx.deleteAndRollbackOnFailure(DTXLogicalTXProviderType.DATASTORE_TX_PROVIDER,
                        LogicalDatastoreType.CONFIGURATION, innerIid, nodeId);
            }
            putFutures.add(writeFuture);
            counter++;

            if (counter == putsPerTx) {
                //Aggregate all the put futures into a listenable future which can ensure all asynchronous writes has been finished
                ListenableFuture<Void> aggregatePutFuture = Futures.transform(Futures.allAsList(putFutures),
                        new Function<List<Void>, Void>() {
                            @Nullable
                            @Override
                            public Void apply(@Nullable List<Void> voids) {
                                return null;
                            }
                        });

                try {
                    aggregatePutFuture.get();
                    CheckedFuture<Void, TransactionCommitFailedException> submitFuture = dtx.submit();
                    try {
                        submitFuture.checkedGet();
                        txSucceed++;
                    } catch (TransactionCommitFailedException e) {
                        txError++;
                    }
                } catch (Exception e) {
                    txError++;
                    dtx.cancel();
                }

                counter = 0;
                dtx = dTxProvider.newTx(nodesMap);
                putFutures = new ArrayList<ListenableFuture<Void>>(putsPerTx);
            }
        }
    }

    ListenableFuture<Void> aggregatePutFuture = Futures.transform(Futures.allAsList(putFutures),
            new Function<List<Void>, Void>() {
                @Nullable
                @Override
                public Void apply(@Nullable List<Void> voids) {
                    return null;
                }
            });

    try {
        aggregatePutFuture.get();
        CheckedFuture<Void, TransactionCommitFailedException> restSubmitFuture = dtx.submit();
        try {
            restSubmitFuture.checkedGet();
            txSucceed++;
        } catch (Exception e) {
            txError++;
        }
    } catch (Exception e) {
        txError++;
    }
    endTime = System.nanoTime();
}

From source file:org.opendaylight.aaa.authn.mdsal.store.AuthNStore.java

private void deleteUserTokenFromDS(String token, String userId) {
    final InstanceIdentifier<UserTokens> userTokens_iid = AuthNStoreUtil.createInstIdentifierUserTokens(userId,
            token);//from w  w  w .  j  av  a  2s .  c o m

    WriteTransaction tx = broker.newWriteOnlyTransaction();
    tx.delete(LogicalDatastoreType.OPERATIONAL, userTokens_iid);
    CheckedFuture<Void, TransactionCommitFailedException> commitFuture = tx.submit();
    try {
        commitFuture.checkedGet();
    } catch (TransactionCommitFailedException e) {
        LOG.error("Something wrong happened in DataStore. UserToken "
                + "deletion for token {} from DataStore failed.", token, e);
    }
}

From source file:org.opendaylight.aaa.authn.mdsal.store.AuthNStore.java

private boolean deleteClaims(String token) {
    final InstanceIdentifier<Claims> claims_iid = AuthNStoreUtil.createInstIdentifierForTokencache(token);
    boolean result = false;
    WriteTransaction tx = broker.newWriteOnlyTransaction();
    tx.delete(LogicalDatastoreType.OPERATIONAL, claims_iid);
    CheckedFuture<Void, TransactionCommitFailedException> commitFuture = tx.submit();

    try {//from w  w  w  .ja  va2 s  . c o m
        commitFuture.checkedGet();
        result = true;
    } catch (TransactionCommitFailedException e) {
        LOG.error("Something wrong happened in DataStore. Claim "
                + "deletion for token {} from DataStore failed.", token, e);
    }
    return result;
}

From source file:org.opendaylight.netvirt.openstack.netvirt.translator.crud.impl.AbstractNeutronInterface.java

protected <B extends org.opendaylight.yangtools.yang.binding.DataObject> B readMd(InstanceIdentifier<B> path) {
    B result = null;/*from  w w w.  j av a2  s . com*/
    final ReadOnlyTransaction transaction = getDataBroker().newReadOnlyTransaction();
    CheckedFuture<Optional<B>, ReadFailedException> future = transaction
            .read(LogicalDatastoreType.CONFIGURATION, path);
    if (future != null) {
        try {
            result = future.checkedGet().orNull();
        } catch (ReadFailedException e) {
            LOG.warn("Failed to read {}", path, e);
        }
    }
    transaction.close();
    return result;
}

From source file:org.opendaylight.netconf.messagebus.eventsources.netconf.NetconfEventSourceMount.java

/**
 * Returns list of streams avaliable on device
 * @return list of streams/*from   www  . j av a 2  s  .com*/
 * @throws ReadFailedException if data read fails
 */
List<Stream> getAvailableStreams() throws ReadFailedException {
    DOMDataReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
    CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> checkFeature = tx
            .read(LogicalDatastoreType.OPERATIONAL, STREAMS_PATH);
    Optional<NormalizedNode<?, ?>> streams = checkFeature.checkedGet();
    if (streams.isPresent()) {
        Streams s = (Streams) CODEC_REGISTRY.fromNormalizedNode(STREAMS_PATH, streams.get()).getValue();
        return s.getStream();
    }
    return Collections.emptyList();
}

From source file:org.opendaylight.hello.impl.HelloProvider.java

private String readFromGreetingRegistry(HelloWorldInput input) {
    String result = "Hello " + input.getName();
    ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
    InstanceIdentifier<GreetingRegistryEntry> iid = toInstanceIdentifier(input);
    CheckedFuture<Optional<GreetingRegistryEntry>, ReadFailedException> future = transaction
            .read(LogicalDatastoreType.CONFIGURATION, iid);
    Optional<GreetingRegistryEntry> optional = Optional.absent();
    try {/*  w w  w.  ja v  a2  s. c  om*/
        optional = future.checkedGet();
    } catch (ReadFailedException e) {
        LOG.warn("Reading greeting failed:", e);
    }
    if (optional.isPresent()) {
        result = optional.get().getGreeting();
    }
    return result;
}

From source file:odl.example.impl.ApplicationRegistryUtils.java

public AddApplicationInput readFromApplicationRegistry(int appId) {
    LOG.info("Reading from application registry for appID {}.", appId);
    AddApplicationInput application = null;
    ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
    InstanceIdentifier<ApplicationRegistryEntry> iid = toInstanceIdentifier(appId);
    CheckedFuture<Optional<ApplicationRegistryEntry>, ReadFailedException> future = transaction
            .read(LogicalDatastoreType.OPERATIONAL, iid);
    Optional<ApplicationRegistryEntry> optional = Optional.absent();
    try {//  w w  w. j  av  a  2  s .co  m
        optional = future.checkedGet();
    } catch (ReadFailedException e) {
        LOG.error("Reading application failed:", e);
    }
    if (optional.isPresent()) {
        application = new AddApplicationInputBuilder().setAppId(optional.get().getAppId())
                .setJitter(optional.get().getJitter()).setPacketLoss(optional.get().getPacketLoss())
                .setPacketDelay(optional.get().getPacketDelay()).setBandwidth(optional.get().getBandwidth())
                .build();
    }
    return application;
}