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