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.japi.Creator;
14 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
15 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
18 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 * The ShardTransactionChain Actor represents a remote TransactionChain
26 public class ShardTransactionChain extends AbstractUntypedActor {
28 private final DOMStoreTransactionChain chain;
29 private final DatastoreContext datastoreContext;
30 private final ShardStats shardStats;
32 public ShardTransactionChain(DOMStoreTransactionChain chain, DatastoreContext datastoreContext,
33 ShardStats shardStats) {
35 this.datastoreContext = datastoreContext;
36 this.shardStats = shardStats;
40 public void handleReceive(Object message) throws Exception {
41 if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
42 CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
43 createTransaction(createTransaction);
44 } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
46 getSender().tell(CloseTransactionChainReply.INSTANCE.toSerializable(), getSelf());
48 unknownMessage(message);
52 private ActorRef getShardActor(){
53 return getContext().parent();
56 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction) {
57 String transactionName = "shard-" + createTransaction.getTransactionId();
58 if(createTransaction.getTransactionType() ==
59 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
60 return getContext().actorOf(
61 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
62 datastoreContext, shardStats, createTransaction.getTransactionId(),
63 createTransaction.getVersion()), transactionName);
64 } else if (createTransaction.getTransactionType() ==
65 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
66 return getContext().actorOf(
67 ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
68 datastoreContext, shardStats, createTransaction.getTransactionId(),
69 createTransaction.getVersion()), transactionName);
70 } else if (createTransaction.getTransactionType() ==
71 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
72 return getContext().actorOf(
73 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
74 datastoreContext, shardStats, createTransaction.getTransactionId(),
75 createTransaction.getVersion()), transactionName);
77 throw new IllegalArgumentException (
78 "CreateTransaction message has unidentified transaction type=" +
79 createTransaction.getTransactionType());
83 private void createTransaction(CreateTransaction createTransaction) {
85 ActorRef transactionActor = createTypedTransactionActor(createTransaction);
86 getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
87 createTransaction.getTransactionId()).toSerializable(), getSelf());
90 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
91 DatastoreContext datastoreContext, ShardStats shardStats) {
92 return Props.create(new ShardTransactionChainCreator(chain, datastoreContext, shardStats));
95 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
96 private static final long serialVersionUID = 1L;
98 final DOMStoreTransactionChain chain;
99 final DatastoreContext datastoreContext;
100 final ShardStats shardStats;
103 ShardTransactionChainCreator(DOMStoreTransactionChain chain, DatastoreContext datastoreContext,
104 ShardStats shardStats) {
106 this.datastoreContext = datastoreContext;
107 this.shardStats = shardStats;
111 public ShardTransactionChain create() throws Exception {
112 return new ShardTransactionChain(chain, datastoreContext, shardStats);