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.base.Supplier;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collection;
14 import java.util.EnumMap;
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;
33 * Abstract composite transaction factory.
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}.
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
46 * Type of {@link DOMStoreTransactionFactory} factory.
48 abstract class AbstractDOMForwardedTransactionFactory<T extends DOMStoreTransactionFactory> implements AutoCloseable {
49 @SuppressWarnings("rawtypes")
50 private static final AtomicIntegerFieldUpdater<AbstractDOMForwardedTransactionFactory> UPDATER =
51 AtomicIntegerFieldUpdater.newUpdater(AbstractDOMForwardedTransactionFactory.class, "closed");
52 private final Map<LogicalDatastoreType, T> storeTxFactories;
53 private volatile int closed = 0;
55 protected AbstractDOMForwardedTransactionFactory(final Map<LogicalDatastoreType, ? extends T> txFactories) {
56 this.storeTxFactories = new EnumMap<>(txFactories);
60 * Implementations must return unique identifier for each and every call of
63 * @return new Unique transaction identifier.
65 protected abstract Object newTransactionIdentifier();
68 * User-supplied implementation of {@link DOMDataWriteTransaction#commit()}
72 * Callback invoked when {@link DOMDataWriteTransaction#commit()} is invoked
73 * on transaction created by this factory.
76 * Transaction on which {@link DOMDataWriteTransaction#commit()}
79 * Iteratable of cohorts for subtransactions associated with
80 * the transaction being committed.
81 * @return a ListenableFuture. if commit coordination on cohorts finished successfully,
82 * a CommitInfo is returned from the Future, On failure,
83 * the Future fails with a {@link TransactionCommitFailedException}.
85 protected abstract <T> ListenableFuture<T> commit(DOMDataWriteTransaction transaction,
86 Collection<DOMStoreThreePhaseCommitCohort> cohorts, Supplier<T> futureValueSupplier);
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 #commit} with transaction which was commited and gathered results.
153 * Id of returned transaction is generated via
154 * {@link #newTransactionIdentifier()}.
156 * @return New composite write-only transaction associated with this factory.
158 public final DOMDataWriteTransaction newWriteOnlyTransaction() {
161 final Map<LogicalDatastoreType, DOMStoreWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
162 for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
163 txns.put(store.getKey(), store.getValue().newWriteOnlyTransaction());
165 return new DOMForwardedWriteTransaction<>(newTransactionIdentifier(), txns, this);
169 * Creates a new composite write-only transaction
172 * Creates a new composite write-only transaction backed by one write-only transaction per factory in
173 * {@link #getTxFactories()}.
176 * Implementation of composite Write-only transaction is following:
181 * {@link org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction#read(LogicalDatastoreType,
182 * YangInstanceIdentifier)}
183 * - backing subtransaction is selected by {@link LogicalDatastoreType},
184 * {@link DOMStoreReadTransaction#read(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is invoked
185 * on selected subtransaction.
187 * {@link DOMDataWriteTransaction#put(LogicalDatastoreType,
188 * org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier,
189 * org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
190 * - backing subtransaction is selected by {@link LogicalDatastoreType},
191 * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier,
192 * org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
193 * is invoked on selected subtransaction.
195 * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType,
196 * org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier,
197 * org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
198 * - backing subtransaction is selected by {@link LogicalDatastoreType},
199 * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier,
200 * org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
201 * is invoked on selected subtransaction.
203 * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType,
204 * org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
205 * - backing subtransaction is selected by {@link LogicalDatastoreType},
206 * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)} is
208 * selected subtransaction.
209 * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
210 * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts
211 * and then invoking finalized implementation callback
212 * {@link #commit} with transaction which was committed and gathered results.
217 * Id of returned transaction is generated via
218 * {@link #newTransactionIdentifier()}.
220 * @return New composite read-write transaction associated with this factory.
222 public final DOMDataReadWriteTransaction newReadWriteTransaction() {
225 final Map<LogicalDatastoreType, DOMStoreReadWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
226 for (Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
227 txns.put(store.getKey(), store.getValue().newReadWriteTransaction());
229 return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), txns, this);
233 * Convenience accessor of backing factories intended to be used only by
234 * finalization of this class.
237 * Finalization of this class may want to access other functionality of
238 * supplied Transaction factories.
240 * @return Map of backing transaction factories.
242 protected final Map<LogicalDatastoreType, T> getTxFactories() {
243 return storeTxFactories;
247 * Checks if instance is not closed.
249 * @throws IllegalStateException If instance of this class was closed.
252 protected final void checkNotClosed() {
253 Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
257 public void close() {
258 final boolean success = UPDATER.compareAndSet(this, 0, 1);
259 Preconditions.checkState(success, "Transaction factory was already closed");