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