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;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 public class ShardWriteTransaction extends ShardTransaction {
43 private final MutableCompositeModification compositeModification = new MutableCompositeModification();
44 private final DOMStoreWriteTransaction transaction;
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;
54 protected DOMStoreTransaction getDOMStoreTransaction() {
59 public void handleReceive(Object message) throws Exception {
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);
70 } else if(MergeData.isSerializedType(message)) {
71 mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
73 } else if(DeleteData.isSerializedType(message)) {
74 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
76 } else if (message instanceof GetCompositedModification) {
77 // This is here for testing only
78 getSender().tell(new GetCompositeModificationReply(compositeModification), getSelf());
80 super.handleReceive(message);
84 private void batchedModifications(BatchedModifications batched) {
86 for(Modification modification: batched.getModifications()) {
87 compositeModification.addModification(modification);
88 modification.apply(transaction);
91 if(batched.isReady()) {
92 readyTransaction(transaction, false);
94 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
96 } catch (Exception e) {
97 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
101 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
102 boolean returnSerialized) {
103 LOG.debug("writeData at path : {}", message.getPath());
105 compositeModification.addModification(
106 new WriteModification(message.getPath(), message.getData()));
108 transaction.write(message.getPath(), message.getData());
109 WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
110 getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
111 writeDataReply, getSelf());
113 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
117 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
118 boolean returnSerialized) {
119 LOG.debug("mergeData at path : {}", message.getPath());
121 compositeModification.addModification(
122 new MergeModification(message.getPath(), message.getData()));
125 transaction.merge(message.getPath(), message.getData());
126 MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
127 getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
128 mergeDataReply, getSelf());
130 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
134 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
135 boolean returnSerialized) {
136 LOG.debug("deleteData at path : {}", message.getPath());
138 compositeModification.addModification(new DeleteModification(message.getPath()));
140 transaction.delete(message.getPath());
141 DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
142 getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
143 deleteDataReply, getSelf());
145 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
149 private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
150 String transactionID = getTransactionID();
152 LOG.debug("readyTransaction : {}", transactionID);
154 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
156 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
157 cohort, compositeModification, returnSerialized), getContext());
159 // The shard will handle the commit from here so we're no longer needed - self-destruct.
160 getSelf().tell(PoisonPill.getInstance(), getSelf());
163 // These classes are in here for test purposes only
165 static class GetCompositedModification {
168 static class GetCompositeModificationReply {
169 private final CompositeModification modification;
172 GetCompositeModificationReply(CompositeModification modification) {
173 this.modification = modification;
176 public CompositeModification getModification() {