Cleaned up sal-dom-* packages and removed legacy interfaces
[mdsal.git] / dom / mdsal-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.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Read-Write Transaction, which is composed of several
33  * {@link DOMStoreWriteTransaction} transactions. A sub-transaction is selected by
34  * {@link LogicalDatastoreType} type parameter in:
35  *
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  * <p>
42  * {@link #commit()} will result in invocation of
43  * {@link DOMDataCommitImplementation#submit(org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction, Iterable)}
44  * invocation with all {@link org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort} for underlying
45  * transactions.
46  *
47  * @param <T> Subtype of {@link DOMStoreWriteTransaction} which is used as
48  *            subtransaction.
49  */
50 class DOMForwardedWriteTransaction<T extends DOMStoreWriteTransaction> extends
51         AbstractDOMForwardedCompositeTransaction<LogicalDatastoreType, T> implements DOMDataWriteTransaction {
52     @SuppressWarnings("rawtypes")
53     private static final AtomicReferenceFieldUpdater<DOMForwardedWriteTransaction, AbstractDOMForwardedTransactionFactory> IMPL_UPDATER =
54             AtomicReferenceFieldUpdater.newUpdater(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
63      * the transaction is running -- which we flip atomically using
64      * {@link #IMPL_UPDATER}.
65      */
66     private volatile AbstractDOMForwardedTransactionFactory<?> commitImpl;
67
68     /**
69      * Future task of transaction commit. It starts off as null, but is
70      * set appropriately on {@link #submit()} and {@link #cancel()} via
71      * {@link AtomicReferenceFieldUpdater#lazySet(Object, Object)}.
72      *
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, final NormalizedNode<?, ?> data) {
88         checkRunning(commitImpl);
89         getSubtransaction(store).write(path, data);
90     }
91
92     @Override
93     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
94         checkRunning(commitImpl);
95         getSubtransaction(store).delete(path);
96     }
97
98     @Override
99     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
100         checkRunning(commitImpl);
101         getSubtransaction(store).merge(path, data);
102     }
103
104     @Override
105     public boolean cancel() {
106         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
107         if (impl != null) {
108             LOG.trace("Transaction {} cancelled before submit", getIdentifier());
109             FUTURE_UPDATER.lazySet(this, CANCELLED_FUTURE);
110             closeSubtransactions();
111             return true;
112         }
113
114         // The transaction is in process of being submitted or cancelled. Busy-wait
115         // for the corresponding future.
116         Future<?> future;
117         do {
118             future = commitFuture;
119         } while (future == null);
120
121         return future.cancel(false);
122     }
123
124     @Deprecated
125     @Override
126     public ListenableFuture<RpcResult<TransactionStatus>> commit() {
127         return CommitCompatibility.convertToLegacyCommitFuture(submit());
128     }
129
130     @Override
131     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
132         final AbstractDOMForwardedTransactionFactory<?> impl = IMPL_UPDATER.getAndSet(this, null);
133         checkRunning(impl);
134
135         final Collection<T> txns = getSubtransactions();
136         final Collection<DOMStoreThreePhaseCommitCohort> cohorts = new ArrayList<>(txns.size());
137
138         // FIXME: deal with errors thrown by backed (ready and submit can fail in theory)
139         for (final DOMStoreWriteTransaction txn : txns) {
140             cohorts.add(txn.ready());
141         }
142
143         final CheckedFuture<Void, TransactionCommitFailedException> ret = impl.submit(this, cohorts);
144         FUTURE_UPDATER.lazySet(this, ret);
145         return ret;
146     }
147
148     private void checkRunning(final AbstractDOMForwardedTransactionFactory<?> impl) {
149         Preconditions.checkState(impl != null, "Transaction %s is no longer running", getIdentifier());
150     }
151 }