Bug 3195: Cleanup on error paths and error handling
[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
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 final ActorSelection leader;
33     private final DataTree dataTree;
34     private final ActorContext actorContext;
35
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;
40     }
41
42     DataTree getDataTree() {
43         return dataTree;
44     }
45
46     @Override
47     public DOMStoreReadTransaction newReadOnlyTransaction(TransactionIdentifier identifier) {
48         return SnapshotBackedTransactions.newReadTransaction(identifier, false, dataTree.takeSnapshot());
49     }
50
51     @Override
52     public DOMStoreReadWriteTransaction newReadWriteTransaction(TransactionIdentifier identifier) {
53         return SnapshotBackedTransactions.newReadWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
54     }
55
56     @Override
57     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
58         return SnapshotBackedTransactions.newWriteTransaction(identifier, false, dataTree.takeSnapshot(), this);
59     }
60
61     @Override
62     protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx) {
63         // No-op
64     }
65
66     @Override
67     protected DOMStoreThreePhaseCommitCohort transactionReady(final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
68             final DataTreeModification tree) {
69         return new LocalThreePhaseCommitCohort(actorContext, leader, tx, tree);
70     }
71
72     @SuppressWarnings("unchecked")
73     @Override
74     public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx) {
75         try {
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);
82         }
83     }
84 }

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