Simplify code using Java 8 features
[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 javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
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 ActorContext actorContext;
37
38     LocalTransactionFactoryImpl(final ActorContext actorContext, final ActorSelection leader, final DataTree dataTree) {
39         this.leader = Preconditions.checkNotNull(leader);
40         this.dataTree = Preconditions.checkNotNull(dataTree);
41         this.actorContext = actorContext;
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(actorContext, leader, tx, tree, readyError);
74     }
75
76     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
77     @Override
78     public LocalThreePhaseCommitCohort onTransactionReady(@Nonnull DOMStoreWriteTransaction tx,
79             @Nullable Exception operationError) {
80         Preconditions.checkArgument(tx instanceof SnapshotBackedWriteTransaction);
81         if (operationError != null) {
82             return new LocalThreePhaseCommitCohort(actorContext, leader,
83                     (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, operationError);
84         }
85
86         return (LocalThreePhaseCommitCohort) tx.ready();
87     }
88 }