Rename ActorContext to ActorUtils
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionFactoryImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
27
28 /**
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.
32  */
33 final class LocalTransactionFactoryImpl extends TransactionReadyPrototype<TransactionIdentifier>
34         implements LocalTransactionFactory {
35
36     private final ActorSelection leader;
37     private final DataTree dataTree;
38     private final ActorUtils actorUtils;
39
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;
44     }
45
46     DataTree getDataTree() {
47         return dataTree;
48     }
49
50     @Override
51     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
52         return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
53     }
54
55     @Override
56     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
57         return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
58     }
59
60     @Override
61     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
62         return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
63     }
64
65     @Override
66     protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
67         // No-op
68     }
69
70     @Override
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);
76     }
77
78     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
79     @Override
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);
86         }
87
88         return (LocalThreePhaseCommitCohort) tx.ready();
89     }
90 }