de2c36605517aa7f7165a648021a6285bda80d78
[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.base.Supplier;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collection;
14 import java.util.EnumMap;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
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 import org.opendaylight.yangtools.concepts.Path;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31
32 /**
33  * Abstract composite transaction factory.
34  *
35  * <p>
36  * Provides an convenience common implementation for composite DOM Transactions,
37  * where subtransaction is identified by {@link LogicalDatastoreType} type and
38  * implementation of subtransaction is provided by
39  * {@link DOMStoreTransactionFactory}.
40  *
41  * <b>Note:</b>This class does not have thread-safe implementation of  {@link #close()},
42  *   implementation may allow accessing and allocating new transactions during closing
43  *   this instance.
44  *
45  * @param <T>
46  *            Type of {@link DOMStoreTransactionFactory} factory.
47  */
48 @Deprecated
49 abstract class AbstractDOMForwardedTransactionFactory<T extends DOMStoreTransactionFactory> implements AutoCloseable {
50     @SuppressWarnings("rawtypes")
51     private static final AtomicIntegerFieldUpdater<AbstractDOMForwardedTransactionFactory> UPDATER =
52             AtomicIntegerFieldUpdater.newUpdater(AbstractDOMForwardedTransactionFactory.class, "closed");
53     private final Map<LogicalDatastoreType, T> storeTxFactories;
54     private volatile int closed = 0;
55
56     protected AbstractDOMForwardedTransactionFactory(final Map<LogicalDatastoreType, ? extends T> txFactories) {
57         this.storeTxFactories = new EnumMap<>(txFactories);
58     }
59
60     /**
61      * Implementations must return unique identifier for each and every call of
62      * this method.
63      *
64      * @return new Unique transaction identifier.
65      */
66     protected abstract Object newTransactionIdentifier();
67
68     /**
69      * User-supplied implementation of {@link DOMDataWriteTransaction#commit()}
70      * for transaction.
71      *
72      * <p>
73      * Callback invoked when {@link DOMDataWriteTransaction#commit()} is invoked
74      * on transaction created by this factory.
75      *
76      * @param transaction
77      *            Transaction on which {@link DOMDataWriteTransaction#commit()}
78      *            was invoked.
79      * @param cohorts
80      *            Iteratable of cohorts for subtransactions associated with
81      *            the transaction being committed.
82      * @return a ListenableFuture. if commit coordination on cohorts finished successfully,
83      *         a CommitInfo is returned from the Future, On failure,
84      *         the Future fails with a {@link TransactionCommitFailedException}.
85      */
86     protected abstract <T> ListenableFuture<T> commit(DOMDataWriteTransaction transaction,
87             Collection<DOMStoreThreePhaseCommitCohort> cohorts, Supplier<T> futureValueSupplier);
88
89     /**
90      * Creates a new composite read-only transaction
91      *
92      * <p>
93      * Creates a new composite read-only transaction backed by one transaction
94      * per factory in {@link #getTxFactories()}.
95      *
96      * <p>
97      * Subtransaction for reading is selected by supplied
98      * {@link LogicalDatastoreType} as parameter for
99      * {@link DOMDataReadOnlyTransaction#read(LogicalDatastoreType,
100      * org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}.
101      *
102      * <p>
103      * Id of returned transaction is retrieved via
104      * {@link #newTransactionIdentifier()}.
105      *
106      * @return New composite read-only transaction.
107      */
108     public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
109         checkNotClosed();
110
111         final Map<LogicalDatastoreType, DOMStoreReadTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
112         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
113             txns.put(store.getKey(), store.getValue().newReadOnlyTransaction());
114         }
115         return new DOMForwardedReadOnlyTransaction(newTransactionIdentifier(), txns);
116     }
117
118     /**
119      * Creates a new composite write-only transaction
120      *
121      * <p>
122      * Creates a new composite write-only transaction backed by one write-only
123      * transaction per factory in {@link #getTxFactories()}.
124      *
125      * <p>
126      * Implementation of composite Write-only transaction is following:
127      *
128      * <p>
129      * <ul><li>
130      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
131      * - backing subtransaction is selected by {@link LogicalDatastoreType},
132      * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier,
133      * org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
134      * is invoked on selected subtransaction.
135      * </li><li>
136      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, YangInstanceIdentifier, NormalizedNode)}
137      * - backing subtransaction is selected by {@link LogicalDatastoreType},
138      * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier,
139      * org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
140      * is invoked on selected subtransaction.
141      * </li><li>
142      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, Path)}
143      * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is
144      * invoked on
145      * selected subtransaction.
146      * </li><li> {@link DOMDataWriteTransaction#commit()} - results in invoking
147      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
148      * and then invoking finalized implementation callback
149      * {@link #commit} with transaction which 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#commit()} - results in invoking
211      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
212      * and then invoking finalized implementation callback
213      * {@link #commit} with transaction which was committed and gathered results.
214      * <li>
215      * </ul>
216      *
217      * <p>
218      * Id of returned transaction is generated via
219      * {@link #newTransactionIdentifier()}.
220      *
221      * @return New composite read-write transaction associated with this factory.
222      */
223     public final DOMDataReadWriteTransaction newReadWriteTransaction() {
224         checkNotClosed();
225
226         final Map<LogicalDatastoreType, DOMStoreReadWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
227         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
228             txns.put(store.getKey(), store.getValue().newReadWriteTransaction());
229         }
230         return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), txns, this);
231     }
232
233     /**
234      * Convenience accessor of backing factories intended to be used only by
235      * finalization of this class.
236      *
237      * <b>Note:</b>
238      * Finalization of this class may want to access other functionality of
239      * supplied Transaction factories.
240      *
241      * @return Map of backing transaction factories.
242      */
243     protected final Map<LogicalDatastoreType, T> getTxFactories() {
244         return storeTxFactories;
245     }
246
247     /**
248      * Checks if instance is not closed.
249      *
250      * @throws IllegalStateException If instance of this class was closed.
251      *
252      */
253     protected final void checkNotClosed() {
254         Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
255     }
256
257     @Override
258     public void close() {
259         final boolean success = UPDATER.compareAndSet(this, 0, 1);
260         Preconditions.checkState(success, "Transaction factory was already closed");
261     }
262 }
263

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.