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 {
56 if(WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
57 writeData(transaction, WriteData.fromSerializable(message, getSchemaContext()));
58 } else if(MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
59 mergeData(transaction, MergeData.fromSerializable(message, getSchemaContext()));
60 } else if(DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
61 deleteData(transaction, DeleteData.fromSerializable(message));
62 } else if(ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
63 readyTransaction(transaction, new ReadyTransaction());
64 } else if (message instanceof GetCompositedModification) {
65 // This is here for testing only
66 getSender().tell(new GetCompositeModificationReply(
67 new ImmutableCompositeModification(modification)), getSelf());
69 super.handleReceive(message);
73 private void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
74 modification.addModification(
75 new WriteModification(message.getPath(), message.getData(), getSchemaContext()));
76 if(LOG.isDebugEnabled()) {
77 LOG.debug("writeData at path : " + message.getPath().toString());
80 transaction.write(message.getPath(), message.getData());
81 getSender().tell(new WriteDataReply().toSerializable(), getSelf());
83 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
87 private void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
88 modification.addModification(
89 new MergeModification(message.getPath(), message.getData(), getSchemaContext()));
90 if(LOG.isDebugEnabled()) {
91 LOG.debug("mergeData at path : " + message.getPath().toString());
94 transaction.merge(message.getPath(), message.getData());
95 getSender().tell(new MergeDataReply().toSerializable(), getSelf());
97 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
101 private void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
102 if(LOG.isDebugEnabled()) {
103 LOG.debug("deleteData at path : " + message.getPath().toString());
105 modification.addModification(new DeleteModification(message.getPath()));
107 transaction.delete(message.getPath());
108 getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
110 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
114 private void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
115 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
117 getShardActor().forward(new ForwardedReadyTransaction(getTransactionID(), cohort, modification),
121 // These classes are in here for test purposes only
123 static class GetCompositedModification {
126 static class GetCompositeModificationReply {
127 private final CompositeModification modification;
130 GetCompositeModificationReply(CompositeModification modification) {
131 this.modification = modification;
134 public CompositeModification getModification() {