Bug 2597: Batch modification operations in TransactionProxy
[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 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 /**
38  * @author: syedbahm
39  * Date: 8/6/14
40  */
41 public class ShardWriteTransaction extends ShardTransaction {
42
43     private final MutableCompositeModification compositeModification = new MutableCompositeModification();
44     private final DOMStoreWriteTransaction transaction;
45
46     public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
47             SchemaContext schemaContext, ShardStats shardStats, String transactionID,
48             short clientTxVersion) {
49         super(shardActor, schemaContext, shardStats, transactionID, clientTxVersion);
50         this.transaction = transaction;
51     }
52
53     @Override
54     protected DOMStoreTransaction getDOMStoreTransaction() {
55         return transaction;
56     }
57
58     @Override
59     public void handleReceive(Object message) throws Exception {
60
61         if (message instanceof BatchedModifications) {
62             batchedModifications((BatchedModifications)message);
63         } else if (message instanceof ReadyTransaction) {
64             readyTransaction(transaction, !SERIALIZED_REPLY);
65         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
66             readyTransaction(transaction, SERIALIZED_REPLY);
67         } else if(WriteData.isSerializedType(message)) {
68             writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
69
70         } else if(MergeData.isSerializedType(message)) {
71             mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
72
73         } else if(DeleteData.isSerializedType(message)) {
74             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
75
76         } else if (message instanceof GetCompositedModification) {
77             // This is here for testing only
78             getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
79         } else {
80             super.handleReceive(message);
81         }
82     }
83
84     private void batchedModifications(BatchedModifications batched) {
85         try {
86             for(Modification modification: batched.getModifications()) {
87                 compositeModification.addModification(modification);
88                 modification.apply(transaction);
89             }
90
91             getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
92         } catch (Exception e) {
93             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
94         }
95     }
96
97     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
98             boolean returnSerialized) {
99         LOG.debug("writeData at path : {}", message.getPath());
100
101         compositeModification.addModification(
102                 new WriteModification(message.getPath(), message.getData()));
103         try {
104             transaction.write(message.getPath(), message.getData());
105             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
106             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
107                 writeDataReply, getSelf());
108         }catch(Exception e){
109             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
110         }
111     }
112
113     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
114             boolean returnSerialized) {
115         LOG.debug("mergeData at path : {}", message.getPath());
116
117         compositeModification.addModification(
118                 new MergeModification(message.getPath(), message.getData()));
119
120         try {
121             transaction.merge(message.getPath(), message.getData());
122             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
123             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
124                 mergeDataReply, getSelf());
125         }catch(Exception e){
126             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
127         }
128     }
129
130     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
131             boolean returnSerialized) {
132         LOG.debug("deleteData at path : {}", message.getPath());
133
134         compositeModification.addModification(new DeleteModification(message.getPath()));
135         try {
136             transaction.delete(message.getPath());
137             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
138             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
139                 deleteDataReply, getSelf());
140         }catch(Exception e){
141             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
142         }
143     }
144
145     private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
146         String transactionID = getTransactionID();
147
148         LOG.debug("readyTransaction : {}", transactionID);
149
150         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
151
152         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
153                 cohort, compositeModification, returnSerialized), getContext());
154
155         // The shard will handle the commit from here so we're no longer needed - self-destruct.
156         getSelf().tell(PoisonPill.getInstance(), getSelf());
157     }
158
159     // These classes are in here for test purposes only
160
161     static class GetCompositedModification {
162     }
163
164     static class GetCompositeModificationReply {
165         private final CompositeModification modification;
166
167
168         GetCompositeModificationReply(CompositeModification modification) {
169             this.modification = modification;
170         }
171
172         public CompositeModification getModification() {
173             return modification;
174         }
175     }
176 }