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.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.yangtools.yang.model.api.SchemaContext;
24 * The ShardTransactionChain Actor represents a remote TransactionChain
26 public class ShardTransactionChain extends AbstractUntypedActor {
28 private final ShardDataTreeTransactionChain chain;
29 private final DatastoreContext datastoreContext;
30 private final ShardStats shardStats;
32 public ShardTransactionChain(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
33 ShardStats shardStats) {
34 this.chain = Preconditions.checkNotNull(chain);
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();
59 final TransactionType type = TransactionType.fromInt(createTransaction.getTransactionType());
60 final AbstractShardDataTreeTransaction<?> transaction;
63 transaction = chain.newReadOnlyTransaction(transactionName);
67 transaction = chain.newReadWriteTransaction(transactionName);
70 throw new IllegalArgumentException("Unhandled transaction type " + type);
73 return getContext().actorOf(
74 ShardTransaction.props(type, transaction, getShardActor(),
75 datastoreContext, shardStats, createTransaction.getTransactionId(),
76 createTransaction.getVersion()), transactionName);
79 private void createTransaction(CreateTransaction createTransaction) {
81 ActorRef transactionActor = createTypedTransactionActor(createTransaction);
82 getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
83 createTransaction.getTransactionId()).toSerializable(), getSelf());
86 public static Props props(ShardDataTreeTransactionChain chain, SchemaContext schemaContext,
87 DatastoreContext datastoreContext, ShardStats shardStats) {
88 return Props.create(new ShardTransactionChainCreator(chain, datastoreContext, shardStats));
91 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
92 private static final long serialVersionUID = 1L;
94 final ShardDataTreeTransactionChain chain;
95 final DatastoreContext datastoreContext;
96 final ShardStats shardStats;
98 ShardTransactionChainCreator(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
99 ShardStats shardStats) {
101 this.datastoreContext = datastoreContext;
102 this.shardStats = shardStats;
106 public ShardTransactionChain create() throws Exception {
107 return new ShardTransactionChain(chain, datastoreContext, shardStats);