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