BUG-5280: synchronize access to local histories
[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.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
16
17 /**
18  * Abstract base for transactions running on SharrdDataTree.
19  *
20  * @param <T> Backing transaction type.
21  */
22 @NotThreadSafe
23 abstract class AbstractShardDataTreeTransaction<T extends DataTreeSnapshot>
24         implements Identifiable<TransactionIdentifier> {
25     private final TransactionIdentifier id;
26     private final T snapshot;
27
28     private boolean closed;
29
30     AbstractShardDataTreeTransaction(final TransactionIdentifier id, final T snapshot) {
31         this.snapshot = Preconditions.checkNotNull(snapshot);
32         this.id = Preconditions.checkNotNull(id);
33     }
34
35     @Override
36     public final TransactionIdentifier getIdentifier() {
37         return id;
38     }
39
40     final T getSnapshot() {
41         return snapshot;
42     }
43
44     final boolean isClosed() {
45         return closed;
46     }
47
48     /**
49      * Close this transaction and mark it as closed, allowing idempotent invocations.
50      *
51      * @return True if the transaction got closed by this method invocation.
52      */
53     protected final boolean close() {
54         if (closed) {
55             return false;
56         }
57
58         closed = true;
59         return true;
60     }
61
62     @Override
63     public final String toString() {
64         return MoreObjects.toStringHelper(this).add("id", id).add("closed", closed).add("snapshot", snapshot)
65                 .toString();
66     }
67
68     abstract void abort();
69 }