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