BUG-5280: Remove PeristentMessages
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardReadTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import com.google.common.base.Optional;
14 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
15 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
16 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
18 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
19 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22
23 /**
24  * @author: syedbahm
25  * Date: 8/6/14
26  */
27 public class ShardReadTransaction extends ShardTransaction {
28     private final AbstractShardDataTreeTransaction<?> transaction;
29
30     public ShardReadTransaction(AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
31             ShardStats shardStats) {
32         super(shardActor, shardStats, transaction.getId());
33         this.transaction = transaction;
34     }
35
36     @Override
37     public void handleReceive(Object message) {
38         if (message instanceof CreateSnapshot) {
39             createSnapshot();
40         } else if(ReadData.isSerializedType(message)) {
41             readData(transaction, ReadData.fromSerializable(message));
42         } else if(DataExists.isSerializedType(message)) {
43             dataExists(transaction, DataExists.fromSerializable(message));
44         } else {
45             super.handleReceive(message);
46         }
47     }
48
49     private void createSnapshot() {
50
51         // This is a special message sent by the shard to send back a serialized snapshot of the whole
52         // data store tree. This transaction was created for that purpose only so we can
53         // self-destruct after sending the reply.
54
55         final ActorRef sender = getSender();
56         final ActorRef self = getSelf();
57         final Optional<NormalizedNode<?, ?>> result = transaction.getSnapshot().readNode(YangInstanceIdentifier.EMPTY);
58
59         byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
60         sender.tell(new CaptureSnapshotReply(serialized), self);
61
62         self.tell(PoisonPill.getInstance(), self);
63     }
64
65     @Override
66     protected AbstractShardDataTreeTransaction<?> getDOMStoreTransaction() {
67         return transaction;
68     }
69
70     @Override
71     protected boolean returnCloseTransactionReply() {
72         return false;
73     }
74 }