CDS: Implement front-end support for local transactions
[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.DOMStoreReadWriteTransaction;
15 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
16 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedTransactions;
17 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
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.
28  */
29 final class LocalTransactionFactoryImpl extends TransactionReadyPrototype<TransactionIdentifier>
30         implements LocalTransactionFactory {
31
32     private static final Logger LOG = LoggerFactory.getLogger(LocalTransactionFactoryImpl.class);
33     private final ActorSelection leader;
34     private final DataTree dataTree;
35     private final ActorContext actorContext;
36
37     LocalTransactionFactoryImpl(final ActorContext actorContext, final ActorSelection leader, final DataTree dataTree) {
38         this.leader = Preconditions.checkNotNull(leader);
39         this.dataTree = Preconditions.checkNotNull(dataTree);
40         this.actorContext = actorContext;
41     }
42
43     DataTree getDataTree() {
44         return dataTree;
45     }
46
47     @Override
48     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
49         return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
50     }
51
52     @Override
53     protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
54         // No-op
55     }
56
57     @Override
58     protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
59             final DataTreeModification tree) {
60         return new LocalThreePhaseCommitCohort(actorContext, leader, tx, tree) {
61             @Override
62             protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
63                 // No-op
64                 LOG.debug("Transaction {} aborted", transaction);
65             }
66
67             @Override
68             protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
69                 // No-op
70                 LOG.debug("Transaction {} committed", transaction);
71             }
72         };
73     }
74 }

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