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.datastore.identifiers.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
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.SnapshotBackedTransactions;
21 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
27 * {@link LocalTransactionFactory} for instantiating backing transactions which are
28 * disconnected from each other, ie not chained. These are used by {@link AbstractTransactionContextFactory}
29 * to instantiate transactions on shards which are co-located with the shard leader.
31 final class LocalTransactionFactoryImpl extends TransactionReadyPrototype<TransactionIdentifier>
32 implements LocalTransactionFactory {
34 private final ActorSelection leader;
35 private final DataTree dataTree;
36 private final ActorContext actorContext;
38 LocalTransactionFactoryImpl(final ActorContext actorContext, final ActorSelection leader, final DataTree dataTree) {
39 this.leader = Preconditions.checkNotNull(leader);
40 this.dataTree = Preconditions.checkNotNull(dataTree);
41 this.actorContext = actorContext;
44 DataTree getDataTree() {
49 public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
50 return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
54 public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
55 return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
59 public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
60 return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
64 protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
69 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
70 final DataTreeModification tree) {
71 return new LocalThreePhaseCommitCohort(actorContext, leader, tx, tree);
74 @SuppressWarnings("unchecked")
76 public LocalThreePhaseCommitCohort onTransactionReady(@Nonnull DOMStoreWriteTransaction tx,
77 @Nullable Exception operationError) {
78 if(operationError != null) {
79 return new LocalThreePhaseCommitCohort(actorContext, leader,
80 (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, operationError);
84 return (LocalThreePhaseCommitCohort) tx.ready();
85 } catch (Exception e) {
86 // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
87 // LocalThreePhaseCommitCohort.
88 return new LocalThreePhaseCommitCohort(actorContext, leader,
89 (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);