Fix warnings/javadocs in sal-distributed-datastore
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / AbstractDOMTransactionFactory.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.databroker;
10
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;
15 import java.util.Map;
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;
24
25 public abstract class AbstractDOMTransactionFactory<T extends DOMStoreTransactionFactory> implements AutoCloseable {
26     @SuppressWarnings("rawtypes")
27     private static final AtomicIntegerFieldUpdater<AbstractDOMTransactionFactory> UPDATER =
28             AtomicIntegerFieldUpdater.newUpdater(AbstractDOMTransactionFactory.class, "closed");
29     private final Map<LogicalDatastoreType, T> storeTxFactories;
30     private volatile int closed = 0;
31
32     protected AbstractDOMTransactionFactory(final Map<LogicalDatastoreType, T> txFactories) {
33         this.storeTxFactories = new EnumMap<>(txFactories);
34     }
35
36     /**
37      * Implementations must return unique identifier for each and every call of
38      * this method.
39      *
40      * @return new Unique transaction identifier.
41      */
42     protected abstract Object newTransactionIdentifier();
43
44     /**
45      * Submits a transaction asynchronously for commit.
46      *
47      * @param transaction the transaction to submit
48      * @param cohorts the associated cohorts
49      * @return a resulting Future
50      */
51     protected abstract CheckedFuture<Void,TransactionCommitFailedException> submit(DOMDataWriteTransaction transaction,
52             Collection<DOMStoreThreePhaseCommitCohort> cohorts);
53
54     /**
55      * Creates a new read-only transaction.
56      *
57      * @return the transaction instance
58      */
59     public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
60         checkNotClosed();
61
62         return new DOMBrokerReadOnlyTransaction(newTransactionIdentifier(), storeTxFactories);
63     }
64
65
66     /**
67      * Creates a new write-only transaction.
68      *
69      * @return the transaction instance
70      */
71     public final DOMDataWriteTransaction newWriteOnlyTransaction() {
72         checkNotClosed();
73
74         return new DOMBrokerWriteOnlyTransaction(newTransactionIdentifier(), storeTxFactories, this);
75     }
76
77
78     /**
79      * Creates a new read-write transaction.
80      *
81      * @return the transaction instance
82      */
83     public final DOMDataReadWriteTransaction newReadWriteTransaction() {
84         checkNotClosed();
85
86         return new DOMBrokerReadWriteTransaction(newTransactionIdentifier(), storeTxFactories, this);
87     }
88
89     /**
90      * Convenience accessor of backing factories intended to be used only by
91      * finalization of this class.
92      *
93      * <b>Note:</b>
94      * Finalization of this class may want to access other functionality of
95      * supplied Transaction factories.
96      *
97      * @return Map of backing transaction factories.
98      */
99     protected final Map<LogicalDatastoreType, T> getTxFactories() {
100         return storeTxFactories;
101     }
102
103     /**
104      * Checks if instance is not closed.
105      *
106      * @throws IllegalStateException If instance of this class was closed.
107      *
108      */
109     protected final void checkNotClosed() {
110         Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
111     }
112
113     @Override
114     public void close() {
115         final boolean success = UPDATER.compareAndSet(this, 0, 1);
116         Preconditions.checkState(success, "Transaction factory was already closed");
117     }
118 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.