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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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.tree.api.DataTreeModification;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
23 import org.opendaylight.yangtools.yang.data.tree.api.ReadOnlyDataTree;
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 ReadOnlyDataTree tree;
36 LocalTransactionChain(final TransactionChainProxy parent, final ActorSelection leader,
37 final ReadOnlyDataTree tree) {
38 this.parent = requireNonNull(parent);
39 this.leader = requireNonNull(leader);
40 this.tree = requireNonNull(tree);
43 ReadOnlyDataTree getDataTree() {
48 protected TransactionIdentifier nextTransactionIdentifier() {
49 throw new UnsupportedOperationException();
53 protected boolean getDebugTransactions() {
58 protected DataTreeSnapshot takeSnapshot() {
59 return tree.takeSnapshot();
63 protected DOMStoreThreePhaseCommitCohort createCohort(
64 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
65 final DataTreeModification modification,
66 final Exception operationError) {
67 return new LocalChainThreePhaseCommitCohort(transaction, modification, operationError);
71 public DOMStoreReadTransaction newReadOnlyTransaction(final TransactionIdentifier identifier) {
72 return super.newReadOnlyTransaction(identifier);
76 public DOMStoreReadWriteTransaction newReadWriteTransaction(final TransactionIdentifier identifier) {
77 return super.newReadWriteTransaction(identifier);
81 public DOMStoreWriteTransaction newWriteOnlyTransaction(final TransactionIdentifier identifier) {
82 return super.newWriteOnlyTransaction(identifier);
85 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
87 public LocalThreePhaseCommitCohort onTransactionReady(final DOMStoreWriteTransaction tx,
88 final Exception operationError) {
89 checkArgument(tx instanceof SnapshotBackedWriteTransaction);
90 if (operationError != null) {
91 return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx,
96 return (LocalThreePhaseCommitCohort) tx.ready();
97 } catch (Exception e) {
98 // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
99 // LocalThreePhaseCommitCohort and the base class.
100 return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);
104 private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
106 protected LocalChainThreePhaseCommitCohort(
107 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
108 final DataTreeModification modification, final Exception operationError) {
109 super(parent.getActorUtils(), leader, transaction, modification, operationError);
112 protected LocalChainThreePhaseCommitCohort(
113 final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
114 final Exception operationError) {
115 super(parent.getActorUtils(), leader, transaction, operationError);
119 protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
120 onTransactionFailed(transaction, ABORTED);
124 protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
125 onTransactionCommited(transaction);