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 com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateSnapshot;
21 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
22 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
23 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
24 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 public class ShardReadTransaction extends ShardTransaction {
35 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
37 private final DOMStoreReadTransaction transaction;
39 public ShardReadTransaction(DOMStoreReadTransaction transaction, ActorRef shardActor,
40 ShardStats shardStats, String transactionID, short clientTxVersion) {
41 super(shardActor, shardStats, transactionID, clientTxVersion);
42 this.transaction = transaction;
46 public void handleReceive(Object message) throws Exception {
47 if(message instanceof ReadData) {
48 readData(transaction, (ReadData) message, !SERIALIZED_REPLY);
50 } else if (message instanceof DataExists) {
51 dataExists(transaction, (DataExists) message, !SERIALIZED_REPLY);
52 } else if (message instanceof CreateSnapshot) {
54 } else if(ReadData.SERIALIZABLE_CLASS.equals(message.getClass())) {
55 readData(transaction, ReadData.fromSerializable(message), SERIALIZED_REPLY);
57 } else if(DataExists.SERIALIZABLE_CLASS.equals(message.getClass())) {
58 dataExists(transaction, DataExists.fromSerializable(message), SERIALIZED_REPLY);
61 super.handleReceive(message);
65 private void createSnapshot() {
67 // This is a special message sent by the shard to send back a serialized snapshot of the whole
68 // data store tree. This transaction was created for that purpose only so we can
69 // self-destruct after sending the reply.
71 final ActorRef sender = getSender();
72 final ActorRef self = getSelf();
73 final ListenableFuture<Optional<NormalizedNode<?, ?>>> future = transaction.read(DATASTORE_ROOT);
75 Futures.addCallback(future, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
77 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
78 byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
79 sender.tell(new CaptureSnapshotReply(serialized), self);
81 self.tell(PoisonPill.getInstance(), self);
85 public void onFailure(Throwable t) {
86 sender.tell(new akka.actor.Status.Failure(t), self);
88 self.tell(PoisonPill.getInstance(), self);
94 protected DOMStoreTransaction getDOMStoreTransaction() {
99 protected boolean returnCloseTransactionReply() {