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;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 public class ShardReadTransaction extends ShardTransaction {
36 private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
38 private final DOMStoreReadTransaction transaction;
40 public ShardReadTransaction(DOMStoreReadTransaction transaction, ActorRef shardActor,
41 SchemaContext schemaContext, ShardStats shardStats, String transactionID,
42 short clientTxVersion) {
43 super(shardActor, schemaContext, shardStats, transactionID, clientTxVersion);
44 this.transaction = transaction;
48 public void handleReceive(Object message) throws Exception {
49 if(message instanceof ReadData) {
50 readData(transaction, (ReadData) message, !SERIALIZED_REPLY);
52 } else if (message instanceof DataExists) {
53 dataExists(transaction, (DataExists) message, !SERIALIZED_REPLY);
54 } else if (message instanceof CreateSnapshot) {
56 } else if(ReadData.SERIALIZABLE_CLASS.equals(message.getClass())) {
57 readData(transaction, ReadData.fromSerializable(message), SERIALIZED_REPLY);
59 } else if(DataExists.SERIALIZABLE_CLASS.equals(message.getClass())) {
60 dataExists(transaction, DataExists.fromSerializable(message), SERIALIZED_REPLY);
63 super.handleReceive(message);
67 private void createSnapshot() {
69 // This is a special message sent by the shard to send back a serialized snapshot of the whole
70 // data store tree. This transaction was created for that purpose only so we can
71 // self-destruct after sending the reply.
73 final ActorRef sender = getSender();
74 final ActorRef self = getSelf();
75 final ListenableFuture<Optional<NormalizedNode<?, ?>>> future = transaction.read(DATASTORE_ROOT);
77 Futures.addCallback(future, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
79 public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
80 byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
81 sender.tell(new CaptureSnapshotReply(serialized), self);
83 self.tell(PoisonPill.getInstance(), self);
87 public void onFailure(Throwable t) {
88 sender.tell(new akka.actor.Status.Failure(t), self);
90 self.tell(PoisonPill.getInstance(), self);
96 protected DOMStoreTransaction getDOMStoreTransaction() {
101 protected boolean returnCloseTransactionReply() {