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.util.concurrent.FluentFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.Optional;
16 import javax.annotation.Nullable;
17 import org.opendaylight.mdsal.common.api.CommitInfo;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 public class TransactionChainReadTransaction implements DOMDataTreeReadTransaction {
25 private final DOMDataTreeReadTransaction delegateReadTx;
26 private final FluentFuture<? extends CommitInfo> previousWriteTxFuture;
27 private final Object identifier;
28 private final ShardedDOMTransactionChainAdapter txChain;
30 TransactionChainReadTransaction(final Object txIdentifier, final DOMDataTreeReadTransaction delegateReadTx,
31 final FluentFuture<? extends CommitInfo> previousWriteTxFuture,
32 final ShardedDOMTransactionChainAdapter txChain) {
33 this.delegateReadTx = delegateReadTx;
34 this.previousWriteTxFuture = previousWriteTxFuture;
35 this.identifier = txIdentifier;
36 this.txChain = txChain;
40 public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
41 final YangInstanceIdentifier path) {
42 final SettableFuture<Optional<NormalizedNode<?, ?>>> readResult = SettableFuture.create();
44 previousWriteTxFuture.addCallback(new FutureCallback<CommitInfo>() {
46 public void onSuccess(@Nullable final CommitInfo result) {
47 delegateReadTx.read(store, path).addCallback(new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
49 public void onSuccess(@Nullable final Optional<NormalizedNode<?, ?>> result) {
50 readResult.set(result);
54 public void onFailure(final Throwable throwable) {
55 txChain.transactionFailed(TransactionChainReadTransaction.this, throwable);
56 readResult.setException(throwable);
58 }, MoreExecutors.directExecutor());
62 public void onFailure(final Throwable throwable) {
63 // we don't have to notify txchain about this failure
64 // failed write transaction should do this
65 readResult.setException(throwable);
67 }, MoreExecutors.directExecutor());
73 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
74 return read(store, path).transform(Optional::isPresent, MoreExecutors.directExecutor());
79 delegateReadTx.close();
80 txChain.closeReadTransaction();
84 public Object getIdentifier() {