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 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;
29 public class ShardReadTransaction extends ShardTransaction {
30 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
32 private final AbstractShardDataTreeTransaction<?> transaction;
34 public ShardReadTransaction(AbstractShardDataTreeTransaction<?> transaction, ActorRef shardActor,
35 ShardStats shardStats, String transactionID, short clientTxVersion) {
36 super(shardActor, shardStats, transactionID, clientTxVersion);
37 this.transaction = transaction;
41 public void handleReceive(Object message) throws Exception {
42 if(message instanceof ReadData) {
43 readData(transaction, (ReadData) message, !SERIALIZED_REPLY);
45 } else if (message instanceof DataExists) {
46 dataExists(transaction, (DataExists) message, !SERIALIZED_REPLY);
47 } else if (message instanceof CreateSnapshot) {
49 } else if(ReadData.SERIALIZABLE_CLASS.equals(message.getClass())) {
50 readData(transaction, ReadData.fromSerializable(message), SERIALIZED_REPLY);
52 } else if(DataExists.SERIALIZABLE_CLASS.equals(message.getClass())) {
53 dataExists(transaction, DataExists.fromSerializable(message), SERIALIZED_REPLY);
56 super.handleReceive(message);
60 private void createSnapshot() {
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.
66 final ActorRef sender = getSender();
67 final ActorRef self = getSelf();
68 final Optional<NormalizedNode<?, ?>> result = transaction.getSnapshot().readNode(DATASTORE_ROOT);
70 byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
71 sender.tell(new CaptureSnapshotReply(serialized), self);
73 self.tell(PoisonPill.getInstance(), self);
77 protected AbstractShardDataTreeTransaction<?> getDOMStoreTransaction() {
82 protected boolean returnCloseTransactionReply() {