Proxy MD-SAL interfaces in DOMMountPointServiceImpl
[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 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
52         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
53     @SuppressWarnings("rawtypes")
54     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction,
55             AbstractDOMForwardedTransactionFactory>
56             IMPL_UPDATER = AtomicReferenceFieldUpdater
57             .newUpdater(DOMForwardedWriteTransaction.class, AbstractDOMForwardedTransactionFactory.class, "commitImpl");
58     @SuppressWarnings("rawtypes")
59     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, Future> FUTURE_UPDATER
60             = AtomicReferenceFieldUpdater.newUpdater(DOMForwardedWriteTransaction.class, Future.class, "commitFuture");
61     private static final Logger LOG = LoggerFactory.getLogger(DOMForwardedWriteTransaction.class);
62     private static final Future<?> CANCELLED_FUTURE = Futures.immediateCancelledFuture();
63
64     /**
65      * Implementation of real commit. It also acts as an indication that
66      * the transaction is running -- which we flip atomically using
67      * {@link #IMPL_UPDATER}.
68      */
69     private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
70
71     /**
72      * Future task of transaction commit. It starts off as null, but is
73      * set appropriately on {@link #commit()} and {@link #cancel()} via
74      * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
75      *
76      * <p>
77      * Lazy set is safe for use because it is only referenced to in the
78      * {@link #cancel()} slow path, where we will busy-wait for it. The
79      * fast path gets the benefit of a store-store barrier instead of the
80      * usual store-load barrier.
81      */
82     private volatile Future<?> commitFuture;
83
84     protected DOMForwardedWriteTransaction(final Object identifier, final Map<LogicalDatastoreType, T> backingTxs,
85                                            final AbstractDOMForwardedTransactionFactory<?> commitImpl) {
86         super(identifier, backingTxs);
87         this.commitImpl = Preconditions.checkNotNull(commitImpl, "commitImpl must not be null.");
88     }
89
90     @Override
91     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
92                     final NormalizedNode<?, ?> data) {
93         checkRunning(commitImpl);
94         getSubtransaction(store).write(path, data);
95     }
96
97     @Override
98     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
99         checkRunning(commitImpl);
100         getSubtransaction(store).delete(path);
101     }
102
103     @Override
104     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
105                       final NormalizedNode<?, ?> data) {
106         checkRunning(commitImpl);
107         getSubtransaction(store).merge(path, data);
108     }
109
110     @Override
111     public boolean cancel() {
112         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
113         if (impl != null) {
114             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
115             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
116             closeSubtransactions();
117             return true;
118         }
119
120         // The transaction is in process of being submitted or cancelled. Busy-wait
121         // for the corresponding future.
122         Future<?> future;
123         do {
124             future = commitFuture;
125         } while (future == null);
126
127         return future.cancel(false);
128     }
129
130     @Override
131     public FluentFuture<? extends CommitInfo> commit() {
132         return FluentFuture.from(doCommit(CommitInfo::empty));
133     }
134
135     @SuppressWarnings("checkstyle:IllegalCatch")
136     private <V> ListenableFuture<V> doCommit(Supplier<V> futureValueSupplier) {
137         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
138         checkRunning(impl);
139
140         final Collection<T> txns = getSubtransactions();
141         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
142
143         ListenableFuture<V> ret;
144         try {
145             for (DOMStoreWriteTransaction txn : txns) {
146                 cohorts.add(txn.ready());
147             }
148
149             ret = impl.commit(this, cohorts, futureValueSupplier);
150         } catch (RuntimeException e) {
151             ret = FluentFuture.from(Futures.immediateFailedFuture(
152                     TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e)));
153         }
154         FUTURE_UPDATER.lazySet(this, ret);
155         return ret;
156     }
157
158     private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
159         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
160     }
161 }