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.cluster.datastore.utils.ActorContext;
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.SnapshotBackedTransactions;
19 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25 * {@link LocalTransactionFactory} for instantiating backing transactions which are
26 * disconnected from each other, ie not chained. These are used by {@link AbstractTransactionContextFactory}
27 * to instantiate transactions on shards which are co-located with the shard leader.
29 final class LocalTransactionFactoryImpl extends TransactionReadyPrototype<TransactionIdentifier>
30 implements LocalTransactionFactory {
32 private final ActorSelection leader;
33 private final DataTree dataTree;
34 private final ActorContext actorContext;
36 LocalTransactionFactoryImpl(final ActorContext actorContext, final ActorSelection leader, final DataTree dataTree) {
37 this.leader = Preconditions.checkNotNull(leader);
38 this.dataTree = Preconditions.checkNotNull(dataTree);
39 this.actorContext = actorContext;
42 DataTree getDataTree() {
47 public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
48 return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
52 public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
53 return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
57 public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
58 return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
62 protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
67 protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
68 final DataTreeModification tree) {
69 return new LocalThreePhaseCommitCohort(actorContext, leader, tx, tree);
72 @SuppressWarnings("unchecked")
74 public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx) {
76 return (LocalThreePhaseCommitCohort) tx.ready();
77 } catch (Exception e) {
78 // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
79 // LocalThreePhaseCommitCohort.
80 return new LocalThreePhaseCommitCohort(actorContext, leader,
81 (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);