Eliminate BindingToNormalizedNodeCodecFactory.getOrCreateInstance()
[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         final DOMDataReadOnlyTransaction delegateTx = delegate.newReadOnlyTransaction();
57         return new BindingDOMReadTransactionAdapter(delegateTx, codec);
58     }
59
60     @Override
61     public ReadWriteTransaction newReadWriteTransaction() {
62         final DOMDataReadWriteTransaction delegateTx = delegate.newReadWriteTransaction();
63         return new BindingDOMReadWriteTransactionAdapter(delegateTx, codec) {
64
65             @Override
66             public CheckedFuture<Void, TransactionCommitFailedException> submit() {
67                 return listenForFailure(this,super.submit());
68             }
69
70         };
71     }
72
73     @Override
74     public WriteTransaction newWriteOnlyTransaction() {
75         final DOMDataWriteTransaction delegateTx = delegate.newWriteOnlyTransaction();
76         return new BindingDOMWriteTransactionAdapter<DOMDataWriteTransaction>(delegateTx, codec) {
77
78             @Override
79             public CheckedFuture<Void,TransactionCommitFailedException> submit() {
80                 return listenForFailure(this,super.submit());
81             }
82
83         };
84     }
85
86     private CheckedFuture<Void, TransactionCommitFailedException> listenForFailure(
87             final WriteTransaction tx, final CheckedFuture<Void, TransactionCommitFailedException> future) {
88         Futures.addCallback(future, new FutureCallback<Void>() {
89             @Override
90             public void onFailure(final Throwable t) {
91                 failTransactionChain(tx,t);
92             }
93
94             @Override
95             public void onSuccess(final Void result) {
96                 // Intentionally NOOP
97             }
98         });
99
100         return future;
101     }
102
103     private void failTransactionChain(final WriteTransaction tx, final Throwable t) {
104         /*
105          *  We asume correct state change for underlaying transaction
106          *
107          * chain, so we are not changing any of our internal state
108          * to mark that we failed.
109          */
110         this.bindingListener.onTransactionChainFailed(this, tx, t);
111     }
112
113     @Override
114     public void close() {
115         delegate.close();
116     }
117
118     private final class DelegateChainListener implements TransactionChainListener {
119
120         @Override
121         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
122                 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
123             Preconditions.checkState(delegate.equals(chain),
124                     "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
125             /*
126              * Intentionally NOOP, callback for failure, since we
127              * are also listening on each transaction future for failure,
128              * in order to have reference to Binding Transaction (which was seen by client
129              * of this transaction chain), instead of DOM transaction
130              * which is known only to this chain, binding transaction implementation
131              * and underlying transaction chain.
132              *
133              */
134             LOG.debug("Transaction chain {} failed. Failed DOM Transaction {}",this,transaction,cause);
135         }
136
137         @Override
138         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
139             Preconditions.checkState(delegate.equals(chain),
140                     "Illegal state - listener for %s was invoked for incorrect chain %s.", delegate, chain);
141             bindingListener.onTransactionChainSuccessful(BindingDOMTransactionChainAdapter.this);
142         }
143     }
144
145 }