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