f2c77e32d87f3bf12e841e6f25fef15c6191e7bb
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardReadTransaction.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.PoisonPill;
15 import com.google.common.base.Optional;
16 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
17 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
21 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 /**
26  * @author: syedbahm
27  * Date: 8/6/14
28  */
29 public class ShardReadTransaction extends ShardTransaction {
30     private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
31
32     private final AbstractShardDataTreeTransaction<?> transaction;
33
34     public ShardReadTransaction(AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
35             ShardStats shardStats, String transactionID, short clientTxVersion) {
36         super(shardActor, shardStats, transactionID, clientTxVersion);
37         this.transaction = transaction;
38     }
39
40     @Override
41     public void handleReceive(Object message) throws Exception {
42         if(message instanceof ReadData) {
43             readData(transaction, (ReadData) message, !SERIALIZED_REPLY);
44
45         } else if (message instanceof DataExists) {
46             dataExists(transaction, (DataExists) message, !SERIALIZED_REPLY);
47         } else if (message instanceof CreateSnapshot) {
48             createSnapshot();
49         } else if(ReadData.SERIALIZABLE_CLASS.equals(message.getClass())) {
50             readData(transaction, ReadData.fromSerializable(message), SERIALIZED_REPLY);
51
52         } else if(DataExists.SERIALIZABLE_CLASS.equals(message.getClass())) {
53             dataExists(transaction, DataExists.fromSerializable(message), SERIALIZED_REPLY);
54
55         } else {
56             super.handleReceive(message);
57         }
58     }
59
60     private void createSnapshot() {
61
62         // This is a special message sent by the shard to send back a serialized snapshot of the whole
63         // data store tree. This transaction was created for that purpose only so we can
64         // self-destruct after sending the reply.
65
66         final ActorRef sender = getSender();
67         final ActorRef self = getSelf();
68         final Optional<NormalizedNode<?, ?>> result = transaction.getSnapshot().readNode(DATASTORE_ROOT);
69
70         byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
71         sender.tell(new CaptureSnapshotReply(serialized), self);
72
73         self.tell(PoisonPill.getInstance(), self);
74     }
75
76     @Override
77     protected AbstractShardDataTreeTransaction<?> getDOMStoreTransaction() {
78         return transaction;
79     }
80
81     @Override
82     protected boolean returnCloseTransactionReply() {
83         return false;
84     }
85 }