Bug 3195: Cleanup on error paths and error handling
[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 LocalChainThreePhaseCommitCohort(transaction, modification);
62     }
63
64     @Override
65     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
66         return super.newReadOnlyTransaction(identifier);
67     }
68
69     @Override
70     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
71         return super.newReadWriteTransaction(identifier);
72     }
73
74     @Override
75     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
76         return super.newWriteOnlyTransaction(identifier);
77     }
78
79     @SuppressWarnings("unchecked")
80     @Override
81     public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx) {
82         try {
83             return (LocalThreePhaseCommitCohort) tx.ready();
84         } catch (Exception e) {
85             // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
86             // LocalThreePhaseCommitCohort and the base class.
87             return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);
88         }
89     }
90
91     private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
92
93         protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
94                 DataTreeModification modification) {
95             super(parent.getActorContext(), leader, transaction, modification);
96         }
97
98         protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
99                 Exception operationError) {
100             super(parent.getActorContext(), leader, transaction, operationError);
101         }
102
103         @Override
104         protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
105             onTransactionFailed(transaction, ABORTED);
106         }
107
108         @Override
109         protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
110             onTransactionCommited(transaction);
111         }
112     }
113 }

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