Merge "Bug 1593 - Flow Statistics manager is updating store with incorrect key Statis...
[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.ReceiveTimeout;
15 import akka.japi.Creator;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19
20 import org.opendaylight.controller.cluster.datastore.exceptions.UnknownMessageException;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
24 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
25 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
26 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
27 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
28 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
29 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
30 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
31 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
32 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
33 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
34 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
35 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
36 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
37 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
38 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
39 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
40 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
41 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
42 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
43 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
45 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransaction;
46 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50
51 /**
52  * The ShardTransaction Actor represents a remote transaction
53  * <p>
54  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
55  * </p>
56  * <p>
57  * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
58  * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
59  * time there are no known advantages for creating a read-only or write-only transaction which may change over time
60  * at which point we can optimize things in the distributed store as well.
61  * </p>
62  * <p>
63  * Handles Messages <br/>
64  * ---------------- <br/>
65  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
66  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
67  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
68  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
69  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
70  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
71  * </p>
72  */
73 public abstract class ShardTransaction extends AbstractUntypedActor {
74
75     private final ActorRef shardActor;
76     protected final SchemaContext schemaContext;
77
78     private final MutableCompositeModification modification = new MutableCompositeModification();
79
80     protected ShardTransaction(ActorRef shardActor, SchemaContext schemaContext) {
81         this.shardActor = shardActor;
82         this.schemaContext = schemaContext;
83     }
84
85     public static Props props(DOMStoreTransaction transaction, ActorRef shardActor,
86             SchemaContext schemaContext, ShardContext shardContext) {
87         return Props.create(new ShardTransactionCreator(transaction, shardActor, schemaContext,
88                 shardContext));
89     }
90
91     protected abstract DOMStoreTransaction getDOMStoreTransaction();
92
93     @Override
94     public void handleReceive(Object message) throws Exception {
95         if (message.getClass().equals(CloseTransaction.SERIALIZABLE_CLASS)) {
96             closeTransaction(true);
97         } else if (message instanceof GetCompositedModification) {
98             // This is here for testing only
99             getSender().tell(new GetCompositeModificationReply(
100                     new ImmutableCompositeModification(modification)), getSelf());
101         } else if (message instanceof ReceiveTimeout) {
102             LOG.debug("Got ReceiveTimeout for inactivity - closing Tx");
103             closeTransaction(false);
104         } else {
105             throw new UnknownMessageException(message);
106         }
107     }
108
109     private void closeTransaction(boolean sendReply) {
110         getDOMStoreTransaction().close();
111
112         if(sendReply) {
113             getSender().tell(new CloseTransactionReply().toSerializable(), getSelf());
114         }
115
116         getSelf().tell(PoisonPill.getInstance(), getSelf());
117     }
118
119     protected void readData(DOMStoreReadTransaction transaction,ReadData message) {
120         final ActorRef sender = getSender();
121         final ActorRef self = getSelf();
122         final YangInstanceIdentifier path = message.getPath();
123         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future =
124                 transaction.read(path);
125
126         future.addListener(new Runnable() {
127             @Override
128             public void run() {
129                 try {
130                     Optional<NormalizedNode<?, ?>> optional = future.checkedGet();
131                     if (optional.isPresent()) {
132                         sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
133                     } else {
134                         sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
135                     }
136                 } catch (Exception e) {
137                     sender.tell(new akka.actor.Status.Failure(e),self);
138                 }
139
140             }
141         }, getContext().dispatcher());
142     }
143
144     protected void dataExists(DOMStoreReadTransaction transaction, DataExists message) {
145         final YangInstanceIdentifier path = message.getPath();
146
147         try {
148             Boolean exists = transaction.exists(path).checkedGet();
149             getSender().tell(new DataExistsReply(exists).toSerializable(), getSelf());
150         } catch (ReadFailedException e) {
151             getSender().tell(new akka.actor.Status.Failure(e),getSelf());
152         }
153
154     }
155
156     protected void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
157         modification.addModification(
158                 new WriteModification(message.getPath(), message.getData(),schemaContext));
159         LOG.debug("writeData at path : " + message.getPath().toString());
160
161         try {
162             transaction.write(message.getPath(), message.getData());
163             getSender().tell(new WriteDataReply().toSerializable(), getSelf());
164         }catch(Exception e){
165             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
166         }
167     }
168
169     protected void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
170         modification.addModification(
171                 new MergeModification(message.getPath(), message.getData(), schemaContext));
172         LOG.debug("mergeData at path : " + message.getPath().toString());
173         try {
174             transaction.merge(message.getPath(), message.getData());
175             getSender().tell(new MergeDataReply().toSerializable(), getSelf());
176         }catch(Exception e){
177             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
178         }
179     }
180
181     protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
182         LOG.debug("deleteData at path : " + message.getPath().toString());
183         modification.addModification(new DeleteModification(message.getPath()));
184         try {
185             transaction.delete(message.getPath());
186             getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
187         }catch(Exception e){
188             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
189         }
190     }
191
192     protected void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
193         DOMStoreThreePhaseCommitCohort cohort =  transaction.ready();
194         ActorRef cohortActor = getContext().actorOf(
195                 ThreePhaseCommitCohort.props(cohort, shardActor, modification), "cohort");
196         getSender()
197         .tell(new ReadyTransactionReply(cohortActor.path()).toSerializable(), getSelf());
198
199     }
200
201     private static class ShardTransactionCreator implements Creator<ShardTransaction> {
202
203         private static final long serialVersionUID = 1L;
204
205         final DOMStoreTransaction transaction;
206         final ActorRef shardActor;
207         final SchemaContext schemaContext;
208         final ShardContext shardContext;
209
210         ShardTransactionCreator(DOMStoreTransaction transaction, ActorRef shardActor,
211                 SchemaContext schemaContext, ShardContext actorContext) {
212             this.transaction = transaction;
213             this.shardActor = shardActor;
214             this.shardContext = actorContext;
215             this.schemaContext = schemaContext;
216         }
217
218         @Override
219         public ShardTransaction create() throws Exception {
220             ShardTransaction tx;
221             if(transaction instanceof DOMStoreReadWriteTransaction) {
222                 tx = new ShardReadWriteTransaction((DOMStoreReadWriteTransaction)transaction,
223                         shardActor, schemaContext);
224             } else if(transaction instanceof DOMStoreReadTransaction) {
225                 tx = new ShardReadTransaction((DOMStoreReadTransaction)transaction, shardActor,
226                         schemaContext);
227             } else {
228                 tx = new ShardWriteTransaction((DOMStoreWriteTransaction)transaction,
229                         shardActor, schemaContext);
230             }
231
232             tx.getContext().setReceiveTimeout(shardContext.getShardTransactionIdleTimeout());
233             return tx;
234         }
235     }
236
237     // These classes are in here for test purposes only
238
239     static class GetCompositedModification {
240     }
241
242
243     static class GetCompositeModificationReply {
244         private final CompositeModification modification;
245
246
247         GetCompositeModificationReply(CompositeModification modification) {
248             this.modification = modification;
249         }
250
251
252         public CompositeModification getModification() {
253             return modification;
254         }
255     }
256 }