2 * Copyright (c) 2014 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.binding.dom.adapter;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.util.concurrent.FluentFuture;
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 java.util.function.Function;
19 import java.util.function.Supplier;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.mdsal.binding.api.ReadTransaction;
22 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.mdsal.binding.api.TransactionChain;
24 import org.opendaylight.mdsal.binding.api.TransactionChainClosedException;
25 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeTransaction;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
31 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
32 import org.opendaylight.mdsal.dom.api.DOMTransactionChainClosedException;
33 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
34 import org.opendaylight.yangtools.concepts.Delegator;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 final class BindingDOMTransactionChainAdapter implements TransactionChain, Delegator<DOMTransactionChain> {
40 private static final Logger LOG = LoggerFactory.getLogger(BindingDOMTransactionChainAdapter.class);
42 private final DOMTransactionChain delegate;
43 private final BindingToNormalizedNodeCodec codec;
44 private final DelegateChainListener domListener;
45 private final TransactionChainListener bindingListener;
47 BindingDOMTransactionChainAdapter(final Function<DOMTransactionChainListener, DOMTransactionChain> chainFactory,
48 final BindingToNormalizedNodeCodec codec, final TransactionChainListener listener) {
49 requireNonNull(chainFactory, "DOM Transaction chain factory must not be null");
50 this.domListener = new DelegateChainListener();
51 this.bindingListener = listener;
52 this.delegate = chainFactory.apply(domListener);
57 public DOMTransactionChain getDelegate() {
62 public ReadTransaction newReadOnlyTransaction() {
63 return new BindingDOMReadTransactionAdapter(createTransaction(delegate::newReadOnlyTransaction), codec);
67 public WriteTransaction newWriteOnlyTransaction() {
68 final DOMDataTreeWriteTransaction delegateTx = createTransaction(delegate::newWriteOnlyTransaction);
69 return new BindingDOMWriteTransactionAdapter<DOMDataTreeWriteTransaction>(delegateTx, codec) {
71 public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
72 return listenForFailure(this, super.commit());
78 public ReadWriteTransaction newReadWriteTransaction() {
79 final DOMDataTreeReadWriteTransaction delegateTx = createTransaction(delegate::newReadWriteTransaction);
80 return new BindingDOMReadWriteTransactionAdapter(delegateTx, codec) {
82 public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
83 return listenForFailure(this, super.commit());
88 private <T, F extends ListenableFuture<T>> F listenForFailure(final WriteTransaction tx, final F future) {
89 Futures.addCallback(future, new FutureCallback<T>() {
91 public void onFailure(final Throwable throwable) {
92 failTransactionChain(tx, throwable);
96 public void onSuccess(final T result) {
99 }, MoreExecutors.directExecutor());
105 private void failTransactionChain(final WriteTransaction tx, final Throwable throwable) {
107 * We asume correct state change for underlaying transaction
109 * chain, so we are not changing any of our internal state
110 * to mark that we failed.
112 this.bindingListener.onTransactionChainFailed(this, tx, throwable);
116 public void close() {
120 private static <T> T createTransaction(final Supplier<T> supplier) {
122 return supplier.get();
123 } catch (DOMTransactionChainClosedException e) {
124 throw new TransactionChainClosedException("Transaction chain already closed", e);
128 private final class DelegateChainListener implements DOMTransactionChainListener {
130 public void onTransactionChainFailed(final DOMTransactionChain chain, final DOMDataTreeTransaction transaction,
131 final Throwable cause) {
132 checkState(delegate.equals(chain), "Listener for %s was invoked for incorrect chain %s.", delegate, chain);
134 * Intentionally NOOP, callback for failure, since we
135 * are also listening on each transaction future for failure,
136 * in order to have reference to Binding Transaction (which was seen by client
137 * of this transaction chain), instead of DOM transaction
138 * which is known only to this chain, binding transaction implementation
139 * and underlying transaction chain.
142 LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
146 public void onTransactionChainSuccessful(final DOMTransactionChain chain) {
147 checkState(delegate.equals(chain), "Listener for %s was invoked for incorrect chain %s.", delegate, chain);
148 bindingListener.onTransactionChainSuccessful(BindingDOMTransactionChainAdapter.this);