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 transactionId) {
61 if(createTransaction.getTransactionType() ==
62 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
63 return getContext().actorOf(
64 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
65 schemaContext, datastoreContext, shardStats), transactionId);
66 } else if (createTransaction.getTransactionType() ==
67 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
68 return getContext().actorOf(
69 ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
70 schemaContext, datastoreContext, shardStats), transactionId);
71 } else if (createTransaction.getTransactionType() ==
72 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
73 return getContext().actorOf(
74 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
75 schemaContext, datastoreContext, shardStats), transactionId);
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, "shard-" + createTransaction.getTransactionId());
87 .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
91 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
92 DatastoreContext datastoreContext, ShardStats shardStats) {
93 return Props.create(new ShardTransactionChainCreator(chain, schemaContext,
94 datastoreContext, shardStats));
97 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
98 private static final long serialVersionUID = 1L;
100 final DOMStoreTransactionChain chain;
101 final DatastoreContext datastoreContext;
102 final SchemaContext schemaContext;
103 final ShardStats shardStats;
106 ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
107 DatastoreContext datastoreContext, ShardStats shardStats) {
109 this.datastoreContext = datastoreContext;
110 this.schemaContext = schemaContext;
111 this.shardStats = shardStats;
115 public ShardTransactionChain create() throws Exception {
116 return new ShardTransactionChain(chain, schemaContext, datastoreContext, shardStats);