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 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.MoreExecutors;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
18 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
29 import org.opendaylight.mdsal.common.api.CommitInfo;
30 import org.opendaylight.yangtools.concepts.Delegator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 @Deprecated(forRemoval = true)
35 final class BindingDOMTransactionChainAdapter implements BindingTransactionChain, Delegator<DOMTransactionChain> {
37 private static final Logger LOG = LoggerFactory.getLogger(BindingDOMTransactionChainAdapter.class);
39 private final DOMTransactionChain delegate;
40 private final BindingToNormalizedNodeCodec codec;
41 private final DelegateChainListener domListener;
42 private final TransactionChainListener bindingListener;
44 BindingDOMTransactionChainAdapter(final DOMDataBroker chainFactory,
45 final BindingToNormalizedNodeCodec codec, final TransactionChainListener listener) {
46 this.domListener = new DelegateChainListener();
47 this.bindingListener = listener;
48 this.delegate = requireNonNull(chainFactory, "DOM Transaction chain factory must not be null")
49 .createTransactionChain(domListener);
54 public DOMTransactionChain getDelegate() {
59 public ReadOnlyTransaction newReadOnlyTransaction() {
60 final DOMDataReadOnlyTransaction delegateTx = delegate.newReadOnlyTransaction();
61 return new BindingDOMReadTransactionAdapter(delegateTx, codec);
65 public ReadWriteTransaction newReadWriteTransaction() {
66 final DOMDataReadWriteTransaction delegateTx = delegate.newReadWriteTransaction();
67 return new BindingDOMReadWriteTransactionAdapter(delegateTx, codec) {
70 public FluentFuture<? extends CommitInfo> commit() {
71 return listenForFailure(this, super.commit());
78 public WriteTransaction newWriteOnlyTransaction() {
79 final DOMDataWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
80 return new BindingDOMWriteTransactionAdapter<DOMDataWriteTransaction>(delegateTx, codec) {
83 public FluentFuture<? extends CommitInfo> commit() {
84 return listenForFailure(this, super.commit());
90 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
91 justification = "https://github.com/spotbugs/spotbugs/issues/811")
92 private FluentFuture<? extends CommitInfo> listenForFailure(
93 final WriteTransaction tx, final FluentFuture<? extends CommitInfo> future) {
94 future.addCallback(new FutureCallback<CommitInfo>() {
96 public void onFailure(final Throwable ex) {
97 failTransactionChain(tx,ex);
101 public void onSuccess(final CommitInfo result) {
102 // Intentionally NOOP
104 }, MoreExecutors.directExecutor());
109 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
110 justification = "https://github.com/spotbugs/spotbugs/issues/811")
111 private void failTransactionChain(final WriteTransaction tx, final Throwable ex) {
113 * We asume correct state change for underlaying transaction
115 * chain, so we are not changing any of our internal state
116 * to mark that we failed.
118 this.bindingListener.onTransactionChainFailed(this, tx, ex);
122 public void close() {
126 private final class DelegateChainListener implements TransactionChainListener {
128 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
129 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
132 * Intentionally NOOP, callback for failure, since we
133 * are also listening on each transaction future for failure,
134 * in order to have reference to Binding Transaction (which was seen by client
135 * of this transaction chain), instead of DOM transaction
136 * which is known only to this chain, binding transaction implementation
137 * and underlying transaction chain.
139 LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
143 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
145 bindingListener.onTransactionChainSuccessful(BindingDOMTransactionChainAdapter.this);
148 private void checkChain(final TransactionChain<?, ?> chain) {
149 checkState(delegate.equals(chain), "Illegal state - listener for %s was invoked for incorrect chain %s.",