6a830a1ddbfd0a0645838cb9e7ed20c14ed976c7
[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 static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
29
30     private final AbstractShardDataTreeTransaction<?> transaction;
31
32     public ShardReadTransaction(AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
33             ShardStats shardStats, String transactionID, short clientTxVersion) {
34         super(shardActor, shardStats, transactionID, clientTxVersion);
35         this.transaction = transaction;
36     }
37
38     @Override
39     public void handleReceive(Object message) throws Exception {
40         if(message instanceof ReadData) {
41             readData(transaction, (ReadData) message, !SERIALIZED_REPLY);
42
43         } else if (message instanceof DataExists) {
44             dataExists(transaction, (DataExists) message, !SERIALIZED_REPLY);
45         } else if (message instanceof CreateSnapshot) {
46             createSnapshot();
47         } else if(ReadData.SERIALIZABLE_CLASS.equals(message.getClass())) {
48             readData(transaction, ReadData.fromSerializable(message), SERIALIZED_REPLY);
49
50         } else if(DataExists.SERIALIZABLE_CLASS.equals(message.getClass())) {
51             dataExists(transaction, DataExists.fromSerializable(message), SERIALIZED_REPLY);
52
53         } else {
54             super.handleReceive(message);
55         }
56     }
57
58     private void createSnapshot() {
59
60         // This is a special message sent by the shard to send back a serialized snapshot of the whole
61         // data store tree. This transaction was created for that purpose only so we can
62         // self-destruct after sending the reply.
63
64         final ActorRef sender = getSender();
65         final ActorRef self = getSelf();
66         final Optional<NormalizedNode<?, ?>> result = transaction.getSnapshot().readNode(DATASTORE_ROOT);
67
68         byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
69         sender.tell(new CaptureSnapshotReply(serialized), self);
70
71         self.tell(PoisonPill.getInstance(), self);
72     }
73
74     @Override
75     protected AbstractShardDataTreeTransaction<?> getDOMStoreTransaction() {
76         return transaction;
77     }
78
79     @Override
80     protected boolean returnCloseTransactionReply() {
81         return false;
82     }
83 }