791c3ef69b135f8387e3b0afb764bc7abaf70fc3
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import akka.actor.ActorSelection;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.mdsal.dom.spi.store.AbstractSnapshotBackedTransactionChain;
16 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
17 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
24
25 /**
26  * Transaction chain instantiated on top of a locally-available DataTree. It does not instantiate
27  * a transaction in the leader and rather chains transactions on top of themselves.
28  */
29 final class LocalTransactionChain extends AbstractSnapshotBackedTransactionChain<TransactionIdentifier>
30         implements LocalTransactionFactory {
31     private static final Throwable ABORTED = new Throwable("Transaction aborted");
32     private final TransactionChainProxy parent;
33     private final ActorSelection leader;
34     private final DataTree tree;
35
36     LocalTransactionChain(final TransactionChainProxy parent, final ActorSelection leader, final DataTree tree) {
37         this.parent = requireNonNull(parent);
38         this.leader = requireNonNull(leader);
39         this.tree = requireNonNull(tree);
40     }
41
42     DataTree getDataTree() {
43         return tree;
44     }
45
46     @Override
47     protected TransactionIdentifier nextTransactionIdentifier() {
48         throw new UnsupportedOperationException();
49     }
50
51     @Override
52     protected boolean getDebugTransactions() {
53         return false;
54     }
55
56     @Override
57     protected DataTreeSnapshot takeSnapshot() {
58         return tree.takeSnapshot();
59     }
60
61     @Override
62     protected DOMStoreThreePhaseCommitCohort createCohort(
63             final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
64             final DataTreeModification modification,
65             final Exception operationError) {
66         return new LocalChainThreePhaseCommitCohort(transaction, modification, operationError);
67     }
68
69     @Override
70     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
71         return super.newReadOnlyTransaction(identifier);
72     }
73
74     @Override
75     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
76         return super.newReadWriteTransaction(identifier);
77     }
78
79     @Override
80     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
81         return super.newWriteOnlyTransaction(identifier);
82     }
83
84     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
85     @Override
86     public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx, Exception operationError) {
87         checkArgument(tx instanceof SnapshotBackedWriteTransaction);
88         if (operationError != null) {
89             return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx,
90                     operationError);
91         }
92
93         try {
94             return (LocalThreePhaseCommitCohort) tx.ready();
95         } catch (Exception e) {
96             // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
97             // LocalThreePhaseCommitCohort and the base class.
98             return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);
99         }
100     }
101
102     private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
103
104         protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
105                 DataTreeModification modification, Exception operationError) {
106             super(parent.getActorUtils(), leader, transaction, modification, operationError);
107         }
108
109         protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
110                 Exception operationError) {
111             super(parent.getActorUtils(), leader, transaction, operationError);
112         }
113
114         @Override
115         protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
116             onTransactionFailed(transaction, ABORTED);
117         }
118
119         @Override
120         protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
121             onTransactionCommited(transaction);
122         }
123     }
124 }

©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.