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