Move byte-based serialization method
[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.DataTreeModification;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.ReadOnlyDataTree;
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 ReadOnlyDataTree dataTree;
36     private final ActorUtils actorUtils;
37
38     LocalTransactionFactoryImpl(final ActorUtils actorUtils, final ActorSelection leader,
39             final ReadOnlyDataTree dataTree) {
40         this.leader = requireNonNull(leader);
41         this.dataTree = requireNonNull(dataTree);
42         this.actorUtils = actorUtils;
43     }
44
45     ReadOnlyDataTree 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(
71             final SnapshotBackedWriteTransaction<TransactionIdentifier> tx,
72             final DataTreeModification tree,
73             final Exception readyError) {
74         return new LocalThreePhaseCommitCohort(actorUtils, leader, tx, tree, readyError);
75     }
76
77     @SuppressWarnings({"unchecked", "checkstyle:IllegalCatch"})
78     @Override
79     public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx, Exception operationError) {
80         checkArgument(tx instanceof SnapshotBackedWriteTransaction);
81         if (operationError != null) {
82             return new LocalThreePhaseCommitCohort(actorUtils, leader,
83                     (SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, operationError);
84         }
85
86         return (LocalThreePhaseCommitCohort) tx.ready();
87     }
88 }