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