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 javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
19 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
23 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
29 * {@link LocalTransactionFactory} for instantiating backing transactions which are
30 * disconnected from each other, ie not chained. These are used by {@link AbstractTransactionContextFactory}
31 * to instantiate transactions on shards which are co-located with the shard leader.
33 final class LocalTransactionFactoryImpl extends TransactionReadyPrototype<TransactionIdentifier>
34 implements LocalTransactionFactory {
36 private final ActorSelection leader;
37 private final DataTree dataTree;
38 private final ActorUtils actorUtils;
40 LocalTransactionFactoryImpl(final ActorUtils actorUtils, final ActorSelection leader, final DataTree dataTree) {
41 this.leader = requireNonNull(leader);
42 this.dataTree = requireNonNull(dataTree);
43 this.actorUtils = actorUtils;
46 DataTree getDataTree() {
51 public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
52 return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
56 public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
57 return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
61 public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
62 return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
66 protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
71 protected DOMStoreThreePhaseCommitCohort transactionReady(
72 final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
73 final DataTreeModification tree,
74 final Exception readyError) {
75 return new LocalThreePhaseCommitCohort(actorUtils, leader, tx, tree, readyError);
78 @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
80 public LocalThreePhaseCommitCohort onTransactionReady(@Nonnull DOMStoreWriteTransaction tx,
81 @Nullable Exception operationError) {
82 checkArgument(tx instanceof SnapshotBackedWriteTransaction);
83 if (operationError != null) {
84 return new LocalThreePhaseCommitCohort(actorUtils, leader,
85 (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, operationError);
88 return (LocalThreePhaseCommitCohort) tx.ready();