Refactor snapshot message processing to RaftActorSnapshotMessageSupport
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardWriteTransaction.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.PoisonPill;
15 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
17 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
18 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
22 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
25 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
26 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
28 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.Modification;
30 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
31 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
35
36 /**
37  * @author: syedbahm
38  * Date: 8/6/14
39  */
40 public class ShardWriteTransaction extends ShardTransaction {
41
42     private final MutableCompositeModification compositeModification = new MutableCompositeModification();
43     private final DOMStoreWriteTransaction transaction;
44
45     public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
46             ShardStats shardStats, String transactionID, short clientTxVersion) {
47         super(shardActor, shardStats, transactionID, clientTxVersion);
48         this.transaction = transaction;
49     }
50
51     @Override
52     protected DOMStoreTransaction getDOMStoreTransaction() {
53         return transaction;
54     }
55
56     @Override
57     public void handleReceive(Object message) throws Exception {
58
59         if (message instanceof BatchedModifications) {
60             batchedModifications((BatchedModifications)message);
61         } else if (message instanceof ReadyTransaction) {
62             readyTransaction(transaction, !SERIALIZED_REPLY);
63         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
64             readyTransaction(transaction, SERIALIZED_REPLY);
65         } else if(WriteData.isSerializedType(message)) {
66             writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
67
68         } else if(MergeData.isSerializedType(message)) {
69             mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
70
71         } else if(DeleteData.isSerializedType(message)) {
72             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
73
74         } else if (message instanceof GetCompositedModification) {
75             // This is here for testing only
76             getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
77         } else {
78             super.handleReceive(message);
79         }
80     }
81
82     private void batchedModifications(BatchedModifications batched) {
83         try {
84             for(Modification modification: batched.getModifications()) {
85                 compositeModification.addModification(modification);
86                 modification.apply(transaction);
87             }
88
89             getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
90         } catch (Exception e) {
91             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
92         }
93     }
94
95     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
96             boolean returnSerialized) {
97         LOG.debug("writeData at path : {}", message.getPath());
98
99         compositeModification.addModification(
100                 new WriteModification(message.getPath(), message.getData()));
101         try {
102             transaction.write(message.getPath(), message.getData());
103             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
104             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
105                 writeDataReply, getSelf());
106         }catch(Exception e){
107             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
108         }
109     }
110
111     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
112             boolean returnSerialized) {
113         LOG.debug("mergeData at path : {}", message.getPath());
114
115         compositeModification.addModification(
116                 new MergeModification(message.getPath(), message.getData()));
117
118         try {
119             transaction.merge(message.getPath(), message.getData());
120             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
121             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
122                 mergeDataReply, getSelf());
123         }catch(Exception e){
124             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
125         }
126     }
127
128     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
129             boolean returnSerialized) {
130         LOG.debug("deleteData at path : {}", message.getPath());
131
132         compositeModification.addModification(new DeleteModification(message.getPath()));
133         try {
134             transaction.delete(message.getPath());
135             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
136             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
137                 deleteDataReply, getSelf());
138         }catch(Exception e){
139             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
140         }
141     }
142
143     private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
144         String transactionID = getTransactionID();
145
146         LOG.debug("readyTransaction : {}", transactionID);
147
148         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
149
150         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
151                 cohort, compositeModification, returnSerialized), getContext());
152
153         // The shard will handle the commit from here so we're no longer needed - self-destruct.
154         getSelf().tell(PoisonPill.getInstance(), getSelf());
155     }
156
157     // These classes are in here for test purposes only
158
159     static class GetCompositedModification {
160     }
161
162     static class GetCompositeModificationReply {
163         private final CompositeModification modification;
164
165
166         GetCompositeModificationReply(CompositeModification modification) {
167             this.modification = modification;
168         }
169
170         public CompositeModification getModification() {
171             return modification;
172         }
173     }
174 }