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 com.google.common.base.Preconditions;
12 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import akka.japi.Creator;
15 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
16 import org.opendaylight.controller.cluster.datastore.TransactionProxy.TransactionType;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
19 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
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 ShardDataTreeTransactionChain chain;
30 private final DatastoreContext datastoreContext;
31 private final ShardStats shardStats;
33 public ShardTransactionChain(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
34 ShardStats shardStats) {
35 this.chain = Preconditions.checkNotNull(chain);
36 this.datastoreContext = datastoreContext;
37 this.shardStats = shardStats;
41 public void handleReceive(Object message) throws Exception {
42 if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
43 CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
44 createTransaction(createTransaction);
45 } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
47 getSender().tell(CloseTransactionChainReply.INSTANCE.toSerializable(), getSelf());
49 unknownMessage(message);
53 private ActorRef getShardActor(){
54 return getContext().parent();
57 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction) {
58 String transactionName = "shard-" + createTransaction.getTransactionId();
60 final TransactionType type = TransactionType.fromInt(createTransaction.getTransactionType());
61 final AbstractShardDataTreeTransaction<?> transaction;
64 transaction = chain.newReadOnlyTransaction(transactionName);
68 transaction = chain.newReadWriteTransaction(transactionName);
71 throw new IllegalArgumentException("Unhandled transaction type " + type);
74 return getContext().actorOf(
75 ShardTransaction.props(type, transaction, getShardActor(),
76 datastoreContext, shardStats, createTransaction.getTransactionId(),
77 createTransaction.getVersion()), transactionName);
80 private void createTransaction(CreateTransaction createTransaction) {
82 ActorRef transactionActor = createTypedTransactionActor(createTransaction);
83 getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
84 createTransaction.getTransactionId()).toSerializable(), getSelf());
87 public static Props props(ShardDataTreeTransactionChain chain, SchemaContext schemaContext,
88 DatastoreContext datastoreContext, ShardStats shardStats) {
89 return Props.create(new ShardTransactionChainCreator(chain, datastoreContext, shardStats));
92 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
93 private static final long serialVersionUID = 1L;
95 final ShardDataTreeTransactionChain chain;
96 final DatastoreContext datastoreContext;
97 final ShardStats shardStats;
99 ShardTransactionChainCreator(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
100 ShardStats shardStats) {
102 this.datastoreContext = datastoreContext;
103 this.shardStats = shardStats;
107 public ShardTransactionChain create() throws Exception {
108 return new ShardTransactionChain(chain, datastoreContext, shardStats);