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