Merge "neutron now works with jetty"
[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.DeleteData;
17 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
20 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
23 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
24 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
25 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
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             short clientTxVersion) {
46         super(shardActor, schemaContext, shardStats, transactionID, clientTxVersion);
47         this.transaction = transaction;
48     }
49
50     @Override
51     protected DOMStoreTransaction getDOMStoreTransaction() {
52         return transaction;
53     }
54
55     @Override
56     public void handleReceive(Object message) throws Exception {
57
58         if (message instanceof WriteData) {
59             writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
60
61         } else if (message instanceof MergeData) {
62             mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
63
64         } else if (message instanceof DeleteData) {
65             deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
66
67         } else if (message instanceof ReadyTransaction) {
68             readyTransaction(transaction, !SERIALIZED_REPLY);
69
70         } else if(WriteData.isSerializedType(message)) {
71             writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
72
73         } else if(MergeData.isSerializedType(message)) {
74             mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
75
76         } else if(DeleteData.isSerializedType(message)) {
77             deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
78
79         } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
80             readyTransaction(transaction, SERIALIZED_REPLY);
81
82         } else if (message instanceof GetCompositedModification) {
83             // This is here for testing only
84             getSender().tell(new GetCompositeModificationReply(modification), getSelf());
85         } else {
86             super.handleReceive(message);
87         }
88     }
89
90     private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
91             boolean returnSerialized) {
92         LOG.debug("writeData at path : {}", message.getPath());
93
94         modification.addModification(
95                 new WriteModification(message.getPath(), message.getData()));
96         try {
97             transaction.write(message.getPath(), message.getData());
98             WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
99             getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
100                 writeDataReply, 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,
107             boolean returnSerialized) {
108         LOG.debug("mergeData at path : {}", message.getPath());
109
110         modification.addModification(
111                 new MergeModification(message.getPath(), message.getData()));
112
113         try {
114             transaction.merge(message.getPath(), message.getData());
115             MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
116             getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
117                 mergeDataReply, getSelf());
118         }catch(Exception e){
119             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
120         }
121     }
122
123     private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
124             boolean returnSerialized) {
125         LOG.debug("deleteData at path : {}", message.getPath());
126
127         modification.addModification(new DeleteModification(message.getPath()));
128         try {
129             transaction.delete(message.getPath());
130             DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
131             getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
132                 deleteDataReply, getSelf());
133         }catch(Exception e){
134             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
135         }
136     }
137
138     private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
139         String transactionID = getTransactionID();
140
141         LOG.debug("readyTransaction : {}", transactionID);
142
143         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
144
145         getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
146                 cohort, modification, returnSerialized), getContext());
147
148         // The shard will handle the commit from here so we're no longer needed - self-destruct.
149         getSelf().tell(PoisonPill.getInstance(), getSelf());
150     }
151
152     // These classes are in here for test purposes only
153
154     static class GetCompositedModification {
155     }
156
157     static class GetCompositeModificationReply {
158         private final CompositeModification modification;
159
160
161         GetCompositeModificationReply(CompositeModification modification) {
162             this.modification = modification;
163         }
164
165         public CompositeModification getModification() {
166             return modification;
167         }
168     }
169 }