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 com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import org.opendaylight.mdsal.binding.api.BindingTransactionChain;
15 import org.opendaylight.mdsal.binding.api.ReadTransaction;
16 import org.opendaylight.mdsal.binding.api.WriteTransaction;
17 import org.opendaylight.mdsal.common.api.AsyncTransaction;
18 import org.opendaylight.mdsal.common.api.TransactionChain;
19 import org.opendaylight.mdsal.common.api.TransactionChainListener;
20 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
21 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
25 import org.opendaylight.yangtools.concepts.Delegator;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 final class BindingDOMTransactionChainAdapter implements BindingTransactionChain, Delegator<DOMTransactionChain> {
31 private static final Logger LOG = LoggerFactory.getLogger(BindingDOMTransactionChainAdapter.class);
33 private final DOMTransactionChain delegate;
34 private final BindingToNormalizedNodeCodec codec;
35 private final DelegateChainListener domListener;
36 private final TransactionChainListener bindingListener;
38 BindingDOMTransactionChainAdapter(final DOMDataBroker chainFactory,
39 final BindingToNormalizedNodeCodec codec, final TransactionChainListener listener) {
40 Preconditions.checkNotNull(chainFactory, "DOM Transaction chain factory must not be null");
41 this.domListener = new DelegateChainListener();
42 this.bindingListener = listener;
43 this.delegate = chainFactory.createTransactionChain(domListener);
48 public DOMTransactionChain getDelegate() {
53 public ReadTransaction newReadOnlyTransaction() {
54 final DOMDataTreeReadTransaction delegateTx = delegate.newReadOnlyTransaction();
55 return new BindingDOMReadTransactionAdapter(delegateTx, codec);
59 public WriteTransaction newWriteOnlyTransaction() {
60 final DOMDataTreeWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
61 return new BindingDOMWriteTransactionAdapter<DOMDataTreeWriteTransaction>(delegateTx, codec) {
64 public CheckedFuture<Void,TransactionCommitFailedException> submit() {
65 return listenForFailure(this,super.submit());
71 private CheckedFuture<Void, TransactionCommitFailedException> listenForFailure(
72 final WriteTransaction tx, final CheckedFuture<Void, TransactionCommitFailedException> future) {
73 Futures.addCallback(future, new FutureCallback<Void>() {
75 public void onFailure(final Throwable throwable) {
76 failTransactionChain(tx, throwable);
80 public void onSuccess(final Void result) {
88 private void failTransactionChain(final WriteTransaction tx, final Throwable throwable) {
90 * We asume correct state change for underlaying transaction
92 * chain, so we are not changing any of our internal state
93 * to mark that we failed.
95 this.bindingListener.onTransactionChainFailed(this, tx, throwable);
103 private final class DelegateChainListener implements TransactionChainListener {
106 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
107 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
108 Preconditions.checkState(delegate.equals(chain),
109 "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
111 * Intentionally NOOP, callback for failure, since we
112 * are also listening on each transaction future for failure,
113 * in order to have reference to Binding Transaction (which was seen by client
114 * of this transaction chain), instead of DOM transaction
115 * which is known only to this chain, binding transaction implementation
116 * and underlying transaction chain.
119 LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
123 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
124 Preconditions.checkState(delegate.equals(chain),
125 "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
126 bindingListener.onTransactionChainSuccessful(BindingDOMTransactionChainAdapter.this);