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.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;
38 public class ShardWriteTransaction extends ShardTransaction {
40 private final MutableCompositeModification modification = new MutableCompositeModification();
41 private final DOMStoreWriteTransaction transaction;
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;
51 protected DOMStoreTransaction getDOMStoreTransaction() {
56 public void handleReceive(Object message) throws Exception {
58 if (message instanceof WriteData) {
59 writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
61 } else if (message instanceof MergeData) {
62 mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
64 } else if (message instanceof DeleteData) {
65 deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
67 } else if (message instanceof ReadyTransaction) {
68 readyTransaction(transaction, !SERIALIZED_REPLY);
70 } else if(WriteData.isSerializedType(message)) {
71 writeData(transaction, WriteData.fromSerializable(message), SERIALIZED_REPLY);
73 } else if(MergeData.isSerializedType(message)) {
74 mergeData(transaction, MergeData.fromSerializable(message), SERIALIZED_REPLY);
76 } else if(DeleteData.isSerializedType(message)) {
77 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
79 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
80 readyTransaction(transaction, SERIALIZED_REPLY);
82 } else if (message instanceof GetCompositedModification) {
83 // This is here for testing only
84 getSender().tell(new GetCompositeModificationReply(modification), getSelf());
86 super.handleReceive(message);
90 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
91 boolean returnSerialized) {
92 LOG.debug("writeData at path : {}", message.getPath());
94 modification.addModification(
95 new WriteModification(message.getPath(), message.getData()));
97 transaction.write(message.getPath(), message.getData());
98 WriteDataReply writeDataReply = WriteDataReply.INSTANCE;
99 getSender().tell(returnSerialized ? writeDataReply.toSerializable(message.getVersion()) :
100 writeDataReply, getSelf());
102 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
106 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
107 boolean returnSerialized) {
108 LOG.debug("mergeData at path : {}", message.getPath());
110 modification.addModification(
111 new MergeModification(message.getPath(), message.getData()));
114 transaction.merge(message.getPath(), message.getData());
115 MergeDataReply mergeDataReply = MergeDataReply.INSTANCE;
116 getSender().tell(returnSerialized ? mergeDataReply.toSerializable(message.getVersion()) :
117 mergeDataReply, getSelf());
119 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
123 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
124 boolean returnSerialized) {
125 LOG.debug("deleteData at path : {}", message.getPath());
127 modification.addModification(new DeleteModification(message.getPath()));
129 transaction.delete(message.getPath());
130 DeleteDataReply deleteDataReply = DeleteDataReply.INSTANCE;
131 getSender().tell(returnSerialized ? deleteDataReply.toSerializable(message.getVersion()) :
132 deleteDataReply, getSelf());
134 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
138 private void readyTransaction(DOMStoreWriteTransaction transaction, boolean returnSerialized) {
139 String transactionID = getTransactionID();
141 LOG.debug("readyTransaction : {}", transactionID);
143 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
145 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getClientTxVersion(),
146 cohort, modification, returnSerialized), getContext());
148 // The shard will handle the commit from here so we're no longer needed - self-destruct.
149 getSelf().tell(PoisonPill.getInstance(), getSelf());
152 // These classes are in here for test purposes only
154 static class GetCompositedModification {
157 static class GetCompositeModificationReply {
158 private final CompositeModification modification;
161 GetCompositeModificationReply(CompositeModification modification) {
162 this.modification = modification;
165 public CompositeModification getModification() {