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 ShardContext shardContext;
29 private final SchemaContext schemaContext;
31 public ShardTransactionChain(DOMStoreTransactionChain chain, SchemaContext schemaContext,
32 ShardContext shardContext) {
34 this.shardContext = shardContext;
35 this.schemaContext = schemaContext;
39 public void handleReceive(Object message) throws Exception {
40 if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
41 CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
42 createTransaction(createTransaction);
43 } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
45 getSender().tell(new CloseTransactionChainReply().toSerializable(), getSelf());
47 unknownMessage(message);
51 private ActorRef getShardActor(){
52 return getContext().parent();
55 private ActorRef createTypedTransactionActor(CreateTransaction createTransaction,
56 String transactionId) {
57 if(createTransaction.getTransactionType() ==
58 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
59 return getContext().actorOf(
60 ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
61 schemaContext, shardContext), transactionId);
62 } else if (createTransaction.getTransactionType() ==
63 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
64 return getContext().actorOf(
65 ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
66 schemaContext, shardContext), transactionId);
67 } else if (createTransaction.getTransactionType() ==
68 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
69 return getContext().actorOf(
70 ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
71 schemaContext, shardContext), transactionId);
73 throw new IllegalArgumentException (
74 "CreateTransaction message has unidentified transaction type=" +
75 createTransaction.getTransactionType());
79 private void createTransaction(CreateTransaction createTransaction) {
81 ActorRef transactionActor = createTypedTransactionActor(createTransaction, "shard-" + createTransaction.getTransactionId());
83 .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
87 public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
88 ShardContext shardContext) {
89 return Props.create(new ShardTransactionChainCreator(chain, schemaContext, shardContext));
92 private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
93 private static final long serialVersionUID = 1L;
95 final DOMStoreTransactionChain chain;
96 final ShardContext shardContext;
97 final SchemaContext schemaContext;
99 ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
100 ShardContext shardContext) {
102 this.shardContext = shardContext;
103 this.schemaContext = schemaContext;
107 public ShardTransactionChain create() throws Exception {
108 return new ShardTransactionChain(chain, schemaContext, shardContext);