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
8 package org.opendaylight.mdsal.dom.broker;
10 import com.google.common.util.concurrent.FluentFuture;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.common.api.CommitInfo;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 public class TransactionChainReadTransaction implements DOMDataTreeReadTransaction {
23 private final DOMDataTreeReadTransaction delegateReadTx;
24 private final FluentFuture<? extends CommitInfo> previousWriteTxFuture;
25 private final Object identifier;
26 private final ShardedDOMTransactionChainAdapter txChain;
28 TransactionChainReadTransaction(final Object txIdentifier, final DOMDataTreeReadTransaction delegateReadTx,
29 final FluentFuture<? extends CommitInfo> previousWriteTxFuture,
30 final ShardedDOMTransactionChainAdapter txChain) {
31 this.delegateReadTx = delegateReadTx;
32 this.previousWriteTxFuture = previousWriteTxFuture;
33 this.identifier = txIdentifier;
34 this.txChain = txChain;
38 public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
39 final YangInstanceIdentifier path) {
40 final SettableFuture<Optional<NormalizedNode<?, ?>>> readResult = SettableFuture.create();
42 previousWriteTxFuture.addCallback(new FutureCallback<CommitInfo>() {
44 public void onSuccess(final CommitInfo result) {
45 delegateReadTx.read(store, path).addCallback(new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
47 public void onSuccess(final Optional<NormalizedNode<?, ?>> result) {
48 readResult.set(result);
52 public void onFailure(final Throwable throwable) {
53 txChain.transactionFailed(TransactionChainReadTransaction.this, throwable);
54 readResult.setException(throwable);
56 }, MoreExecutors.directExecutor());
60 public void onFailure(final Throwable throwable) {
61 // we don't have to notify txchain about this failure
62 // failed write transaction should do this
63 readResult.setException(throwable);
65 }, MoreExecutors.directExecutor());
67 return FluentFuture.from(readResult);
71 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
72 return read(store, path).transform(Optional::isPresent, MoreExecutors.directExecutor());
77 delegateReadTx.close();
78 txChain.closeReadTransaction();
82 public Object getIdentifier() {