CDS: Implement front-end support for local transactions
[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.DOMStoreReadWriteTransaction;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
16 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
20
21 /**
22  * Transaction chain instantiated on top of a locally-available DataTree. It does not instantiate
23  * a transaction in the leader and rather chains transactions on top of themselves.
24  */
25 final class LocalTransactionChain extends AbstractSnapshotBackedTransactionChain<TransactionIdentifier>
26         implements LocalTransactionFactory {
27     private static final Throwable ABORTED = new Throwable("Transaction aborted");
28     private final TransactionChainProxy parent;
29     private final ActorSelection leader;
30     private final DataTree tree;
31
32     LocalTransactionChain(final TransactionChainProxy parent, final ActorSelection leader, final DataTree tree) {
33         this.parent = Preconditions.checkNotNull(parent);
34         this.leader = Preconditions.checkNotNull(leader);
35         this.tree = Preconditions.checkNotNull(tree);
36     }
37
38     DataTree getDataTree() {
39         return tree;
40     }
41
42     @Override
43     protected TransactionIdentifier nextTransactionIdentifier() {
44         throw new UnsupportedOperationException();
45     }
46
47     @Override
48     protected boolean getDebugTransactions() {
49         return false;
50     }
51
52     @Override
53     protected DataTreeSnapshot takeSnapshot() {
54         return tree.takeSnapshot();
55     }
56
57     @Override
58     protected DOMStoreThreePhaseCommitCohort createCohort(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
59         return new LocalThreePhaseCommitCohort(parent.getActorContext(), leader, transaction, modification) {
60             @Override
61             protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
62                 onTransactionFailed(transaction, ABORTED);
63             }
64
65             @Override
66             protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
67                 onTransactionCommited(transaction);
68             }
69         };
70     }
71
72     @Override
73     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
74         return super.newReadWriteTransaction(identifier);
75     }
76 }