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

©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.