Move byte-based serialization method
[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 org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
13 import org.opendaylight.controller.cluster.datastore.persisted.AbortTransactionPayload;
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. This class is NOT thread-safe.
19  *
20  * @param <T> Backing transaction type.
21  */
22 abstract class AbstractShardDataTreeTransaction<T extends DataTreeSnapshot>
23         implements Identifiable<TransactionIdentifier> {
24     private final ShardDataTreeTransactionParent parent;
25     private final TransactionIdentifier id;
26     private final T snapshot;
27
28     private boolean closed;
29
30     AbstractShardDataTreeTransaction(final ShardDataTreeTransactionParent parent, final TransactionIdentifier id,
31         final T snapshot) {
32         this.parent = Preconditions.checkNotNull(parent);
33         this.snapshot = Preconditions.checkNotNull(snapshot);
34         this.id = Preconditions.checkNotNull(id);
35     }
36
37     @Override
38     public final TransactionIdentifier getIdentifier() {
39         return id;
40     }
41
42     final ShardDataTreeTransactionParent getParent() {
43         return parent;
44     }
45
46     final T getSnapshot() {
47         return snapshot;
48     }
49
50     final boolean isClosed() {
51         return closed;
52     }
53
54     /**
55      * Close this transaction and mark it as closed, allowing idempotent invocations.
56      *
57      * @return True if the transaction got closed by this method invocation.
58      */
59     protected final boolean close() {
60         if (closed) {
61             return false;
62         }
63
64         closed = true;
65         return true;
66     }
67
68     final void abort(final Runnable callback) {
69         Preconditions.checkState(close(), "Transaction is already closed");
70         parent.abortTransaction(this, callback);
71     }
72
73     /**
74      * This method is exposed for sake of {@link ShardTransaction}, which is an actor. We need to ensure that
75      * the parent is updated to reflect the transaction has been closed, but no journal actions may be invoked.
76      *
77      * <p>
78      * ShardTransaction is responsible for additionally sending a request to persist an {@link AbortTransactionPayload}
79      * via a message to the Shard actor.
80      */
81     final void abortFromTransactionActor() {
82         if (close()) {
83             parent.abortFromTransactionActor(this);
84         }
85     }
86
87     @Override
88     public final String toString() {
89         return MoreObjects.toStringHelper(this).add("id", id).add("closed", closed).add("snapshot", snapshot)
90                 .toString();
91     }
92 }