Bump versions by x.y.(z+1)
[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 javax.annotation.Nonnull;
16 import org.opendaylight.mdsal.binding.javav2.api.BindingTransactionChain;
17 import org.opendaylight.mdsal.binding.javav2.api.ReadTransaction;
18 import org.opendaylight.mdsal.binding.javav2.api.WriteTransaction;
19 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingToNormalizedNodeCodec;
20 import org.opendaylight.mdsal.common.api.AsyncTransaction;
21 import org.opendaylight.mdsal.common.api.TransactionChain;
22 import org.opendaylight.mdsal.common.api.TransactionChainListener;
23 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
28 import org.opendaylight.yangtools.concepts.Delegator;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Transaction chain adapter.
34  */
35 @Beta
36 public final class BindingDOMTransactionChainAdapter
37         implements BindingTransactionChain, Delegator<DOMTransactionChain> {
38
39     private static final Logger LOG = LoggerFactory.getLogger(BindingDOMTransactionChainAdapter.class);
40
41     private final DOMTransactionChain delegate;
42     private final BindingToNormalizedNodeCodec codec;
43     private final TransactionChainListener bindingListener;
44
45     public BindingDOMTransactionChainAdapter(final DOMDataBroker chainFactory, final BindingToNormalizedNodeCodec codec,
46             final TransactionChainListener listener) {
47         Preconditions.checkNotNull(chainFactory, "DOM Transaction chain factory must not be null");
48         this.bindingListener = listener;
49         this.delegate = chainFactory.createTransactionChain(new DelegateChainListener());
50         this.codec = codec;
51     }
52
53     @Override
54     public DOMTransactionChain getDelegate() {
55         return delegate;
56     }
57
58     @Override
59     public ReadTransaction newReadOnlyTransaction() {
60         final DOMDataTreeReadTransaction delegateTx = delegate.newReadOnlyTransaction();
61         return new BindingDOMReadTransactionAdapter(delegateTx, codec);
62     }
63
64     @Override
65     public WriteTransaction newWriteOnlyTransaction() {
66         final DOMDataTreeWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
67         return new BindingDOMWriteTransactionAdapter<DOMDataTreeWriteTransaction>(delegateTx, codec) {
68
69             @Override
70             public CheckedFuture<Void, TransactionCommitFailedException> submit() {
71                 return listenForFailure(this, super.submit());
72             }
73
74         };
75     }
76
77     private CheckedFuture<Void, TransactionCommitFailedException> listenForFailure(final WriteTransaction tx,
78             final CheckedFuture<Void, TransactionCommitFailedException> future) {
79         Futures.addCallback(future, new FutureCallback<Void>() {
80             @Override
81             public void onFailure(@Nonnull final Throwable throwable) {
82                 failTransactionChain(tx, throwable);
83             }
84
85             @Override
86             public void onSuccess(final Void result) {
87                 // Intentionally NOOP
88             }
89         });
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 }