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;
30 * Abstract composite transaction factory.
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}.
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
42 * Type of {@link DOMStoreTransactionFactory} factory.
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;
51 protected AbstractDOMForwardedTransactionFactory(final Map<LogicalDatastoreType, ? extends T> txFactories) {
52 this.storeTxFactories = new EnumMap<>(txFactories);
56 * Implementations must return unique identifier for each and every call of
59 * @return new Unique transaction identifier.
61 protected abstract Object newTransactionIdentifier();
64 * User-supplied implementation of {@link DOMDataWriteTransaction#submit()}
67 * Callback invoked when {@link DOMDataWriteTransaction#submit()} is invoked
68 * on transaction created by this factory.
71 * Transaction on which {@link DOMDataWriteTransaction#commit()}
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}.
80 protected abstract CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
81 final Collection<DOMStoreThreePhaseCommitCohort> cohorts);
84 * Creates a new composite read-only transaction
86 * Creates a new composite read-only transaction backed by one transaction per factory in
87 * {@link #getTxFactories()}.
89 * Subtransaction for reading is selected by supplied {@link LogicalDatastoreType} as parameter
91 * {@link DOMDataReadOnlyTransaction#read(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
94 * Id of returned transaction is retrieved via {@link #newTransactionIdentifier()}.
96 * @return New composite read-only transaction.
98 public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
101 final Map<LogicalDatastoreType, DOMStoreReadTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
102 for (final Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
103 txns.put(store.getKey(), store.getValue().newReadOnlyTransaction());
105 return new DOMForwardedReadOnlyTransaction(newTransactionIdentifier(), txns);
109 * Creates a new composite write-only transaction
112 * Creates a new composite write-only transaction backed by one write-only transaction per
113 * factory in {@link #getTxFactories()}.
116 * Implementation of composite Write-only transaction is following:
120 * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
121 * - backing subtransaction is selected by {@link LogicalDatastoreType},
122 * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
123 * is invoked on selected subtransaction.</li>
125 * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
126 * - backing subtransaction is selected by {@link LogicalDatastoreType},
127 * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
128 * is invoked on selected subtransaction.</li>
130 * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
131 * - backing subtransaction is selected by {@link LogicalDatastoreType},
132 * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
133 * is invoked on selected subtransaction.
134 * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
135 * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts and then invoking
136 * finalized implementation callback {@link #submit(DOMDataWriteTransaction, Collection)} with
137 * transaction which was commited and gathered results.</li>
140 * Id of returned transaction is generated via {@link #newTransactionIdentifier()}.
142 * @return New composite write-only transaction associated with this factory.
144 public final DOMDataWriteTransaction newWriteOnlyTransaction() {
147 final Map<LogicalDatastoreType, DOMStoreWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
148 for (final Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
149 txns.put(store.getKey(), store.getValue().newWriteOnlyTransaction());
151 return new DOMForwardedWriteTransaction<DOMStoreWriteTransaction>(newTransactionIdentifier(), txns, this);
155 * Creates a new composite write-only transaction
158 * Creates a new composite write-only transaction backed by one write-only transaction per
159 * factory in {@link #getTxFactories()}.
161 * Implementation of composite Write-only transaction is following:
165 * {@link DOMDataWriteTransaction#put(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
166 * - backing subtransaction is selected by {@link LogicalDatastoreType},
167 * {@link DOMStoreWriteTransaction#write(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
168 * is invoked on selected subtransaction.</li>
170 * {@link DOMDataWriteTransaction#merge(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
171 * - backing subtransaction is selected by {@link LogicalDatastoreType},
172 * {@link DOMStoreWriteTransaction#merge(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)}
173 * is invoked on selected subtransaction.</li>
175 * {@link DOMDataWriteTransaction#delete(LogicalDatastoreType, org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
176 * - backing subtransaction is selected by {@link LogicalDatastoreType},
177 * {@link DOMStoreWriteTransaction#delete(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)}
178 * is invoked on selected subtransaction.</li>
179 * <li> {@link DOMDataWriteTransaction#commit()} - results in invoking
180 * {@link DOMStoreWriteTransaction#ready()}, gathering all resulting cohorts and then invoking
181 * finalized implementation callback {@link #submit(DOMDataWriteTransaction, Collection)} with
182 * transaction which was commited and gathered results.</li>
185 * Id of returned transaction is generated via {@link #newTransactionIdentifier()}.
187 * @return New composite read-write transaction associated with this factory.
189 public final DOMDataReadWriteTransaction newReadWriteTransaction() {
192 final Map<LogicalDatastoreType, DOMStoreReadWriteTransaction> txns = new EnumMap<>(LogicalDatastoreType.class);
193 for (final Entry<LogicalDatastoreType, T> store : storeTxFactories.entrySet()) {
194 txns.put(store.getKey(), store.getValue().newReadWriteTransaction());
196 return new DOMForwardedReadWriteTransaction(newTransactionIdentifier(), txns, this);
200 * Convenience accessor of backing factories intended to be used only by
201 * finalization of this class.
204 * Finalization of this class may want to access other functionality of
205 * supplied Transaction factories.
207 * @return Map of backing transaction factories.
209 protected final Map<LogicalDatastoreType, T> getTxFactories() {
210 return storeTxFactories;
214 * Checks if instance is not closed.
216 * @throws IllegalStateException If instance of this class was closed.
219 protected final void checkNotClosed() {
220 Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
224 public void close() {
225 final boolean success = UPDATER.compareAndSet(this, 0, 1);
226 Preconditions.checkState(success, "Transaction factory was already closed");