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;
16 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 * The ShardTransactionChain Actor represents a remote TransactionChain
27 public class ShardTransactionChain extends AbstractUntypedActor {
29 private final DOMStoreTransactionChain chain;
30 private final DatastoreContext datastoreContext;
31 private final SchemaContext schemaContext;
32 private final ShardStats shardStats;
34 public ShardTransactionChain(DOMStoreTransactionChain chain, SchemaContext schemaContext,
35 DatastoreContext datastoreContext, ShardStats shardStats) {
37 this.datastoreContext = datastoreContext;
38 this.schemaContext = schemaContext;
39 this.shardStats = shardStats;
43 public void handleReceive(Object message) throws Exception {
44 if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
45 CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
46 createTransaction(createTransaction);
47 } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
49 getSender().tell(new CloseTransactionChainReply().toSerializable(), getSelf());
51 unknownMessage(message);
55 private ActorRef getShardActor(){
56 return getContext().parent();
59 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction) {
60 String transactionName = "shard-" + createTransaction.getTransactionId();
61 if(createTransaction.getTransactionType() ==
62 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
63 return getContext().actorOf(
64 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
65 schemaContext, datastoreContext, shardStats,
66 createTransaction.getTransactionId()), transactionName);
67 } else if (createTransaction.getTransactionType() ==
68 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
69 return getContext().actorOf(
70 ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
71 schemaContext, datastoreContext, shardStats,
72 createTransaction.getTransactionId()), transactionName);
73 } else if (createTransaction.getTransactionType() ==
74 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
75 return getContext().actorOf(
76 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
77 schemaContext, datastoreContext, shardStats,
78 createTransaction.getTransactionId()), transactionName);
80 throw new IllegalArgumentException (
81 "CreateTransaction message has unidentified transaction type=" +
82 createTransaction.getTransactionType());
86 private void createTransaction(CreateTransaction createTransaction) {
88 ActorRef transactionActor = createTypedTransactionActor(createTransaction);
89 getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
90 createTransaction.getTransactionId()).toSerializable(), getSelf());
93 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
94 DatastoreContext datastoreContext, ShardStats shardStats) {
95 return Props.create(new ShardTransactionChainCreator(chain, schemaContext,
96 datastoreContext, shardStats));
99 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
100 private static final long serialVersionUID = 1L;
102 final DOMStoreTransactionChain chain;
103 final DatastoreContext datastoreContext;
104 final SchemaContext schemaContext;
105 final ShardStats shardStats;
108 ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
109 DatastoreContext datastoreContext, ShardStats shardStats) {
111 this.datastoreContext = datastoreContext;
112 this.schemaContext = schemaContext;
113 this.shardStats = shardStats;
117 public ShardTransactionChain create() throws Exception {
118 return new ShardTransactionChain(chain, schemaContext, datastoreContext, shardStats);