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