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