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 int txnClientVersion) {
47 super(shardActor, schemaContext, shardStats, transactionID, txnClientVersion);
48 this.transaction = transaction;
52 protected DOMStoreTransaction getDOMStoreTransaction() {
57 public void handleReceive(Object message) throws Exception {
59 if (message instanceof WriteData) {
60 writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
62 } else if (message instanceof MergeData) {
63 mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
65 } else if (message instanceof DeleteData) {
66 deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
68 } else if (message instanceof ReadyTransaction) {
69 readyTransaction(transaction, new ReadyTransaction(), !SERIALIZED_REPLY);
71 } else if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
72 writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
74 } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
75 mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
77 } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
78 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
80 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
81 readyTransaction(transaction, new ReadyTransaction(), SERIALIZED_REPLY);
83 } else if (message instanceof GetCompositedModification) {
84 // This is here for testing only
85 getSender().tell(new GetCompositeModificationReply(
86 new ImmutableCompositeModification(modification)), getSelf());
88 super.handleReceive(message);
92 private void writeData(DOMStoreWriteTransaction transaction, WriteData message,
93 boolean returnSerialized) {
94 LOG.debug("writeData at path : {}", message.getPath());
96 modification.addModification(
97 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
99 transaction.write(message.getPath(), message.getData());
100 WriteDataReply writeDataReply = new WriteDataReply();
101 getSender().tell(returnSerialized ? writeDataReply.toSerializable() : writeDataReply,
104 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
108 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message,
109 boolean returnSerialized) {
110 LOG.debug("mergeData at path : {}", message.getPath());
112 modification.addModification(
113 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
116 transaction.merge(message.getPath(), message.getData());
117 MergeDataReply mergeDataReply = new MergeDataReply();
118 getSender().tell(returnSerialized ? mergeDataReply.toSerializable() : mergeDataReply ,
121 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
125 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message,
126 boolean returnSerialized) {
127 LOG.debug("deleteData at path : {}", message.getPath());
129 modification.addModification(new DeleteModification(message.getPath()));
131 transaction.delete(message.getPath());
132 DeleteDataReply deleteDataReply = new DeleteDataReply();
133 getSender().tell(returnSerialized ? deleteDataReply.toSerializable() : deleteDataReply,
136 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
140 private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message,
141 boolean returnSerialized) {
142 String transactionID = getTransactionID();
144 LOG.debug("readyTransaction : {}", transactionID);
146 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
148 getShardActor().forward(new ForwardedReadyTransaction(transactionID, getTxnClientVersion(),
149 cohort, modification, returnSerialized), getContext());
151 // The shard will handle the commit from here so we're no longer needed - self-destruct.
152 getSelf().tell(PoisonPill.getInstance(), getSelf());
155 // These classes are in here for test purposes only
157 static class GetCompositedModification {
160 static class GetCompositeModificationReply {
161 private final CompositeModification modification;
164 GetCompositeModificationReply(CompositeModification modification) {
165 this.modification = modification;
168 public CompositeModification getModification() {