356891164628bdfd8434b620707452ffa89bf7d5
[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             if(batched.isReady()) {
92                 readyTransaction(transaction, false);
93             } else {
94                 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
95             }
96         } catch (Exception e) {
97             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
98         }
99     }
100
101     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
102             boolean returnSerialized) {
103         LOG.debug("writeData at path : {}", message.getPath());
104
105         compositeModification.addModification(
106                 new WriteModification(message.getPath(), message.getData()));
107         try {
108             transaction.write(message.getPath(), message.getData());
109             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
110             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
111                 writeDataReply, getSelf());
112         }catch(Exception e){
113             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
114         }
115     }
116
117     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
118             boolean returnSerialized) {
119         LOG.debug("mergeData at path : {}", message.getPath());
120
121         compositeModification.addModification(
122                 new MergeModification(message.getPath(), message.getData()));
123
124         try {
125             transaction.merge(message.getPath(), message.getData());
126             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
127             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
128                 mergeDataReply, getSelf());
129         }catch(Exception e){
130             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
131         }
132     }
133
134     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
135             boolean returnSerialized) {
136         LOG.debug("deleteData at path : {}", message.getPath());
137
138         compositeModification.addModification(new DeleteModification(message.getPath()));
139         try {
140             transaction.delete(message.getPath());
141             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
142             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
143                 deleteDataReply, getSelf());
144         }catch(Exception e){
145             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
146         }
147     }
148
149     private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
150         String transactionID = getTransactionID();
151
152         LOG.debug("readyTransaction : {}", transactionID);
153
154         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
155
156         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
157                 cohort, compositeModification, returnSerialized), getContext());
158
159         // The shard will handle the commit from here so we're no longer needed - self-destruct.
160         getSelf().tell(PoisonPill.getInstance(), getSelf());
161     }
162
163     // These classes are in here for test purposes only
164
165     static class GetCompositedModification {
166     }
167
168     static class GetCompositeModificationReply {
169         private final CompositeModification modification;
170
171
172         GetCompositeModificationReply(CompositeModification modification) {
173             this.modification = modification;
174         }
175
176         public CompositeModification getModification() {
177             return modification;
178         }
179     }
180 }