Fix javadocs and enable doclint
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / AbstractDOMForwardedTransactionFactory.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 java.util.Collection;
13 import java.util.EnumMap;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28
29 /**
30  *
31  * Abstract composite transaction factory.
32  *
33  * Provides an convenience common implementation for composite DOM Transactions,
34  * where subtransaction is identified by {@link LogicalDatastoreType} type and
35  * implementation of subtransaction is provided by
36  * {@link DOMStoreTransactionFactory}.
37  *
38  * <b>Note:</b>This class does not have thread-safe implementation of  {@link #close()},
39  *   implementation may allow accessing and allocating new transactions during closing
40  *   this instance.
41  *
42  * @param <T>
43  *            Type of {@link DOMStoreTransactionFactory} factory.
44  */
45 abstract class AbstractDOMForwardedTransactionFactory<T extends DOMStoreTransactionFactory> implements AutoCloseable {
46     @SuppressWarnings("rawtypes")
47     private static final AtomicIntegerFieldUpdater<AbstractDOMForwardedTransactionFactory> UPDATER =
48             AtomicIntegerFieldUpdater.newUpdater(AbstractDOMForwardedTransactionFactory.class, "closed");
49     private final Map<LogicalDatastoreType, T> storeTxFactories;
50     private volatile int closed = 0;
51
52     protected AbstractDOMForwardedTransactionFactory(final Map<LogicalDatastoreType, ? extends T> txFactories) {
53         this.storeTxFactories = new EnumMap<>(txFactories);
54     }
55
56     /**
57      * Implementations must return unique identifier for each and every call of
58      * this method;
59      *
60      * @return new Unique transaction identifier.
61      */
62     protected abstract Object newTransactionIdentifier();
63
64     /**
65      * User-supplied implementation of {@link DOMDataWriteTransaction#submit()}
66      * for transaction.
67      *
68      * Callback invoked when {@link DOMDataWriteTransaction#submit()} is invoked
69      * on transaction created by this factory.
70      *
71      * @param transaction
72      *            Transaction on which {@link DOMDataWriteTransaction#commit()}
73      *            was invoked.
74      * @param cohorts
75      *            Iteratable of cohorts for subtransactions associated with
76      *            the transaction being committed.
77      * @return a CheckedFuture. if commit coordination on cohorts finished successfully,
78      *         nothing is returned from the Future, On failure,
79      *         the Future fails with a {@link TransactionCommitFailedException}.
80      */
81     protected abstract CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
82             final Collection<DOMStoreThreePhaseCommitCohort> cohorts);
83
84     /**
85      * Creates a new composite read-only transaction
86      *
87      * Creates a new composite read-only transaction backed by one transaction
88      * per factory in {@link #getTxFactories()}.
89      *
90      * Subtransaction for reading is selected by supplied
91      * {@link LogicalDatastoreType} as parameter for
92      * {@link DOMDataReadOnlyTransaction#read(LogicalDatastoreType,org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
93      * .
94      *
95      * Id of returned transaction is retrieved via
96      * {@link #newTransactionIdentifier()}.
97      *
98      * @return New composite read-only transaction.
99      */
100     public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
101         checkNotClosed();
102
103         final Map<LogicalDatastoreType, DOMStoreReadTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
104         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
105             txns.put(store.getKey(), store.getValue().newReadOnlyTransaction());
106         }
107         return new DOMForwardedReadOnlyTransaction(newTransactionIdentifier(), txns);
108     }
109
110     /**
111      * Creates a new composite write-only transaction
112      *
113      * <p>
114      * Creates a new composite write-only transaction backed by one write-only
115      * transaction per factory in {@link #getTxFactories()}.
116      *
117      * <p>
118      * Implementation of composite Write-only transaction is following:
119      *
120      * <ul><li>
121      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
122      * - backing subtransaction is selected by {@link LogicalDatastoreType},
123      * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
124      * is invoked on selected subtransaction.
125      * </li><li>
126      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
127      * - backing subtransaction is selected by {@link LogicalDatastoreType},
128      * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
129      * is invoked on selected subtransaction.
130      * </li><li>
131      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
132      * - backing subtransaction is selected by {@link LogicalDatastoreType},
133      * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is invoked on
134      * selected subtransaction.
135      * </li><li> {@link DOMDataWriteTransaction#commit()} - results in invoking
136      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
137      * and then invoking finalized implementation callback
138      * {@link #submit(DOMDataWriteTransaction, Collection)} with transaction which
139      * was commited and gathered results.
140      * </li>
141      * </ul>
142      *
143      * Id of returned transaction is generated via
144      * {@link #newTransactionIdentifier()}.
145      *
146      * @return New composite write-only transaction associated with this
147      *         factory.
148      */
149     public final DOMDataWriteTransaction newWriteOnlyTransaction() {
150         checkNotClosed();
151
152         final Map<LogicalDatastoreType, DOMStoreWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
153         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
154             txns.put(store.getKey(), store.getValue().newWriteOnlyTransaction());
155         }
156         return new DOMForwardedWriteTransaction<>(newTransactionIdentifier(), txns, this);
157     }
158
159     /**
160      * Creates a new composite write-only transaction
161      *
162      * <p>
163      * Creates a new composite write-only transaction backed by one write-only
164      * transaction per factory in {@link #getTxFactories()}.
165      * <p>
166      * Implementation of composite Write-only transaction is following:
167      *
168      * <ul>
169      * <li>
170      * {@link DOMDataReadTransaction#read(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
171      * - backing subtransaction is selected by {@link LogicalDatastoreType},
172      * {@link DOMStoreReadTransaction#read(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is invoked on
173      * selected subtransaction.
174      * <li>
175      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
176      * - backing subtransaction is selected by {@link LogicalDatastoreType},
177      * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
178      * is invoked on selected subtransaction.
179      * <li>
180      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
181      * - backing subtransaction is selected by {@link LogicalDatastoreType},
182      * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
183      * is invoked on selected subtransaction.
184      * <li>
185      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
186      * - backing subtransaction is selected by {@link LogicalDatastoreType},
187      * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is invoked on
188      * selected subtransaction.
189      * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
190      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
191      * and then invoking finalized implementation callback
192      * {@link #submit(DOMDataWriteTransaction, Collection)} with transaction which
193      * was committed and gathered results.
194      * <li>
195      * </ul>
196      *
197      * Id of returned transaction is generated via
198      * {@link #newTransactionIdentifier()}.
199      *
200      * @return New composite read-write transaction associated with this
201      *         factory.
202      */
203     public final DOMDataReadWriteTransaction newReadWriteTransaction() {
204         checkNotClosed();
205
206         final Map<LogicalDatastoreType, DOMStoreReadWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
207         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
208             txns.put(store.getKey(), store.getValue().newReadWriteTransaction());
209         }
210         return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), txns, this);
211     }
212
213     /**
214      * Convenience accessor of backing factories intended to be used only by
215      * finalization of this class.
216      *
217      * <b>Note:</b>
218      * Finalization of this class may want to access other functionality of
219      * supplied Transaction factories.
220      *
221      * @return Map of backing transaction factories.
222      */
223     protected final Map<LogicalDatastoreType, T> getTxFactories() {
224         return storeTxFactories;
225     }
226
227     /**
228      * Checks if instance is not closed.
229      *
230      * @throws IllegalStateException If instance of this class was closed.
231      *
232      */
233     protected final void checkNotClosed() {
234         Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
235     }
236
237     @Override
238     public void close() {
239         final boolean success = UPDATER.compareAndSet(this, 0, 1);
240         Preconditions.checkState(success, "Transaction factory was already closed");
241     }
242 }
243