149b9370ecba75bd264e2f50c8d5cdfd89a4b39a
[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 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;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
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 static final Logger LOG = LoggerFactory.getLogger(LocalTransactionFactoryImpl.class);
35     private final ActorSelection leader;
36     private final DataTree dataTree;
37     private final ActorContext actorContext;
38
39     LocalTransactionFactoryImpl(final ActorContext actorContext, final ActorSelection leader, final DataTree dataTree) {
40         this.leader = Preconditions.checkNotNull(leader);
41         this.dataTree = Preconditions.checkNotNull(dataTree);
42         this.actorContext = actorContext;
43     }
44
45     DataTree getDataTree() {
46         return dataTree;
47     }
48
49     @Override
50     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
51         return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
52     }
53
54     @Override
55     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
56         return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
57     }
58
59     @Override
60     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
61         return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
62     }
63
64     @Override
65     protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
66         // No-op
67     }
68
69     @Override
70     protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
71             final DataTreeModification tree) {
72         return new LocalThreePhaseCommitCohort(actorContext, leader, tx, tree) {
73             @Override
74             protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
75                 // No-op
76                 LOG.debug("Transaction {} aborted", transaction);
77             }
78
79             @Override
80             protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
81                 // No-op
82                 LOG.debug("Transaction {} committed", transaction);
83             }
84         };
85     }
86 }

©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.