Default AsyncWriteTransaction#submit()
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMTransactionChainAdapter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FluentFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.api.BindingTransactionChain;
18 import org.opendaylight.mdsal.binding.api.ReadTransaction;
19 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.mdsal.binding.api.WriteTransaction;
21 import org.opendaylight.mdsal.common.api.AsyncTransaction;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.common.api.TransactionChain;
24 import org.opendaylight.mdsal.common.api.TransactionChainListener;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
30 import org.opendaylight.yangtools.concepts.Delegator;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 final class BindingDOMTransactionChainAdapter implements BindingTransactionChain, Delegator<DOMTransactionChain> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(BindingDOMTransactionChainAdapter.class);
37
38     private final DOMTransactionChain delegate;
39     private final BindingToNormalizedNodeCodec codec;
40     private final DelegateChainListener domListener;
41     private final TransactionChainListener bindingListener;
42
43     BindingDOMTransactionChainAdapter(final DOMDataBroker chainFactory,
44             final BindingToNormalizedNodeCodec codec, final TransactionChainListener listener) {
45         Preconditions.checkNotNull(chainFactory, "DOM Transaction chain factory must not be null");
46         this.domListener = new DelegateChainListener();
47         this.bindingListener = listener;
48         this.delegate = chainFactory.createTransactionChain(domListener);
49         this.codec = codec;
50     }
51
52     @Override
53     public DOMTransactionChain getDelegate() {
54         return delegate;
55     }
56
57     @Override
58     public ReadTransaction newReadOnlyTransaction() {
59         final DOMDataTreeReadTransaction delegateTx = delegate.newReadOnlyTransaction();
60         return new BindingDOMReadTransactionAdapter(delegateTx, codec);
61     }
62
63     @Override
64     public WriteTransaction newWriteOnlyTransaction() {
65         final DOMDataTreeWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
66         return new BindingDOMWriteTransactionAdapter<DOMDataTreeWriteTransaction>(delegateTx, codec) {
67
68             @Override
69             public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
70                 return listenForFailure(this, super.commit());
71             }
72
73         };
74     }
75
76     @Override
77     public ReadWriteTransaction newReadWriteTransaction() {
78         final DOMDataTreeReadWriteTransaction delegateTx = delegate.newReadWriteTransaction();
79         return new BindingDOMReadWriteTransactionAdapter(delegateTx, codec) {
80
81             @Override
82             public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
83                 return listenForFailure(this, super.commit());
84             }
85         };
86     }
87
88     private <T, F extends ListenableFuture<T>> F listenForFailure(final WriteTransaction tx, final F future) {
89         Futures.addCallback(future, new FutureCallback<T>() {
90             @Override
91             public void onFailure(final Throwable throwable) {
92                 failTransactionChain(tx, throwable);
93             }
94
95             @Override
96             public void onSuccess(final T result) {
97                 // Intentionally NOOP
98             }
99         }, MoreExecutors.directExecutor());
100
101         return future;
102     }
103
104
105     private void failTransactionChain(final WriteTransaction tx, final Throwable throwable) {
106         /*
107          *  We asume correct state change for underlaying transaction
108          *
109          * chain, so we are not changing any of our internal state
110          * to mark that we failed.
111          */
112         this.bindingListener.onTransactionChainFailed(this, tx, throwable);
113     }
114
115     @Override
116     public void close() {
117         delegate.close();
118     }
119
120     private final class DelegateChainListener implements TransactionChainListener {
121
122         @Override
123         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
124                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
125             Preconditions.checkState(delegate.equals(chain),
126                     "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
127             /*
128              * Intentionally NOOP, callback for failure, since we
129              * are also listening on each transaction future for failure,
130              * in order to have reference to Binding Transaction (which was seen by client
131              * of this transaction chain), instead of DOM transaction
132              * which is known only to this chain, binding transaction implementation
133              * and underlying transaction chain.
134              *
135              */
136             LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
137         }
138
139         @Override
140         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
141             Preconditions.checkState(delegate.equals(chain),
142                     "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
143             bindingListener.onTransactionChainSuccessful(BindingDOMTransactionChainAdapter.this);
144         }
145     }
146 }