2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.broker.impl;
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;
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;
32 * Abstract composite transaction factory.
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}.
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
45 * Type of {@link DOMStoreTransactionFactory} factory.
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;
54 protected AbstractDOMForwardedTransactionFactory(final Map<LogicalDatastoreType, ? extends T> txFactories) {
55 this.storeTxFactories = new EnumMap<>(txFactories);
59 * Implementations must return unique identifier for each and every call of
62 * @return new Unique transaction identifier.
64 protected abstract Object newTransactionIdentifier();
67 * User-supplied implementation of {@link DOMDataWriteTransaction#submit()}
71 * Callback invoked when {@link DOMDataWriteTransaction#submit()} is invoked
72 * on transaction created by this factory.
75 * Transaction on which {@link DOMDataWriteTransaction#commit()}
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}.
84 protected abstract CheckedFuture<Void,
85 TransactionCommitFailedException>
86 submit(DOMDataWriteTransaction transaction, Collection<DOMStoreThreePhaseCommitCohort> cohorts);
89 * Creates a new composite read-only transaction
92 * Creates a new composite read-only transaction backed by one transaction
93 * per factory in {@link #getTxFactories()}.
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)}.
102 * Id of returned transaction is retrieved via
103 * {@link #newTransactionIdentifier()}.
105 * @return New composite read-only transaction.
107 public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
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());
114 return new DOMForwardedReadOnlyTransaction(newTransactionIdentifier(), txns);
118 * Creates a new composite write-only transaction
121 * Creates a new composite write-only transaction backed by one write-only
122 * transaction per factory in {@link #getTxFactories()}.
125 * Implementation of composite Write-only transaction is following:
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.
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.
141 * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, Path)}
142 * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is
144 * selected subtransaction.
145 * </li><li> {@link DOMDataWriteTransaction#commit()} - 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.
154 * Id of returned transaction is generated via
155 * {@link #newTransactionIdentifier()}.
157 * @return New composite write-only transaction associated with this factory.
159 public final DOMDataWriteTransaction newWriteOnlyTransaction() {
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());
166 return new DOMForwardedWriteTransaction<>(newTransactionIdentifier(), txns, this);
170 * Creates a new composite write-only transaction
173 * Creates a new composite write-only transaction backed by one write-only transaction per factory in
174 * {@link #getTxFactories()}.
177 * Implementation of composite Write-only transaction is following:
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.
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.
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.
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
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 #submit(DOMDataWriteTransaction, Collection)} with transaction which
214 * was committed and gathered results.
219 * Id of returned transaction is generated via
220 * {@link #newTransactionIdentifier()}.
222 * @return New composite read-write transaction associated with this factory.
224 public final DOMDataReadWriteTransaction newReadWriteTransaction() {
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());
231 return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), txns, this);
235 * Convenience accessor of backing factories intended to be used only by
236 * finalization of this class.
239 * Finalization of this class may want to access other functionality of
240 * supplied Transaction factories.
242 * @return Map of backing transaction factories.
244 protected final Map<LogicalDatastoreType, T> getTxFactories() {
245 return storeTxFactories;
249 * Checks if instance is not closed.
251 * @throws IllegalStateException If instance of this class was closed.
254 protected final void checkNotClosed() {
255 Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
259 public void close() {
260 final boolean success = UPDATER.compareAndSet(this, 0, 1);
261 Preconditions.checkState(success, "Transaction factory was already closed");