Deprecate all MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMForwardedWriteTransaction.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.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Supplier;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Read-Write Transaction, which is composed of several
32  * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
33  * {@link LogicalDatastoreType} type parameter in:
34  *
35  * <p>
36  * <ul>
37  * <li>{@link #put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
38  * <li>{@link #delete(LogicalDatastoreType, YangInstanceIdentifier)}
39  * <li>{@link #merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
40  * </ul>
41  *
42  * <p>
43  * {@link #commit()} will result in invocation of
44  * {@link DOMDataCommitImplementation#submit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction,
45  * Iterable)} invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort}
46  * for underlying transactions.
47  *
48  * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as
49  *            subtransaction.
50  */
51 @Deprecated
52 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
53         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
54     @SuppressWarnings("rawtypes")
55     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
56             AbstractDOMForwardedTransactionFactory>
57             IMPL_UPDATER = AtomicReferenceFieldUpdater
58             .newUpdater(DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
59     @SuppressWarnings("rawtypes")
60     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER
61             = AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
62     private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
63     private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
64
65     /**
66      * Implementation of real commit. It also acts as an indication that
67      * the transaction is running -- which we flip atomically using
68      * {@link #IMPL_UPDATER}.
69      */
70     private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
71
72     /**
73      * Future task of transaction commit. It starts off as null, but is
74      * set appropriately on {@link #commit()} and {@link #cancel()} via
75      * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
76      *
77      * <p>
78      * Lazy set is safe for use because it is only referenced to in the
79      * {@link #cancel()} slow path, where we will busy-wait for it. The
80      * fast path gets the benefit of a store-store barrier instead of the
81      * usual store-load barrier.
82      */
83     private volatile Future<?> commitFuture;
84
85     protected DOMForwardedWriteTransaction(final Object identifier, final Map<LogicalDatastoreType, T> backingTxs,
86                                            final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
87         super(identifier, backingTxs);
88         this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
89     }
90
91     @Override
92     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
93                     final NormalizedNode<?, ?> data) {
94         checkRunning(commitImpl);
95         getSubtransaction(store).write(path, data);
96     }
97
98     @Override
99     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
100         checkRunning(commitImpl);
101         getSubtransaction(store).delete(path);
102     }
103
104     @Override
105     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
106                       final NormalizedNode<?, ?> data) {
107         checkRunning(commitImpl);
108         getSubtransaction(store).merge(path, data);
109     }
110
111     @Override
112     public boolean cancel() {
113         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
114         if (impl != null) {
115             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
116             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
117             closeSubtransactions();
118             return true;
119         }
120
121         // The transaction is in process of being submitted or cancelled. Busy-wait
122         // for the corresponding future.
123         Future<?> future;
124         do {
125             future = commitFuture;
126         } while (future == null);
127
128         return future.cancel(false);
129     }
130
131     @Override
132     public FluentFuture<? extends CommitInfo> commit() {
133         return FluentFuture.from(doCommit(CommitInfo::empty));
134     }
135
136     @SuppressWarnings("checkstyle:IllegalCatch")
137     private <V> ListenableFuture<V> doCommit(Supplier<V> futureValueSupplier) {
138         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
139         checkRunning(impl);
140
141         final Collection<T> txns = getSubtransactions();
142         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
143
144         ListenableFuture<V> ret;
145         try {
146             for (DOMStoreWriteTransaction txn : txns) {
147                 cohorts.add(txn.ready());
148             }
149
150             ret = impl.commit(this, cohorts, futureValueSupplier);
151         } catch (RuntimeException e) {
152             ret = FluentFuture.from(Futures.immediateFailedFuture(
153                     TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e)));
154         }
155         FUTURE_UPDATER.lazySet(this, ret);
156         return ret;
157     }
158
159     private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
160         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
161     }
162 }