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.controller.md.sal.binding.impl;
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.controller.md.sal.binding.api.BindingTransactionChain;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
26 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
27 import org.opendaylight.yangtools.concepts.Delegator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 final class BindingDOMTransactionChainAdapter implements BindingTransactionChain, Delegator<DOMTransactionChain> {
33 private static final Logger LOG = LoggerFactory.getLogger(BindingDOMTransactionChainAdapter.class);
35 private final DOMTransactionChain delegate;
36 private final BindingToNormalizedNodeCodec codec;
37 private final DelegateChainListener domListener;
38 private final TransactionChainListener bindingListener;
40 public BindingDOMTransactionChainAdapter(final DOMDataBroker chainFactory,
41 final BindingToNormalizedNodeCodec codec, final TransactionChainListener listener) {
42 Preconditions.checkNotNull(chainFactory, "DOM Transaction chain factory must not be null");
43 this.domListener = new DelegateChainListener();
44 this.bindingListener = listener;
45 this.delegate = chainFactory.createTransactionChain(domListener);
50 public DOMTransactionChain getDelegate() {
55 public ReadOnlyTransaction newReadOnlyTransaction() {
56 final DOMDataReadOnlyTransaction delegateTx = delegate.newReadOnlyTransaction();
57 return new BindingDOMReadTransactionAdapter(delegateTx, codec);
61 public ReadWriteTransaction newReadWriteTransaction() {
62 final DOMDataReadWriteTransaction delegateTx = delegate.newReadWriteTransaction();
63 return new BindingDOMReadWriteTransactionAdapter(delegateTx, codec) {
66 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
67 return listenForFailure(this,super.submit());
74 public WriteTransaction newWriteOnlyTransaction() {
75 final DOMDataWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
76 return new BindingDOMWriteTransactionAdapter<DOMDataWriteTransaction>(delegateTx, codec) {
79 public CheckedFuture<Void,TransactionCommitFailedException> submit() {
80 return listenForFailure(this,super.submit());
86 private CheckedFuture<Void, TransactionCommitFailedException> listenForFailure(
87 final WriteTransaction tx, final CheckedFuture<Void, TransactionCommitFailedException> future) {
88 Futures.addCallback(future, new FutureCallback<Void>() {
90 public void onFailure(final Throwable t) {
91 failTransactionChain(tx,t);
95 public void onSuccess(final Void result) {
103 private void failTransactionChain(final WriteTransaction tx, final Throwable t) {
105 * We asume correct state change for underlaying transaction
107 * chain, so we are not changing any of our internal state
108 * to mark that we failed.
110 this.bindingListener.onTransactionChainFailed(this, tx, t);
114 public void close() {
118 private final class DelegateChainListener implements TransactionChainListener {
121 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
122 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
123 Preconditions.checkState(delegate.equals(chain),
124 "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
126 * Intentionally NOOP, callback for failure, since we
127 * are also listening on each transaction future for failure,
128 * in order to have reference to Binding Transaction (which was seen by client
129 * of this transaction chain), instead of DOM transaction
130 * which is known only to this chain, binding transaction implementation
131 * and underlying transaction chain.
134 LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
138 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
139 Preconditions.checkState(delegate.equals(chain),
140 "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
141 bindingListener.onTransactionChainSuccessful(BindingDOMTransactionChainAdapter.this);