Mechanical code cleanup (sal-dom-broker)
[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
28 /**
29  *
30  * Abstract composite transaction factory.
31  *
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 DOMDataWriteTransaction#submit()}
65      * for transaction.
66      *
67      * Callback invoked when {@link DOMDataWriteTransaction#submit()} is invoked
68      * on transaction created by this factory.
69      *
70      * @param transaction
71      *            Transaction on which {@link DOMDataWriteTransaction#commit()}
72      *            was invoked.
73      * @param cohorts
74      *            Iteratable of cohorts for subtransactions associated with
75      *            the transaction being committed.
76      * @return a CheckedFuture. if commit coordination on cohorts finished successfully,
77      *         nothing is returned from the Future, On failure,
78      *         the Future fails with a {@link TransactionCommitFailedException}.
79      */
80     protected abstract CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
81             final Collection<DOMStoreThreePhaseCommitCohort> cohorts);
82
83     /**
84      * Creates a new composite read-only transaction
85      *
86      * Creates a new composite read-only transaction backed by one transaction
87      * per factory in {@link #getTxFactories()}.
88      *
89      * Subtransaction for reading is selected by supplied
90      * {@link LogicalDatastoreType} as parameter for
91      * {@link DOMDataReadOnlyTransaction#read(LogicalDatastoreType,org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)}
92      * .
93      *
94      * Id of returned transaction is retrieved via
95      * {@link #newTransactionIdentifier()}.
96      *
97      * @return New composite read-only transaction.
98      */
99     public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
100         checkNotClosed();
101
102         final Map<LogicalDatastoreType, DOMStoreReadTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
103         for (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
114      * transaction per factory in {@link #getTxFactories()}.
115      *
116      * <p>
117      * Implementation of composite Write-only transaction is following:
118      *
119      * <ul>
120      * <li>
121      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, 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.InstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
124      * is invoked on selected subtransaction.
125      * <li>
126      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, 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.InstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
129      * is invoked on selected subtransaction.
130      * <li>
131      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)
132      * - backing subtransaction is selected by {@link LogicalDatastoreType},
133      * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)} is invoked on
134      * selected subtransaction.
135      * <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, Iterable)} with transaction which
139      * was commited and gathered results.
140      * </ul>
141      *
142      * Id of returned transaction is generated via
143      * {@link #newTransactionIdentifier()}.
144      *
145      * @return New composite write-only transaction associated with this
146      *         factory.
147      */
148     public final DOMDataWriteTransaction newWriteOnlyTransaction() {
149         checkNotClosed();
150
151         final Map<LogicalDatastoreType, DOMStoreWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
152         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
153             txns.put(store.getKey(), store.getValue().newWriteOnlyTransaction());
154         }
155         return new DOMForwardedWriteTransaction<>(newTransactionIdentifier(), txns, this);
156     }
157
158     /**
159      * Creates a new composite write-only transaction
160      *
161      * <p>
162      * Creates a new composite write-only transaction backed by one write-only
163      * transaction per factory in {@link #getTxFactories()}.
164      * <p>
165      * Implementation of composite Write-only transaction is following:
166      *
167      * <ul>
168      * <li>
169      * {@link DOMDataWriteTransaction#read(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)}
170      * - backing subtransaction is selected by {@link LogicalDatastoreType},
171      * {@link DOMStoreWriteTransaction#read(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)} is invoked on
172      * selected subtransaction.
173      * <li>
174      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
175      * - backing subtransaction is selected by {@link LogicalDatastoreType},
176      * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
177      * is invoked on selected subtransaction.
178      * <li>
179      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
180      * - backing subtransaction is selected by {@link LogicalDatastoreType},
181      * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
182      * is invoked on selected subtransaction.
183      * <li>
184      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)
185      * - backing subtransaction is selected by {@link LogicalDatastoreType},
186      * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.InstanceIdentifier)} is invoked on
187      * selected subtransaction.
188      * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
189      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
190      * and then invoking finalized implementation callback
191      * {@link #submit(DOMDataWriteTransaction, Iterable)} with transaction which
192      * was commited and gathered results.
193      * <li>
194      * </ul>
195      *
196      * Id of returned transaction is generated via
197      * {@link #newTransactionIdentifier()}.
198      *
199      * @return New composite read-write transaction associated with this
200      *         factory.
201      */
202     public final DOMDataReadWriteTransaction newReadWriteTransaction() {
203         checkNotClosed();
204
205         final Map<LogicalDatastoreType, DOMStoreReadWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
206         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
207             txns.put(store.getKey(), store.getValue().newReadWriteTransaction());
208         }
209         return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), txns, this);
210     }
211
212     /**
213      * Convenience accessor of backing factories intended to be used only by
214      * finalization of this class.
215      *
216      * <b>Note:</b>
217      * Finalization of this class may want to access other functionality of
218      * supplied Transaction factories.
219      *
220      * @return Map of backing transaction factories.
221      */
222     protected final Map<LogicalDatastoreType, T> getTxFactories() {
223         return storeTxFactories;
224     }
225
226     /**
227      * Checks if instance is not closed.
228      *
229      * @throws IllegalStateException If instance of this class was closed.
230      *
231      */
232     protected final void checkNotClosed() {
233         Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
234     }
235
236     @Override
237     public void close() {
238         final boolean success = UPDATER.compareAndSet(this, 0, 1);
239         Preconditions.checkState(success, "Transaction factory was already closed");
240     }
241 }
242

©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.