2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mdsal.dom.broker;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import com.google.common.util.concurrent.SettableFuture;
19 import javax.annotation.Nullable;
20 import org.opendaylight.mdsal.common.api.CommitInfo;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.common.api.ReadFailedException;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 public class TransactionChainReadTransaction implements DOMDataTreeReadTransaction {
29 private final DOMDataTreeReadTransaction delegateReadTx;
30 private final ListenableFuture<? extends CommitInfo> previousWriteTxFuture;
31 private final Object identifier;
32 private final ShardedDOMTransactionChainAdapter txChain;
34 TransactionChainReadTransaction(final Object txIdentifier, final DOMDataTreeReadTransaction delegateReadTx,
35 final ListenableFuture<? extends CommitInfo> previousWriteTxFuture,
36 final ShardedDOMTransactionChainAdapter txChain) {
37 this.delegateReadTx = delegateReadTx;
38 this.previousWriteTxFuture = previousWriteTxFuture;
39 this.identifier = txIdentifier;
40 this.txChain = txChain;
44 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
45 final YangInstanceIdentifier path) {
46 final SettableFuture<Optional<NormalizedNode<?, ?>>> readResult = SettableFuture.create();
48 Futures.addCallback(previousWriteTxFuture, new FutureCallback<CommitInfo>() {
50 public void onSuccess(@Nullable final CommitInfo result) {
51 Futures.addCallback(delegateReadTx.read(store, path),
52 new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
55 public void onSuccess(@Nullable final Optional<NormalizedNode<?, ?>> result) {
56 readResult.set(result);
60 public void onFailure(final Throwable throwable) {
61 txChain.transactionFailed(TransactionChainReadTransaction.this, throwable);
62 readResult.setException(throwable);
64 }, MoreExecutors.directExecutor());
68 public void onFailure(final Throwable throwable) {
69 // we don't have to notify txchain about this failure
70 // failed write transaction should do this
71 readResult.setException(throwable);
73 }, MoreExecutors.directExecutor());
75 return Futures.makeChecked(readResult, ReadFailedException.MAPPER);
79 public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
80 final YangInstanceIdentifier path) {
81 final Function<Optional<NormalizedNode<?, ?>>, Boolean> transform =
82 optionalNode -> optionalNode.isPresent() ? Boolean.TRUE : Boolean.FALSE;
83 final ListenableFuture<Boolean> existsResult = Futures.transform(read(store, path), transform,
84 MoreExecutors.directExecutor());
85 return Futures.makeChecked(existsResult, ReadFailedException.MAPPER);
90 delegateReadTx.close();
91 txChain.closeReadTransaction();
95 public Object getIdentifier() {