f747afa7867ac8f7f6434987045315b98bc9cca5
[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.actor.UntypedActor;
14 import akka.event.Logging;
15 import akka.event.LoggingAdapter;
16 import akka.japi.Creator;
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
21 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
22 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
24 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
28 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
31 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
32 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
33 import org.opendaylight.controller.cluster.datastore.modification.ImmutableCompositeModification;
34 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
35 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
36 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41
42 import java.util.concurrent.ExecutionException;
43
44 /**
45  * The ShardTransaction Actor represents a remote transaction
46  * <p>
47  * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
48  * </p>
49  * <p>
50  * Even though the DOMStore and the DOMStoreTransactionChain implement multiple types of transactions
51  * the ShardTransaction Actor only works with read-write transactions. This is just to keep the logic simple. At this
52  * time there are no known advantages for creating a read-only or write-only transaction which may change over time
53  * at which point we can optimize things in the distributed store as well.
54  * </p>
55  * <p>
56  * Handles Messages <br/>
57  * ---------------- <br/>
58  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadData}
59  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.WriteData}
60  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.MergeData}
61  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.DeleteData}
62  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction}
63  * <li> {@link org.opendaylight.controller.cluster.datastore.messages.CloseTransaction}
64  * </p>
65  */
66 public class ShardTransaction extends UntypedActor {
67
68     private final ActorRef shardActor;
69
70     private final DOMStoreReadWriteTransaction transaction;
71
72     private final MutableCompositeModification modification =
73         new MutableCompositeModification();
74
75     private final LoggingAdapter log =
76         Logging.getLogger(getContext().system(), this);
77
78     public ShardTransaction(DOMStoreReadWriteTransaction transaction,
79         ActorRef shardActor) {
80         this.transaction = transaction;
81         this.shardActor = shardActor;
82     }
83
84
85     public static Props props(final DOMStoreReadWriteTransaction transaction,
86         final ActorRef shardActor) {
87         return Props.create(new Creator<ShardTransaction>() {
88
89             @Override
90             public ShardTransaction create() throws Exception {
91                 return new ShardTransaction(transaction, shardActor);
92             }
93         });
94     }
95
96     @Override
97     public void onReceive(Object message) throws Exception {
98         log.debug("Received message {}", message);
99
100         if (message instanceof ReadData) {
101             readData((ReadData) message);
102         } else if (message instanceof WriteData) {
103             writeData((WriteData) message);
104         } else if (message instanceof MergeData) {
105             mergeData((MergeData) message);
106         } else if (message instanceof DeleteData) {
107             deleteData((DeleteData) message);
108         } else if (message instanceof ReadyTransaction) {
109             readyTransaction((ReadyTransaction) message);
110         } else if (message instanceof CloseTransaction) {
111             closeTransaction((CloseTransaction) message);
112         } else if (message instanceof GetCompositedModification) {
113             // This is here for testing only
114             getSender().tell(new GetCompositeModificationReply(
115                 new ImmutableCompositeModification(modification)), getSelf());
116         }
117     }
118
119     private void readData(ReadData message) {
120         final ActorRef sender = getSender();
121         final ActorRef self = getSelf();
122         final InstanceIdentifier path = message.getPath();
123         final ListenableFuture<Optional<NormalizedNode<?, ?>>> future =
124             transaction.read(path);
125
126         future.addListener(new Runnable() {
127             @Override
128             public void run() {
129                 try {
130                     Optional<NormalizedNode<?, ?>> optional = future.get();
131                     if (optional.isPresent()) {
132                         sender.tell(new ReadDataReply(optional.get()), self);
133                     } else {
134                         //TODO : Need to decide what to do here
135                     }
136                 } catch (InterruptedException | ExecutionException e) {
137                     log.error(e,
138                         "An exception happened when reading data from path : "
139                             + path.toString());
140                 }
141
142             }
143         }, getContext().dispatcher());
144     }
145
146
147     private void writeData(WriteData message) {
148         modification.addModification(
149             new WriteModification(message.getPath(), message.getData()));
150         transaction.write(message.getPath(), message.getData());
151         getSender().tell(new WriteDataReply(), getSelf());
152     }
153
154     private void mergeData(MergeData message) {
155         modification.addModification(
156             new MergeModification(message.getPath(), message.getData()));
157         transaction.merge(message.getPath(), message.getData());
158         getSender().tell(new MergeDataReply(), getSelf());
159     }
160
161     private void deleteData(DeleteData message) {
162         modification.addModification(new DeleteModification(message.getPath()));
163         transaction.delete(message.getPath());
164         getSender().tell(new DeleteDataReply(), getSelf());
165     }
166
167     private void readyTransaction(ReadyTransaction message) {
168         DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
169         ActorRef cohortActor = getContext().actorOf(
170             ThreePhaseCommitCohort.props(cohort, shardActor, modification));
171         getSender()
172             .tell(new ReadyTransactionReply(cohortActor.path()), getSelf());
173
174     }
175
176     private void closeTransaction(CloseTransaction message) {
177         transaction.close();
178         getSender().tell(new CloseTransactionReply(), getSelf());
179     }
180
181
182     // These classes are in here for test purposes only
183
184
185     static class GetCompositedModification {
186
187     }
188
189
190     static class GetCompositeModificationReply {
191         private final CompositeModification modification;
192
193
194         GetCompositeModificationReply(CompositeModification modification) {
195             this.modification = modification;
196         }
197
198
199         public CompositeModification getModification() {
200             return modification;
201         }
202     }
203 }