78046bce9be123b423b6e50d61dd79cd83f9027f
[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 org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
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.SnapshotBackedTransactions;
21 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
25
26 /**
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.
30  */
31 final class LocalTransactionFactoryImpl extends TransactionReadyPrototype<TransactionIdentifier>
32         implements LocalTransactionFactory {
33
34     private final ActorSelection leader;
35     private final DataTree dataTree;
36     private final ActorUtils actorUtils;
37
38     LocalTransactionFactoryImpl(final ActorUtils actorUtils, final ActorSelection leader, final DataTree dataTree) {
39         this.leader = requireNonNull(leader);
40         this.dataTree = requireNonNull(dataTree);
41         this.actorUtils = actorUtils;
42     }
43
44     DataTree getDataTree() {
45         return dataTree;
46     }
47
48     @Override
49     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
50         return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
51     }
52
53     @Override
54     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
55         return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
56     }
57
58     @Override
59     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
60         return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
61     }
62
63     @Override
64     protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
65         // No-op
66     }
67
68     @Override
69     protected DOMStoreThreePhaseCommitCohort transactionReady(
70             final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
71             final DataTreeModification tree,
72             final Exception readyError) {
73         return new LocalThreePhaseCommitCohort(actorUtils, leader, tx, tree, readyError);
74     }
75
76     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
77     @Override
78     public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx, Exception operationError) {
79         checkArgument(tx instanceof SnapshotBackedWriteTransaction);
80         if (operationError != null) {
81             return new LocalThreePhaseCommitCohort(actorUtils, leader,
82                     (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, operationError);
83         }
84
85         return (LocalThreePhaseCommitCohort) tx.ready();
86     }
87 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.