Unit tests for ClientBackedDataStore class
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractShardDataTreeTransaction.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 package org.opendaylight.controller.cluster.datastore;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.concurrent.NotThreadSafe;
13 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.persisted.AbortTransactionPayload;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Abstract base for transactions running on SharrdDataTree.
22  *
23  * @param <T> Backing transaction type.
24  */
25 @NotThreadSafe
26 abstract class AbstractShardDataTreeTransaction<T extends DataTreeSnapshot>
27         implements Identifiable<TransactionIdentifier> {
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractShardDataTreeTransaction.class);
29
30     private final ShardDataTreeTransactionParent parent;
31     private final TransactionIdentifier id;
32     private final T snapshot;
33
34     private boolean closed;
35
36     AbstractShardDataTreeTransaction(final ShardDataTreeTransactionParent parent, final TransactionIdentifier id,
37         final T snapshot) {
38         this.parent = Preconditions.checkNotNull(parent);
39         this.snapshot = Preconditions.checkNotNull(snapshot);
40         this.id = Preconditions.checkNotNull(id);
41     }
42
43     @Override
44     public final TransactionIdentifier getIdentifier() {
45         return id;
46     }
47
48     final ShardDataTreeTransactionParent getParent() {
49         return parent;
50     }
51
52     final T getSnapshot() {
53         return snapshot;
54     }
55
56     final boolean isClosed() {
57         return closed;
58     }
59
60     /**
61      * Close this transaction and mark it as closed, allowing idempotent invocations.
62      *
63      * @return True if the transaction got closed by this method invocation.
64      */
65     protected final boolean close() {
66         if (closed) {
67             return false;
68         }
69
70         closed = true;
71         return true;
72     }
73
74     final void abort(final Runnable callback) {
75         Preconditions.checkState(close(), "Transaction is already closed");
76         parent.abortTransaction(this, callback);
77     }
78
79     /**
80      * This method is exposed for sake of {@link ShardTransaction}, which is an actor. We need to ensure that
81      * the parent is updated to reflect the transaction has been closed, but no journal actions may be invoked.
82      *
83      * <p>
84      * ShardTransaction is responsible for additionally sending a request to persist an {@link AbortTransactionPayload}
85      * via a message to the Shard actor.
86      */
87     final void abortFromTransactionActor() {
88         if (close()) {
89             parent.abortFromTransactionActor(this);
90         }
91     }
92
93     @Override
94     public final String toString() {
95         return MoreObjects.toStringHelper(this).add("id", id).add("closed", closed).add("snapshot", snapshot)
96                 .toString();
97     }
98 }