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 javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.controller.cluster.access.concepts.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;
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.
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;
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);
42 DataTree getDataTree() {
47 protected TransactionIdentifier nextTransactionIdentifier() {
48 throw new UnsupportedOperationException();
52 protected boolean getDebugTransactions() {
57 protected DataTreeSnapshot takeSnapshot() {
58 return tree.takeSnapshot();
62 protected DOMStoreThreePhaseCommitCohort createCohort(
63 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
64 final DataTreeModification modification) {
65 return new LocalChainThreePhaseCommitCohort(transaction, modification);
69 public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
70 return super.newReadOnlyTransaction(identifier);
74 public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
75 return super.newReadWriteTransaction(identifier);
79 public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
80 return super.newWriteOnlyTransaction(identifier);
83 @SuppressWarnings("unchecked")
85 public LocalThreePhaseCommitCohort onTransactionReady(@Nonnull DOMStoreWriteTransaction tx,
86 @Nullable Exception operationError) {
87 if(operationError != null) {
88 return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx,
93 return (LocalThreePhaseCommitCohort) tx.ready();
94 } catch (Exception e) {
95 // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
96 // LocalThreePhaseCommitCohort and the base class.
97 return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);
101 private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
103 protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
104 DataTreeModification modification) {
105 super(parent.getActorContext(), leader, transaction, modification);
108 protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
109 Exception operationError) {
110 super(parent.getActorContext(), leader, transaction, operationError);
114 protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
115 onTransactionFailed(transaction, ABORTED);
119 protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
120 onTransactionCommited(transaction);