Merge "Add some unimplemented proxy classes related to DOMStore"
[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 java.util.Map;
11 import java.util.Map.Entry;
12
13 import javax.annotation.concurrent.GuardedBy;
14
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.ImmutableMap;
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 public abstract class AbstractDOMForwardedTransactionFactory<T extends DOMStoreTransactionFactory> implements DOMDataCommitImplementation, AutoCloseable {
46
47     private final ImmutableMap<LogicalDatastoreType, T> storeTxFactories;
48
49     private boolean closed;
50
51     protected AbstractDOMForwardedTransactionFactory(final Map<LogicalDatastoreType, ? extends T> txFactories) {
52         this.storeTxFactories = ImmutableMap.copyOf(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      * Creates a new composite read-only transaction
65      *
66      * Creates a new composite read-only transaction backed by one transaction
67      * per factory in {@link #getTxFactories()}.
68      *
69      * Subtransaction for reading is selected by supplied
70      * {@link LogicalDatastoreType} as parameter for
71      * {@link DOMDataReadTransaction#read(LogicalDatastoreType, InstanceIdentifier)}
72      * .
73      *
74      * Id of returned transaction is retrieved via
75      * {@link #newTransactionIdentifier()}.
76      *
77      * @return New composite read-only transaction.
78      */
79     public DOMDataReadTransaction newReadOnlyTransaction() {
80         checkNotClosed();
81         ImmutableMap.Builder<LogicalDatastoreType, DOMStoreReadTransaction> builder = ImmutableMap.builder();
82         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
83             builder.put(store.getKey(), store.getValue().newReadOnlyTransaction());
84         }
85         return new DOMForwardedReadOnlyTransaction(newTransactionIdentifier(), builder.build());
86     }
87
88
89
90     /**
91      * Creates a new composite write-only transaction
92      *
93      * <p>
94      * Creates a new composite write-only transaction backed by one write-only
95      * transaction per factory in {@link #getTxFactories()}.
96      *
97      * <p>
98      * Implementation of composite Write-only transaction is following:
99      *
100      * <ul>
101      * <li>
102      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
103      * - backing subtransaction is selected by {@link LogicalDatastoreType},
104      * {@link DOMStoreWriteTransaction#write(InstanceIdentifier, NormalizedNode)}
105      * is invoked on selected subtransaction.
106      * <li>
107      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
108      * - backing subtransaction is selected by {@link LogicalDatastoreType},
109      * {@link DOMStoreWriteTransaction#merge(InstanceIdentifier, NormalizedNode)}
110      * is invoked on selected subtransaction.
111      * <li>
112      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, InstanceIdentifier)
113      * - backing subtransaction is selected by {@link LogicalDatastoreType},
114      * {@link DOMStoreWriteTransaction#delete(InstanceIdentifier)} is invoked on
115      * selected subtransaction.
116      * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
117      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
118      * and then invoking finalized implementation callback
119      * {@link #commit(DOMDataWriteTransaction, Iterable)} with transaction which
120      * was commited and gathered results.
121      * </ul>
122      *
123      * Id of returned transaction is generated via
124      * {@link #newTransactionIdentifier()}.
125      *
126      * @return New composite write-only transaction associated with this
127      *         factory.
128      */
129     public DOMDataWriteTransaction newWriteOnlyTransaction() {
130         checkNotClosed();
131         ImmutableMap.Builder<LogicalDatastoreType, DOMStoreWriteTransaction> builder = ImmutableMap.builder();
132         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
133             builder.put(store.getKey(), store.getValue().newWriteOnlyTransaction());
134         }
135         return new DOMForwardedWriteTransaction<DOMStoreWriteTransaction>(newTransactionIdentifier(), builder.build(),
136                 this);
137     }
138
139     /**
140      * Creates a new composite write-only transaction
141      *
142      * <p>
143      * Creates a new composite write-only transaction backed by one write-only
144      * transaction per factory in {@link #getTxFactories()}.
145      * <p>
146      * Implementation of composite Write-only transaction is following:
147      *
148      * <ul>
149      * <li>
150      * {@link DOMDataWriteTransaction#read(LogicalDatastoreType, InstanceIdentifier)}
151      * - backing subtransaction is selected by {@link LogicalDatastoreType},
152      * {@link DOMStoreWriteTransaction#read(InstanceIdentifier)} is invoked on
153      * selected subtransaction.
154      * <li>
155      * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
156      * - backing subtransaction is selected by {@link LogicalDatastoreType},
157      * {@link DOMStoreWriteTransaction#write(InstanceIdentifier, NormalizedNode)}
158      * is invoked on selected subtransaction.
159      * <li>
160      * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, InstanceIdentifier, NormalizedNode)}
161      * - backing subtransaction is selected by {@link LogicalDatastoreType},
162      * {@link DOMStoreWriteTransaction#merge(InstanceIdentifier, NormalizedNode)}
163      * is invoked on selected subtransaction.
164      * <li>
165      * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, InstanceIdentifier)
166      * - backing subtransaction is selected by {@link LogicalDatastoreType},
167      * {@link DOMStoreWriteTransaction#delete(InstanceIdentifier)} is invoked on
168      * selected subtransaction.
169      * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
170      * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
171      * and then invoking finalized implementation callback
172      * {@link #commit(DOMDataWriteTransaction, Iterable)} with transaction which
173      * was commited and gathered results.
174      * <li>
175      * </ul>
176      *
177      * Id of returned transaction is generated via
178      * {@link #newTransactionIdentifier()}.
179      *
180      * @return New composite read-write transaction associated with this
181      *         factory.
182      *
183      */
184     public DOMDataReadWriteTransaction newReadWriteTransaction() {
185         checkNotClosed();
186         ImmutableMap.Builder<LogicalDatastoreType, DOMStoreReadWriteTransaction> builder = ImmutableMap.builder();
187         for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
188             builder.put(store.getKey(), store.getValue().newReadWriteTransaction());
189         }
190         return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), builder.build(), this);
191     }
192
193     /**
194      * Convenience accessor of backing factories intended to be used only by
195      * finalization of this class.
196      *
197      * <b>Note:</b>
198      * Finalization of this class may want to access other functionality of
199      * supplied Transaction factories.
200      *
201      * @return Map of backing transaction factories.
202      */
203     protected final Map<LogicalDatastoreType, T> getTxFactories() {
204         return storeTxFactories;
205     }
206
207     /**
208      *
209      * Checks if instance is not closed.
210      *
211      * @throws IllegalStateException If instance of this class was closed.
212      *
213      */
214     @GuardedBy("this")
215     protected synchronized void checkNotClosed() {
216         Preconditions.checkState(!closed,"Transaction factory was closed. No further operations allowed.");
217     }
218
219     @Override
220     @GuardedBy("this")
221     public synchronized void close() {
222         closed = true;
223     }
224
225 }