21c210daf252fc4633b12882bb18dfea99779aa5
[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 org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
15 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
16 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
17 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
19 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
23 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
25 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
26 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
28 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 /**
35  * @author: syedbahm
36  * Date: 8/6/14
37  */
38 public class ShardWriteTransaction extends ShardTransaction {
39
40     private final MutableCompositeModification modification = new MutableCompositeModification();
41     private final DOMStoreWriteTransaction transaction;
42
43     public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
44             SchemaContext schemaContext, ShardStats shardStats, String transactionID) {
45         super(shardActor, schemaContext, shardStats, transactionID);
46         this.transaction = transaction;
47     }
48
49     @Override
50     protected DOMStoreTransaction getDOMStoreTransaction() {
51         return transaction;
52     }
53
54     @Override
55     public void handleReceive(Object message) throws Exception {
56
57         if (message instanceof WriteData) {
58             writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
59
60         } else if (message instanceof MergeData) {
61             mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
62
63         } else if (message instanceof DeleteData) {
64             deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
65
66         } else if (message instanceof ReadyTransaction) {
67             readyTransaction(transaction, new ReadyTransaction(), !SERIALIZED_REPLY);
68
69         } else if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
70             writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
71
72         } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
73             mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
74
75         } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
76             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
77
78         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
79             readyTransaction(transaction, new ReadyTransaction(), SERIALIZED_REPLY);
80
81         } else if (message instanceof GetCompositedModification) {
82             // This is here for testing only
83             getSender().tell(new GetCompositeModificationReply(
84                     new ImmutableCompositeModification(modification)), getSelf());
85         } else {
86             super.handleReceive(message);
87         }
88     }
89
90     private void writeData(DOMStoreWriteTransaction transaction, WriteData message, boolean returnSerialized) {
91         modification.addModification(
92                 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
93         if(LOG.isDebugEnabled()) {
94             LOG.debug("writeData at path : " + message.getPath().toString());
95         }
96         try {
97             transaction.write(message.getPath(), message.getData());
98             WriteDataReply writeDataReply = new WriteDataReply();
99             getSender().tell(returnSerialized ? writeDataReply.toSerializable() : writeDataReply,
100                 getSelf());
101         }catch(Exception e){
102             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
103         }
104     }
105
106     private void mergeData(DOMStoreWriteTransaction transaction, MergeData message, boolean returnSerialized) {
107         modification.addModification(
108                 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
109         if(LOG.isDebugEnabled()) {
110             LOG.debug("mergeData at path : " + message.getPath().toString());
111         }
112         try {
113             transaction.merge(message.getPath(), message.getData());
114             MergeDataReply mergeDataReply = new MergeDataReply();
115             getSender().tell(returnSerialized ? mergeDataReply.toSerializable() : mergeDataReply ,
116                 getSelf());
117         }catch(Exception e){
118             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
119         }
120     }
121
122     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message, boolean returnSerialized) {
123         if(LOG.isDebugEnabled()) {
124             LOG.debug("deleteData at path : " + message.getPath().toString());
125         }
126         modification.addModification(new DeleteModification(message.getPath()));
127         try {
128             transaction.delete(message.getPath());
129             DeleteDataReply deleteDataReply = new DeleteDataReply();
130             getSender().tell(returnSerialized ? deleteDataReply.toSerializable() : deleteDataReply,
131                 getSelf());
132         }catch(Exception e){
133             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
134         }
135     }
136
137     private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message, boolean returnSerialized) {
138         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
139
140         getShardActor().forward(new ForwardedReadyTransaction(
141             getTransactionID(), cohort, modification, returnSerialized),
142                 getContext());
143     }
144
145     // These classes are in here for test purposes only
146
147     static class GetCompositedModification {
148     }
149
150     static class GetCompositeModificationReply {
151         private final CompositeModification modification;
152
153
154         GetCompositeModificationReply(CompositeModification modification) {
155             this.modification = modification;
156         }
157
158         public CompositeModification getModification() {
159             return modification;
160         }
161     }
162 }