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