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 BindingTranslatedTransactionChain implements BindingTransactionChain, Delegator<DOMTransactionChain> {
33 private static final Logger LOG = LoggerFactory.getLogger(BindingTranslatedTransactionChain.class);
35 private final DOMTransactionChain delegate;
36 private final BindingToNormalizedNodeCodec codec;
37 private final DelegateChainListener domListener;
38 private final TransactionChainListener bindingListener;
40 public BindingTranslatedTransactionChain(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 DOMDataReadOnlyTransaction delegateTx = delegate.newReadOnlyTransaction();
57 ReadOnlyTransaction bindingTx = new BindingDataReadTransactionImpl(delegateTx, codec);
62 public ReadWriteTransaction newReadWriteTransaction() {
63 DOMDataReadWriteTransaction delegateTx = delegate.newReadWriteTransaction();
64 ReadWriteTransaction bindingTx = new BindingDataReadWriteTransactionImpl(delegateTx, codec) {
67 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
68 return listenForFailure(this,super.submit());
76 public WriteTransaction newWriteOnlyTransaction() {
77 final DOMDataWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
78 WriteTransaction bindingTx = new BindingDataWriteTransactionImpl<DOMDataWriteTransaction>(delegateTx, codec) {
81 public CheckedFuture<Void,TransactionCommitFailedException> submit() {
82 return listenForFailure(this,super.submit());
89 private CheckedFuture<Void, TransactionCommitFailedException> listenForFailure(
90 final WriteTransaction tx, CheckedFuture<Void, TransactionCommitFailedException> future) {
91 Futures.addCallback(future, new FutureCallback<Void>() {
93 public void onFailure(Throwable t) {
94 failTransactionChain(tx,t);
98 public void onSuccess(Void result) {
106 private void failTransactionChain(WriteTransaction tx, Throwable t) {
108 * We asume correct state change for underlaying transaction
110 * chain, so we are not changing any of our internal state
111 * to mark that we failed.
113 this.bindingListener.onTransactionChainFailed(this, tx, t);
117 public void close() {
121 private final class DelegateChainListener implements TransactionChainListener {
124 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
125 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
126 Preconditions.checkState(delegate.equals(chain),
127 "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
129 * Intentionally NOOP, callback for failure, since we
130 * are also listening on each transaction future for failure,
131 * in order to have reference to Binding Transaction (which was seen by client
132 * of this transaction chain), instead of DOM transaction
133 * which is known only to this chain, binding transaction implementation
134 * and underlying transaction chain.
137 LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
141 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
142 Preconditions.checkState(delegate.equals(chain),
143 "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
144 bindingListener.onTransactionChainSuccessful(BindingTranslatedTransactionChain.this);