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