BUG-5280: implement backend message handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeTransactionChain.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.LocalHistoryIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.yangtools.concepts.Identifiable;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * A transaction chain attached to a Shard.
23  */
24 @NotThreadSafe
25 final class ShardDataTreeTransactionChain extends ShardDataTreeTransactionParent
26         implements Identifiable<LocalHistoryIdentifier> {
27     private static final Logger LOG = LoggerFactory.getLogger(ShardDataTreeTransactionChain.class);
28     private final LocalHistoryIdentifier chainId;
29     private final ShardDataTree dataTree;
30
31     private ReadWriteShardDataTreeTransaction previousTx;
32     private ReadWriteShardDataTreeTransaction openTransaction;
33     private boolean closed;
34
35     ShardDataTreeTransactionChain(final LocalHistoryIdentifier localHistoryIdentifier, final ShardDataTree dataTree) {
36         this.chainId = Preconditions.checkNotNull(localHistoryIdentifier);
37         this.dataTree = Preconditions.checkNotNull(dataTree);
38     }
39
40     private DataTreeSnapshot getSnapshot() {
41         Preconditions.checkState(!closed, "TransactionChain %s has been closed", this);
42         Preconditions.checkState(openTransaction == null, "Transaction %s is open", openTransaction);
43
44         if (previousTx == null) {
45             return dataTree.takeSnapshot();
46         } else {
47             return previousTx.getSnapshot();
48         }
49     }
50
51     ReadOnlyShardDataTreeTransaction newReadOnlyTransaction(final TransactionIdentifier txId) {
52         final DataTreeSnapshot snapshot = getSnapshot();
53         LOG.debug("Allocated read-only transaction {} snapshot {}", txId, snapshot);
54
55         return new ReadOnlyShardDataTreeTransaction(txId, snapshot);
56     }
57
58     ReadWriteShardDataTreeTransaction newReadWriteTransaction(final TransactionIdentifier txId) {
59         final DataTreeSnapshot snapshot = getSnapshot();
60         LOG.debug("Allocated read-write transaction {} snapshot {}", txId, snapshot);
61
62         openTransaction = new ReadWriteShardDataTreeTransaction(this, txId, snapshot.newModification());
63         return openTransaction;
64     }
65
66     void close() {
67         closed = true;
68     }
69
70     @Override
71     protected void abortTransaction(final AbstractShardDataTreeTransaction<?> transaction) {
72         if (transaction instanceof ReadWriteShardDataTreeTransaction) {
73             Preconditions.checkState(openTransaction != null,
74                     "Attempted to abort transaction %s while none is outstanding", transaction);
75             LOG.debug("Aborted transaction {}", transaction);
76             openTransaction = null;
77         }
78     }
79
80     @Override
81     protected ShardDataTreeCohort finishTransaction(final ReadWriteShardDataTreeTransaction transaction) {
82         Preconditions.checkState(openTransaction != null,
83                 "Attempted to finish transaction %s while none is outstanding", transaction);
84
85         // dataTree is finalizing ready the transaction, we just record it for the next
86         // transaction in chain
87         final ShardDataTreeCohort delegate = dataTree.finishTransaction(transaction);
88         openTransaction = null;
89         previousTx = transaction;
90         LOG.debug("Committing transaction {}", transaction);
91
92         return new ChainedCommitCohort(this, transaction, delegate);
93     }
94
95     @Override
96     public String toString() {
97         return MoreObjects.toStringHelper(this).add("id", chainId).toString();
98     }
99
100     void clearTransaction(final ReadWriteShardDataTreeTransaction transaction) {
101         if (transaction.equals(previousTx)) {
102             previousTx = null;
103         }
104     }
105
106     @Override
107     public LocalHistoryIdentifier getIdentifier() {
108         return chainId;
109     }
110
111     @Override
112     ShardDataTreeCohort createReadyCohort(final TransactionIdentifier txId, final DataTreeModification modification) {
113         return dataTree.createReadyCohort(txId, modification);
114     }
115 }