Merge "BUG-2288: implement DOMNotificationRouter"
[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 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;
30
31 /**
32  * @author: syedbahm
33  * Date: 8/6/14
34  */
35 public class ShardReadTransaction extends ShardTransaction {
36     private static final YangInstanceIdentifier DATASTORE_ROOT = YangInstanceIdentifier.builder().build();
37
38     private final DOMStoreReadTransaction transaction;
39
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;
45     }
46
47     @Override
48     public void handleReceive(Object message) throws Exception {
49         if(message instanceof ReadData) {
50             readData(transaction, (ReadData) message, !SERIALIZED_REPLY);
51
52         } else if (message instanceof DataExists) {
53             dataExists(transaction, (DataExists) message, !SERIALIZED_REPLY);
54         } else if (message instanceof CreateSnapshot) {
55             createSnapshot();
56         } else if(ReadData.SERIALIZABLE_CLASS.equals(message.getClass())) {
57             readData(transaction, ReadData.fromSerializable(message), SERIALIZED_REPLY);
58
59         } else if(DataExists.SERIALIZABLE_CLASS.equals(message.getClass())) {
60             dataExists(transaction, DataExists.fromSerializable(message), SERIALIZED_REPLY);
61
62         } else {
63             super.handleReceive(message);
64         }
65     }
66
67     private void createSnapshot() {
68
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.
72
73         final ActorRef sender = getSender();
74         final ActorRef self = getSelf();
75         final ListenableFuture<Optional<NormalizedNode<?, ?>>> future = transaction.read(DATASTORE_ROOT);
76
77         Futures.addCallback(future, new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
78             @Override
79             public void onSuccess(Optional<NormalizedNode<?, ?>> result) {
80                 byte[] serialized = SerializationUtils.serializeNormalizedNode(result.get());
81                 sender.tell(new CaptureSnapshotReply(serialized), self);
82
83                 self.tell(PoisonPill.getInstance(), self);
84             }
85
86             @Override
87             public void onFailure(Throwable t) {
88                 sender.tell(new akka.actor.Status.Failure(t), self);
89
90                 self.tell(PoisonPill.getInstance(), self);
91             }
92         });
93     }
94
95     @Override
96     protected DOMStoreTransaction getDOMStoreTransaction() {
97         return transaction;
98     }
99
100     @Override
101     protected boolean returnCloseTransactionReply() {
102         return false;
103     }
104 }