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.ImmutableCompositeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
28 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
29 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class ShardWriteTransaction extends ShardTransaction {
41 private final MutableCompositeModification modification = new MutableCompositeModification();
42 private final DOMStoreWriteTransaction transaction;
44 public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor,
45 SchemaContext schemaContext, ShardStats shardStats, String transactionID) {
46 super(shardActor, schemaContext, shardStats, transactionID);
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, new ReadyTransaction(), !SERIALIZED_REPLY);
70 } else if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
71 writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
73 } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
74 mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
76 } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
77 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
79 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
80 readyTransaction(transaction, new ReadyTransaction(), SERIALIZED_REPLY);
82 } else if (message instanceof GetCompositedModification) {
83 // This is here for testing only
84 getSender().tell(new GetCompositeModificationReply(
85 new ImmutableCompositeModification(modification)), getSelf());
87 super.handleReceive(message);
91 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
92 boolean returnSerialized) {
93 LOG.debug("writeData at path : {}", message.getPath());
95 modification.addModification(
96 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
98 transaction.write(message.getPath(), message.getData());
99 WriteDataReply writeDataReply = new WriteDataReply();
100 getSender().tell(returnSerialized ? writeDataReply.toSerializable() : writeDataReply,
103 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
107 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
108 boolean returnSerialized) {
109 LOG.debug("mergeData at path : {}", message.getPath());
111 modification.addModification(
112 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
115 transaction.merge(message.getPath(), message.getData());
116 MergeDataReply mergeDataReply = new MergeDataReply();
117 getSender().tell(returnSerialized ? mergeDataReply.toSerializable() : mergeDataReply ,
120 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
124 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
125 boolean returnSerialized) {
126 LOG.debug("deleteData at path : {}", message.getPath());
128 modification.addModification(new DeleteModification(message.getPath()));
130 transaction.delete(message.getPath());
131 DeleteDataReply deleteDataReply = new DeleteDataReply();
132 getSender().tell(returnSerialized ? deleteDataReply.toSerializable() : deleteDataReply,
135 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
139 private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message,
140 boolean returnSerialized) {
141 String transactionID = getTransactionID();
143 LOG.debug("readyTransaction : {}", transactionID);
145 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
147 getShardActor().forward(new ForwardedReadyTransaction(transactionID, cohort, modification,
148 returnSerialized), getContext());
150 // The shard will handle the commit from here so we're no longer needed - self-destruct.
151 getSelf().tell(PoisonPill.getInstance(), getSelf());
154 // These classes are in here for test purposes only
156 static class GetCompositedModification {
159 static class GetCompositeModificationReply {
160 private final CompositeModification modification;
163 GetCompositeModificationReply(CompositeModification modification) {
164 this.modification = modification;
167 public CompositeModification getModification() {