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