Bug 3194: Dynamically update PrimaryShardInfo cache when leader changes
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionChain.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 akka.actor.ActorSelection;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
13 import org.opendaylight.controller.sal.core.spi.data.AbstractSnapshotBackedTransactionChain;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
22
23 /**
24  * Transaction chain instantiated on top of a locally-available DataTree. It does not instantiate
25  * a transaction in the leader and rather chains transactions on top of themselves.
26  */
27 final class LocalTransactionChain extends AbstractSnapshotBackedTransactionChain<TransactionIdentifier>
28         implements LocalTransactionFactory {
29     private static final Throwable ABORTED = new Throwable("Transaction aborted");
30     private final TransactionChainProxy parent;
31     private final ActorSelection leader;
32     private final DataTree tree;
33
34     LocalTransactionChain(final TransactionChainProxy parent, final ActorSelection leader, final DataTree tree) {
35         this.parent = Preconditions.checkNotNull(parent);
36         this.leader = Preconditions.checkNotNull(leader);
37         this.tree = Preconditions.checkNotNull(tree);
38     }
39
40     DataTree getDataTree() {
41         return tree;
42     }
43
44     @Override
45     protected TransactionIdentifier nextTransactionIdentifier() {
46         throw new UnsupportedOperationException();
47     }
48
49     @Override
50     protected boolean getDebugTransactions() {
51         return false;
52     }
53
54     @Override
55     protected DataTreeSnapshot takeSnapshot() {
56         return tree.takeSnapshot();
57     }
58
59     @Override
60     protected DOMStoreThreePhaseCommitCohort createCohort(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
61         return new LocalThreePhaseCommitCohort(parent.getActorContext(), leader, transaction, modification) {
62             @Override
63             protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
64                 onTransactionFailed(transaction, ABORTED);
65             }
66
67             @Override
68             protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
69                 onTransactionCommited(transaction);
70             }
71         };
72     }
73
74     @Override
75     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
76         return super.newReadOnlyTransaction(identifier);
77     }
78
79     @Override
80     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
81         return super.newReadWriteTransaction(identifier);
82     }
83
84     @Override
85     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
86         return super.newWriteOnlyTransaction(identifier);
87     }
88 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.