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;
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(CloseTransactionChainReply.INSTANCE.toSerializable(), getSelf());
50 unknownMessage(message);
54 private ActorRef getShardActor(){
55 return getContext().parent();
58 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction) {
59 String transactionName = "shard-" + createTransaction.getTransactionId();
60 if(createTransaction.getTransactionType() ==
61 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
62 return getContext().actorOf(
63 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
64 schemaContext, datastoreContext, shardStats,
65 createTransaction.getTransactionId(),
66 createTransaction.getVersion()), 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(),
73 createTransaction.getVersion()), transactionName);
74 } else if (createTransaction.getTransactionType() ==
75 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
76 return getContext().actorOf(
77 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
78 schemaContext, datastoreContext, shardStats,
79 createTransaction.getTransactionId(),
80 createTransaction.getVersion()), transactionName);
82 throw new IllegalArgumentException (
83 "CreateTransaction message has unidentified transaction type=" +
84 createTransaction.getTransactionType());
88 private void createTransaction(CreateTransaction createTransaction) {
90 ActorRef transactionActor = createTypedTransactionActor(createTransaction);
91 getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
92 createTransaction.getTransactionId()).toSerializable(), getSelf());
95 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
96 DatastoreContext datastoreContext, ShardStats shardStats) {
97 return Props.create(new ShardTransactionChainCreator(chain, schemaContext,
98 datastoreContext, shardStats));
101 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
102 private static final long serialVersionUID = 1L;
104 final DOMStoreTransactionChain chain;
105 final DatastoreContext datastoreContext;
106 final SchemaContext schemaContext;
107 final ShardStats shardStats;
110 ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
111 DatastoreContext datastoreContext, ShardStats shardStats) {
113 this.datastoreContext = datastoreContext;
114 this.schemaContext = schemaContext;
115 this.shardStats = shardStats;
119 public ShardTransactionChain create() throws Exception {
120 return new ShardTransactionChain(chain, schemaContext, datastoreContext, shardStats);