2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.databroker;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.util.Collection;
14 import java.util.EnumMap;
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.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory;
25 public abstract class AbstractDOMTransactionFactory<T extends DOMStoreTransactionFactory> implements AutoCloseable {
26 private static final AtomicIntegerFieldUpdater<AbstractDOMTransactionFactory> UPDATER =
27 AtomicIntegerFieldUpdater.newUpdater(AbstractDOMTransactionFactory.class, "closed");
28 private final Map<LogicalDatastoreType, T> storeTxFactories;
29 private volatile int closed = 0;
31 protected AbstractDOMTransactionFactory(final Map<LogicalDatastoreType, T> txFactories) {
32 this.storeTxFactories = new EnumMap<>(txFactories);
36 * Implementations must return unique identifier for each and every call of
39 * @return new Unique transaction identifier.
41 protected abstract Object newTransactionIdentifier();
49 protected abstract CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
50 final Collection<DOMStoreThreePhaseCommitCohort> cohorts);
56 public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
59 return new DOMBrokerReadOnlyTransaction(newTransactionIdentifier(), storeTxFactories);
67 public final DOMDataWriteTransaction newWriteOnlyTransaction() {
70 return new DOMBrokerWriteOnlyTransaction(newTransactionIdentifier(), storeTxFactories, this);
78 public final DOMDataReadWriteTransaction newReadWriteTransaction() {
81 return new DOMBrokerReadWriteTransaction<>(newTransactionIdentifier(), storeTxFactories, this);
85 * Convenience accessor of backing factories intended to be used only by
86 * finalization of this class.
89 * Finalization of this class may want to access other functionality of
90 * supplied Transaction factories.
92 * @return Map of backing transaction factories.
94 protected final Map<LogicalDatastoreType, T> getTxFactories() {
95 return storeTxFactories;
99 * Checks if instance is not closed.
101 * @throws IllegalStateException If instance of this class was closed.
104 protected final void checkNotClosed() {
105 Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
109 public void close() {
110 final boolean success = UPDATER.compareAndSet(this, 0, 1);
111 Preconditions.checkState(success, "Transaction factory was already closed");