3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
11 package org.opendaylight.controller.cluster.datastore;
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;
40 public class ShardWriteTransaction extends ShardTransaction {
42 private final MutableCompositeModification compositeModification = new MutableCompositeModification();
43 private final DOMStoreWriteTransaction transaction;
45 public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
46 ShardStats shardStats, String transactionID, short clientTxVersion) {
47 super(shardActor, shardStats, transactionID, clientTxVersion);
48 this.transaction = transaction;
52 protected DOMStoreTransaction getDOMStoreTransaction() {
57 public void handleReceive(Object message) throws Exception {
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);
68 } else if(MergeData.isSerializedType(message)) {
69 mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
71 } else if(DeleteData.isSerializedType(message)) {
72 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
74 } else if (message instanceof GetCompositedModification) {
75 // This is here for testing only
76 getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
78 super.handleReceive(message);
82 private void batchedModifications(BatchedModifications batched) {
84 for(Modification modification: batched.getModifications()) {
85 compositeModification.addModification(modification);
86 modification.apply(transaction);
89 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
90 } catch (Exception e) {
91 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
95 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
96 boolean returnSerialized) {
97 LOG.debug("writeData at path : {}", message.getPath());
99 compositeModification.addModification(
100 new WriteModification(message.getPath(), message.getData()));
102 transaction.write(message.getPath(), message.getData());
103 WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
104 getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
105 writeDataReply, getSelf());
107 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
111 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
112 boolean returnSerialized) {
113 LOG.debug("mergeData at path : {}", message.getPath());
115 compositeModification.addModification(
116 new MergeModification(message.getPath(), message.getData()));
119 transaction.merge(message.getPath(), message.getData());
120 MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
121 getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
122 mergeDataReply, getSelf());
124 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
128 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
129 boolean returnSerialized) {
130 LOG.debug("deleteData at path : {}", message.getPath());
132 compositeModification.addModification(new DeleteModification(message.getPath()));
134 transaction.delete(message.getPath());
135 DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
136 getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
137 deleteDataReply, getSelf());
139 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
143 private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
144 String transactionID = getTransactionID();
146 LOG.debug("readyTransaction : {}", transactionID);
148 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
150 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
151 cohort, compositeModification, returnSerialized), getContext());
153 // The shard will handle the commit from here so we're no longer needed - self-destruct.
154 getSelf().tell(PoisonPill.getInstance(), getSelf());
157 // These classes are in here for test purposes only
159 static class GetCompositedModification {
162 static class GetCompositeModificationReply {
163 private final CompositeModification modification;
166 GetCompositeModificationReply(CompositeModification modification) {
167 this.modification = modification;
170 public CompositeModification getModification() {