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.messages.CloseTransactionChain;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
17 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 * The ShardTransactionChain Actor represents a remote TransactionChain
25 public class ShardTransactionChain extends AbstractUntypedActor {
27 private final DOMStoreTransactionChain chain;
28 private final DatastoreContext datastoreContext;
29 private final SchemaContext schemaContext;
30 private final String shardName;
32 public ShardTransactionChain(DOMStoreTransactionChain chain, SchemaContext schemaContext,
33 DatastoreContext datastoreContext,String shardName) {
35 this.datastoreContext = datastoreContext;
36 this.schemaContext = schemaContext;
37 this.shardName = shardName;
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(new CloseTransactionChainReply().toSerializable(), getSelf());
49 unknownMessage(message);
53 private ActorRef getShardActor(){
54 return getContext().parent();
57 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction,
58 String transactionId) {
59 if(createTransaction.getTransactionType() ==
60 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
61 return getContext().actorOf(
62 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
63 schemaContext, datastoreContext,shardName), transactionId);
64 } else if (createTransaction.getTransactionType() ==
65 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
66 return getContext().actorOf(
67 ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
68 schemaContext, datastoreContext,shardName), transactionId);
69 } else if (createTransaction.getTransactionType() ==
70 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
71 return getContext().actorOf(
72 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
73 schemaContext, datastoreContext,shardName), transactionId);
75 throw new IllegalArgumentException (
76 "CreateTransaction message has unidentified transaction type=" +
77 createTransaction.getTransactionType());
81 private void createTransaction(CreateTransaction createTransaction) {
83 ActorRef transactionActor = createTypedTransactionActor(createTransaction, "shard-" + createTransaction.getTransactionId());
85 .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
89 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
90 DatastoreContext datastoreContext, String shardName) {
91 return Props.create(new ShardTransactionChainCreator(chain, schemaContext, datastoreContext, shardName));
94 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
95 private static final long serialVersionUID = 1L;
97 final DOMStoreTransactionChain chain;
98 final DatastoreContext datastoreContext;
99 final SchemaContext schemaContext;
100 final String shardName;
103 ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
104 DatastoreContext datastoreContext, String shardName) {
106 this.datastoreContext = datastoreContext;
107 this.schemaContext = schemaContext;
108 this.shardName = shardName;
112 public ShardTransactionChain create() throws Exception {
113 return new ShardTransactionChain(chain, schemaContext, datastoreContext,shardName);