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.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;
46 * The ShardTransaction Actor represents a remote transaction
48 * The ShardTransaction Actor delegates all actions to DOMDataReadWriteTransaction
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.
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}
67 public abstract class ShardTransaction extends AbstractUntypedActor {
69 private final ActorRef shardActor;
70 protected final SchemaContext schemaContext;
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;
78 private final MutableCompositeModification modification =
79 new MutableCompositeModification();
81 private final LoggingAdapter log =
82 Logging.getLogger(getContext().system(), this);
84 protected ShardTransaction(
85 ActorRef shardActor, SchemaContext schemaContext) {
86 this(null, shardActor, schemaContext);
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;
99 public static Props props(final DOMStoreReadTransaction transaction,
100 final ActorRef shardActor, final SchemaContext schemaContext) {
101 return Props.create(new Creator<ShardTransaction>() {
104 public ShardTransaction create() throws Exception {
105 return new ShardReadTransaction(transaction, shardActor, schemaContext);
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>() {
115 public ShardTransaction create() throws Exception {
116 return new ShardReadTransaction(transactionChain, transaction, shardActor, schemaContext);
121 public static Props props(final DOMStoreReadWriteTransaction transaction,
122 final ActorRef shardActor, final SchemaContext schemaContext) {
123 return Props.create(new Creator<ShardTransaction>() {
126 public ShardTransaction create() throws Exception {
127 return new ShardReadWriteTransaction(transaction, shardActor, schemaContext);
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>() {
137 public ShardTransaction create() throws Exception {
138 return new ShardReadWriteTransaction(transactionChain, transaction, shardActor, schemaContext);
144 public static Props props(final DOMStoreWriteTransaction transaction,
145 final ActorRef shardActor, final SchemaContext schemaContext) {
146 return Props.create(new Creator<ShardTransaction>() {
149 public ShardTransaction create() throws Exception {
150 return new ShardWriteTransaction(transaction, shardActor, schemaContext);
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>() {
160 public ShardTransaction create() throws Exception {
161 return new ShardWriteTransaction(transactionChain, transaction, shardActor, schemaContext);
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());
176 throw new Exception ("ShardTransaction:handleRecieve received an unknown message"+message);
180 abstract protected void closeTransaction(CloseTransaction message);
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);
189 future.addListener(new Runnable() {
193 Optional<NormalizedNode<?, ?>> optional = future.get();
194 if (optional.isPresent()) {
195 sender.tell(new ReadDataReply(schemaContext,optional.get()).toSerializable(), self);
197 sender.tell(new ReadDataReply(schemaContext,null).toSerializable(), self);
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);
205 }, getContext().dispatcher());
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());
215 transaction.write(message.getPath(), message.getData());
216 getSender().tell(new WriteDataReply().toSerializable(), getSelf());
218 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
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());
227 transaction.merge(message.getPath(), message.getData());
228 getSender().tell(new MergeDataReply().toSerializable(), getSelf());
230 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
234 protected void deleteData(DOMStoreWriteTransaction transaction, DeleteData message) {
235 modification.addModification(new DeleteModification(message.getPath()));
237 transaction.delete(message.getPath());
238 getSender().tell(new DeleteDataReply().toSerializable(), getSelf());
240 getSender().tell(new akka.actor.Status.Failure(e), getSelf());
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");
249 .tell(new ReadyTransactionReply(cohortActor.path()).toSerializable(), getSelf());
254 // These classes are in here for test purposes only
257 static class GetCompositedModification {
262 static class GetCompositeModificationReply {
263 private final CompositeModification modification;
266 GetCompositeModificationReply(CompositeModification modification) {
267 this.modification = modification;
271 public CompositeModification getModification() {