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 org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
15 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
16 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
17 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
19 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
23 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
25 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
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 super(shardActor, schemaContext, shardStats, transactionID);
46 this.transaction = transaction;
50 protected DOMStoreTransaction getDOMStoreTransaction() {
55 public void handleReceive(Object message) throws Exception {
57 if (message instanceof WriteData) {
58 writeData(transaction, (WriteData) message, !SERIALIZED_REPLY);
60 } else if (message instanceof MergeData) {
61 mergeData(transaction, (MergeData) message, !SERIALIZED_REPLY);
63 } else if (message instanceof DeleteData) {
64 deleteData(transaction, (DeleteData) message, !SERIALIZED_REPLY);
66 } else if (message instanceof ReadyTransaction) {
67 readyTransaction(transaction, new ReadyTransaction(), !SERIALIZED_REPLY);
69 } else if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
70 writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
72 } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
73 mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()), SERIALIZED_REPLY);
75 } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
76 deleteData(transaction, DeleteData.fromSerializable(message), SERIALIZED_REPLY);
78 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
79 readyTransaction(transaction, new ReadyTransaction(), SERIALIZED_REPLY);
81 } else if (message instanceof GetCompositedModification) {
82 // This is here for testing only
83 getSender().tell(new GetCompositeModificationReply(
84 new ImmutableCompositeModification(modification)), getSelf());
86 super.handleReceive(message);
90 private void writeData(DOMStoreWriteTransaction transaction, WriteData message, boolean returnSerialized) {
91 modification.addModification(
92 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
93 if(LOG.isDebugEnabled()) {
94 LOG.debug("writeData at path : " + message.getPath().toString());
97 transaction.write(message.getPath(), message.getData());
98 WriteDataReply writeDataReply = new WriteDataReply();
99 getSender().tell(returnSerialized ? writeDataReply.toSerializable() : writeDataReply,
102 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
106 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message, boolean returnSerialized) {
107 modification.addModification(
108 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
109 if(LOG.isDebugEnabled()) {
110 LOG.debug("mergeData at path : " + message.getPath().toString());
113 transaction.merge(message.getPath(), message.getData());
114 MergeDataReply mergeDataReply = new MergeDataReply();
115 getSender().tell(returnSerialized ? mergeDataReply.toSerializable() : mergeDataReply ,
118 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
122 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message, boolean returnSerialized) {
123 if(LOG.isDebugEnabled()) {
124 LOG.debug("deleteData at path : " + message.getPath().toString());
126 modification.addModification(new DeleteModification(message.getPath()));
128 transaction.delete(message.getPath());
129 DeleteDataReply deleteDataReply = new DeleteDataReply();
130 getSender().tell(returnSerialized ? deleteDataReply.toSerializable() : deleteDataReply,
133 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
137 private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message, boolean returnSerialized) {
138 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
140 getShardActor().forward(new ForwardedReadyTransaction(
141 getTransactionID(), cohort, modification, returnSerialized),
145 // These classes are in here for test purposes only
147 static class GetCompositedModification {
150 static class GetCompositeModificationReply {
151 private final CompositeModification modification;
154 GetCompositeModificationReply(CompositeModification modification) {
155 this.modification = modification;
158 public CompositeModification getModification() {