Make LocalThreePhaseCommitCohort::operationError final
[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 javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
15 import org.opendaylight.controller.sal.core.spi.data.AbstractSnapshotBackedTransactionChain;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.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 = Preconditions.checkNotNull(parent);
38         this.leader = Preconditions.checkNotNull(leader);
39         this.tree = Preconditions.checkNotNull(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(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
63         return new LocalChainThreePhaseCommitCohort(transaction, modification);
64     }
65
66     @Override
67     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
68         return super.newReadOnlyTransaction(identifier);
69     }
70
71     @Override
72     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
73         return super.newReadWriteTransaction(identifier);
74     }
75
76     @Override
77     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
78         return super.newWriteOnlyTransaction(identifier);
79     }
80
81     @SuppressWarnings("unchecked")
82     @Override
83     public LocalThreePhaseCommitCohort onTransactionReady(@Nonnull DOMStoreWriteTransaction tx,
84             @Nullable Exception operationError) {
85         if(operationError != null) {
86             return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx,
87                     operationError);
88         }
89
90         try {
91             return (LocalThreePhaseCommitCohort) tx.ready();
92         } catch (Exception e) {
93             // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
94             // LocalThreePhaseCommitCohort and the base class.
95             return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);
96         }
97     }
98
99     private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
100
101         protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
102                 DataTreeModification modification) {
103             super(parent.getActorContext(), leader, transaction, modification);
104         }
105
106         protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
107                 Exception operationError) {
108             super(parent.getActorContext(), leader, transaction, operationError);
109         }
110
111         @Override
112         protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
113             onTransactionFailed(transaction, ABORTED);
114         }
115
116         @Override
117         protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
118             onTransactionCommited(transaction);
119         }
120     }
121 }