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