2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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.sal.core.spi.data.DOMStoreReadTransaction;
36 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
37 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
38 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
39 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import java.util.concurrent.ExecutionException;
47 * The ShardTransaction Actor represents a remote transaction
49 * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
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.
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}
68 public abstract class ShardTransaction extends AbstractUntypedActor {
70 private final ActorRef shardActor;
71 protected final SchemaContext schemaContext;
74 // If transactionChain is not null then this transaction is part of a
75 // transactionChain. Not really clear as to what that buys us
76 private final DOMStoreTransactionChain transactionChain;
79 private final MutableCompositeModification modification =
80 new MutableCompositeModification();
82 private final LoggingAdapter log =
83 Logging.getLogger(getContext().system(), this);
85 protected ShardTransaction(
86 ActorRef shardActor, SchemaContext schemaContext) {
87 this(null, shardActor, schemaContext);
90 protected ShardTransaction(DOMStoreTransactionChain transactionChain,
91 ActorRef shardActor, SchemaContext schemaContext) {
92 this.transactionChain = transactionChain;
93 //this.transaction = transaction;
94 this.shardActor = shardActor;
95 this.schemaContext = schemaContext;
100 public static Props props(final DOMStoreReadTransaction transaction,
101 final ActorRef shardActor, final SchemaContext schemaContext) {
102 return Props.create(new Creator<ShardTransaction>() {
105 public ShardTransaction create() throws Exception {
106 return new ShardReadTransaction(transaction, shardActor, schemaContext);
111 public static Props props(final DOMStoreTransactionChain transactionChain, final DOMStoreReadTransaction transaction,
112 final ActorRef shardActor, final SchemaContext schemaContext) {
113 return Props.create(new Creator<ShardTransaction>() {
116 public ShardTransaction create() throws Exception {
117 return new ShardReadTransaction(transactionChain, transaction, shardActor, schemaContext);
122 public static Props props(final DOMStoreReadWriteTransaction transaction,
123 final ActorRef shardActor, final SchemaContext schemaContext) {
124 return Props.create(new Creator<ShardTransaction>() {
127 public ShardTransaction create() throws Exception {
128 return new ShardReadWriteTransaction(transaction, shardActor, schemaContext);
133 public static Props props(final DOMStoreTransactionChain transactionChain, final DOMStoreReadWriteTransaction transaction,
134 final ActorRef shardActor, final SchemaContext schemaContext) {
135 return Props.create(new Creator<ShardTransaction>() {
138 public ShardTransaction create() throws Exception {
139 return new ShardReadWriteTransaction(transactionChain, transaction, shardActor, schemaContext);
145 public static Props props(final DOMStoreWriteTransaction transaction,
146 final ActorRef shardActor, final SchemaContext schemaContext) {
147 return Props.create(new Creator<ShardTransaction>() {
150 public ShardTransaction create() throws Exception {
151 return new ShardWriteTransaction(transaction, shardActor, schemaContext);
156 public static Props props(final DOMStoreTransactionChain transactionChain, final DOMStoreWriteTransaction transaction,
157 final ActorRef shardActor, final SchemaContext schemaContext) {
158 return Props.create(new Creator<ShardTransaction>() {
161 public ShardTransaction create() throws Exception {
162 return new ShardWriteTransaction(transactionChain, transaction, shardActor, schemaContext);
169 public void handleReceive(Object message) throws Exception {
170 if (message.getClass().equals(CloseTransaction.SERIALIZABLE_CLASS)) {
171 closeTransaction(new CloseTransaction());
172 } else if (message instanceof GetCompositedModification) {
173 // This is here for testing only
174 getSender().tell(new GetCompositeModificationReply(
175 new ImmutableCompositeModification(modification)), getSelf());
177 throw new Exception ("ShardTransaction:handleRecieve received an unknown message"+message);
181 abstract protected void closeTransaction(CloseTransaction message);
183 protected void readData(DOMStoreReadTransaction transaction,ReadData message) {
184 final ActorRef sender = getSender();
185 final ActorRef self = getSelf();
186 final YangInstanceIdentifier path = message.getPath();
187 final ListenableFuture<Optional<NormalizedNode<?, ?>>> future =
188 transaction.read(path);
190 future.addListener(new Runnable() {
194 Optional<NormalizedNode<?, ?>> optional = future.get();
195 if (optional.isPresent()) {
196 sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
198 sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
200 } catch (InterruptedException | ExecutionException e) {
202 "An exception happened when reading data from path : "
207 }, getContext().dispatcher());
211 protected void writeData(DOMStoreWriteTransaction transaction, WriteData message) {
212 modification.addModification(
213 new WriteModification(message.getPath(), message.getData(),schemaContext));
214 LOG.debug("writeData at path : " + message.getPath().toString());
215 transaction.write(message.getPath(), message.getData());
216 getSender().tell(new WriteDataReply().toSerializable(), getSelf());
219 protected void mergeData(DOMStoreWriteTransaction transaction, MergeData message) {
220 modification.addModification(
221 new MergeModification(message.getPath(), message.getData(), schemaContext));
222 LOG.debug("mergeData at path : " + message.getPath().toString());
223 transaction.merge(message.getPath(), message.getData());
224 getSender().tell(new MergeDataReply().toSerializable(), getSelf());
227 protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
228 modification.addModification(new DeleteModification(message.getPath()));
229 transaction.delete(message.getPath());
230 getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
233 protected void readyTransaction(DOMStoreWriteTransaction transaction, ReadyTransaction message) {
234 DOMStoreThreePhaseCommitCohort cohort = transaction.ready();
235 ActorRef cohortActor = getContext().actorOf(
236 ThreePhaseCommitCohort.props(cohort, shardActor, modification), "cohort");
238 .tell(new ReadyTransactionReply(cohortActor.path()).toSerializable(), getSelf());
243 // These classes are in here for test purposes only
246 static class GetCompositedModification {
251 static class GetCompositeModificationReply {
252 private final CompositeModification modification;
255 GetCompositeModificationReply(CompositeModification modification) {
256 this.modification = modification;
260 public CompositeModification getModification() {