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;
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 SchemaContext schemaContext;
31 private final ShardStats shardStats;
33 public ShardTransactionChain(DOMStoreTransactionChain chain, SchemaContext schemaContext,
34 DatastoreContext datastoreContext, ShardStats shardStats) {
36 this.datastoreContext = datastoreContext;
37 this.schemaContext = schemaContext;
38 this.shardStats = shardStats;
42 public void handleReceive(Object message) throws Exception {
43 if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
44 CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
45 createTransaction(createTransaction);
46 } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
48 getSender().tell(new CloseTransactionChainReply().toSerializable(), getSelf());
50 unknownMessage(message);
54 private ActorRef getShardActor(){
55 return getContext().parent();
58 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction,
59 String transactionId) {
60 if(createTransaction.getTransactionType() ==
61 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
62 return getContext().actorOf(
63 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
64 schemaContext, datastoreContext, shardStats), transactionId);
65 } else if (createTransaction.getTransactionType() ==
66 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
67 return getContext().actorOf(
68 ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
69 schemaContext, datastoreContext, shardStats), transactionId);
70 } else if (createTransaction.getTransactionType() ==
71 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
72 return getContext().actorOf(
73 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
74 schemaContext, datastoreContext, shardStats), transactionId);
76 throw new IllegalArgumentException (
77 "CreateTransaction message has unidentified transaction type=" +
78 createTransaction.getTransactionType());
82 private void createTransaction(CreateTransaction createTransaction) {
84 ActorRef transactionActor = createTypedTransactionActor(createTransaction, "shard-" + createTransaction.getTransactionId());
86 .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
90 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
91 DatastoreContext datastoreContext, ShardStats shardStats) {
92 return Props.create(new ShardTransactionChainCreator(chain, schemaContext,
93 datastoreContext, shardStats));
96 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
97 private static final long serialVersionUID = 1L;
99 final DOMStoreTransactionChain chain;
100 final DatastoreContext datastoreContext;
101 final SchemaContext schemaContext;
102 final ShardStats shardStats;
105 ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
106 DatastoreContext datastoreContext, ShardStats shardStats) {
108 this.datastoreContext = datastoreContext;
109 this.schemaContext = schemaContext;
110 this.shardStats = shardStats;
114 public ShardTransactionChain create() throws Exception {
115 return new ShardTransactionChain(chain, schemaContext, datastoreContext, shardStats);