Fix NPE on reading data
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.actor.Props;
14 import akka.actor.UntypedActor;
15 import akka.event.Logging;
16 import akka.event.LoggingAdapter;
17 import akka.japi.Creator;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
23 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
24 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
25 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
28 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
29 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
31 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
32 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
33 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
34 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
35 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
36 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
37 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
39 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
40 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
41 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43
44 import java.util.concurrent.ExecutionException;
45
46 /**
47  * The ShardTransaction Actor represents a remote transaction
48  * <p>
49  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
50  * </p>
51  * <p>
52  * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
53  * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
54  * time there are no known advantages for creating a read-only or write-only transaction which may change over time
55  * at which point we can optimize things in the distributed store as well.
56  * </p>
57  * <p>
58  * Handles Messages <br/>
59  * ---------------- <br/>
60  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
61  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
62  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
63  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
64  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
65  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
66  * </p>
67  */
68 public class ShardTransaction extends UntypedActor {
69
70     private final ActorRef shardActor;
71
72     // FIXME : see below
73     // If transactionChain is not null then this transaction is part of a
74     // transactionChain. Not really clear as to what that buys us
75     private final DOMStoreTransactionChain transactionChain;
76
77     private final DOMStoreReadWriteTransaction transaction;
78
79     private final MutableCompositeModification modification =
80         new MutableCompositeModification();
81
82     private final LoggingAdapter log =
83         Logging.getLogger(getContext().system(), this);
84
85     public ShardTransaction(DOMStoreReadWriteTransaction transaction,
86         ActorRef shardActor) {
87         this(null, transaction, shardActor);
88     }
89
90     public ShardTransaction(DOMStoreTransactionChain transactionChain, DOMStoreReadWriteTransaction transaction,
91         ActorRef shardActor) {
92         this.transactionChain = transactionChain;
93         this.transaction = transaction;
94         this.shardActor = shardActor;
95     }
96
97
98
99     public static Props props(final DOMStoreReadWriteTransaction transaction,
100         final ActorRef shardActor) {
101         return Props.create(new Creator<ShardTransaction>() {
102
103             @Override
104             public ShardTransaction create() throws Exception {
105                 return new ShardTransaction(transaction, shardActor);
106             }
107         });
108     }
109
110     public static Props props(final DOMStoreTransactionChain transactionChain, final DOMStoreReadWriteTransaction transaction,
111         final ActorRef shardActor) {
112         return Props.create(new Creator<ShardTransaction>() {
113
114             @Override
115             public ShardTransaction create() throws Exception {
116                 return new ShardTransaction(transactionChain, transaction, shardActor);
117             }
118         });
119     }
120
121
122     @Override
123     public void onReceive(Object message) throws Exception {
124         log.debug("Received message {}", message);
125
126         if (message instanceof ReadData) {
127             readData((ReadData) message);
128         } else if (message instanceof WriteData) {
129             writeData((WriteData) message);
130         } else if (message instanceof MergeData) {
131             mergeData((MergeData) message);
132         } else if (message instanceof DeleteData) {
133             deleteData((DeleteData) message);
134         } else if (message instanceof ReadyTransaction) {
135             readyTransaction((ReadyTransaction) message);
136         } else if (message instanceof CloseTransaction) {
137             closeTransaction((CloseTransaction) message);
138         } else if (message instanceof GetCompositedModification) {
139             // This is here for testing only
140             getSender().tell(new GetCompositeModificationReply(
141                 new ImmutableCompositeModification(modification)), getSelf());
142         }
143     }
144
145     private void readData(ReadData message) {
146         final ActorRef sender = getSender();
147         final ActorRef self = getSelf();
148         final InstanceIdentifier path = message.getPath();
149         final ListenableFuture<Optional<NormalizedNode<?, ?>>> future =
150             transaction.read(path);
151
152         future.addListener(new Runnable() {
153             @Override
154             public void run() {
155                 try {
156                     Optional<NormalizedNode<?, ?>> optional = future.get();
157                     if (optional.isPresent()) {
158                         sender.tell(new ReadDataReply(optional.get()), self);
159                     } else {
160                         sender.tell(new ReadDataReply(null), self);
161                     }
162                 } catch (InterruptedException | ExecutionException e) {
163                     log.error(e,
164                         "An exception happened when reading data from path : "
165                             + path.toString());
166                 }
167
168             }
169         }, getContext().dispatcher());
170     }
171
172
173     private void writeData(WriteData message) {
174         modification.addModification(
175             new WriteModification(message.getPath(), message.getData()));
176         transaction.write(message.getPath(), message.getData());
177         getSender().tell(new WriteDataReply(), getSelf());
178     }
179
180     private void mergeData(MergeData message) {
181         modification.addModification(
182             new MergeModification(message.getPath(), message.getData()));
183         transaction.merge(message.getPath(), message.getData());
184         getSender().tell(new MergeDataReply(), getSelf());
185     }
186
187     private void deleteData(DeleteData message) {
188         modification.addModification(new DeleteModification(message.getPath()));
189         transaction.delete(message.getPath());
190         getSender().tell(new DeleteDataReply(), getSelf());
191     }
192
193     private void readyTransaction(ReadyTransaction message) {
194         DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
195         ActorRef cohortActor = getContext().actorOf(
196             ThreePhaseCommitCohort.props(cohort, shardActor, modification));
197         getSender()
198             .tell(new ReadyTransactionReply(cohortActor.path()), getSelf());
199
200     }
201
202     private void closeTransaction(CloseTransaction message) {
203         transaction.close();
204         getSender().tell(new CloseTransactionReply(), getSelf());
205         getSelf().tell(PoisonPill.getInstance(), getSelf());
206     }
207
208
209     // These classes are in here for test purposes only
210
211
212     static class GetCompositedModification {
213
214     }
215
216
217     static class GetCompositeModificationReply {
218         private final CompositeModification modification;
219
220
221         GetCompositeModificationReply(CompositeModification modification) {
222             this.modification = modification;
223         }
224
225
226         public CompositeModification getModification() {
227             return modification;
228         }
229     }
230 }