2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
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.
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;
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);
40 DataTree getDataTree() {
45 protected TransactionIdentifier nextTransactionIdentifier() {
46 throw new UnsupportedOperationException();
50 protected boolean getDebugTransactions() {
55 protected DataTreeSnapshot takeSnapshot() {
56 return tree.takeSnapshot();
60 protected DOMStoreThreePhaseCommitCohort createCohort(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
61 return new LocalChainThreePhaseCommitCohort(transaction, modification);
65 public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
66 return super.newReadOnlyTransaction(identifier);
70 public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
71 return super.newReadWriteTransaction(identifier);
75 public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
76 return super.newWriteOnlyTransaction(identifier);
79 @SuppressWarnings("unchecked")
81 public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx) {
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);
91 private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
93 protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
94 DataTreeModification modification) {
95 super(parent.getActorContext(), leader, transaction, modification);
98 protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
99 Exception operationError) {
100 super(parent.getActorContext(), leader, transaction, operationError);
104 protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
105 onTransactionFailed(transaction, ABORTED);
109 protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
110 onTransactionCommited(transaction);