c25109517674fe41ed8049010001e9cf495bcb79
[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      *
46      * @param transaction
47      * @param cohorts
48      * @return
49      */
50     protected abstract CheckedFuture<Void,TransactionCommitFailedException> submit(final DOMDataWriteTransaction transaction,
51                                                                                    final Collection<DOMStoreThreePhaseCommitCohort> cohorts);
52
53     /**
54      *
55      * @return
56      */
57     public final DOMDataReadOnlyTransaction newReadOnlyTransaction() {
58         checkNotClosed();
59
60         return new DOMBrokerReadOnlyTransaction(newTransactionIdentifier(), storeTxFactories);
61     }
62
63
64     /**
65      *
66      * @return
67      */
68     public final DOMDataWriteTransaction newWriteOnlyTransaction() {
69         checkNotClosed();
70
71         return new DOMBrokerWriteOnlyTransaction(newTransactionIdentifier(), storeTxFactories, this);
72     }
73
74
75     /**
76      *
77      * @return
78      */
79     public final DOMDataReadWriteTransaction newReadWriteTransaction() {
80         checkNotClosed();
81
82         return new DOMBrokerReadWriteTransaction(newTransactionIdentifier(), storeTxFactories, this);
83     }
84
85     /**
86      * Convenience accessor of backing factories intended to be used only by
87      * finalization of this class.
88      *
89      * <b>Note:</b>
90      * Finalization of this class may want to access other functionality of
91      * supplied Transaction factories.
92      *
93      * @return Map of backing transaction factories.
94      */
95     protected final Map<LogicalDatastoreType, T> getTxFactories() {
96         return storeTxFactories;
97     }
98
99     /**
100      * Checks if instance is not closed.
101      *
102      * @throws IllegalStateException If instance of this class was closed.
103      *
104      */
105     protected final void checkNotClosed() {
106         Preconditions.checkState(closed == 0, "Transaction factory was closed. No further operations allowed.");
107     }
108
109     @Override
110     public void close() {
111         final boolean success = UPDATER.compareAndSet(this, 0, 1);
112         Preconditions.checkState(success, "Transaction factory was already closed");
113     }
114 }