Merge "Fixed remove rpc api in rpc registry"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransactionChain.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.japi.Creator;
14 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
15 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
16 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
17 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20
21 /**
22  * The ShardTransactionChain Actor represents a remote TransactionChain
23  */
24 public class ShardTransactionChain extends AbstractUntypedActor {
25
26     private final DOMStoreTransactionChain chain;
27     private final SchemaContext schemaContext;
28
29     public ShardTransactionChain(DOMStoreTransactionChain chain, SchemaContext schemaContext) {
30         this.chain = chain;
31         this.schemaContext = schemaContext;
32     }
33
34     @Override
35     public void handleReceive(Object message) throws Exception {
36         if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
37             CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
38             createTransaction(createTransaction);
39         } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
40             chain.close();
41             getSender().tell(new CloseTransactionChainReply().toSerializable(), getSelf());
42         }else{
43           throw new Exception("Not recognized message recieved="+message);
44         }
45     }
46
47   private ActorRef createTypedTransactionActor(CreateTransaction createTransaction,String transactionId){
48     if(createTransaction.getTransactionType()== TransactionProxy.TransactionType.READ_ONLY.ordinal()){
49       return getContext().actorOf(
50           ShardTransaction.props( chain.newReadOnlyTransaction(), getSelf(), schemaContext), transactionId);
51
52     }else if (createTransaction.getTransactionType()== TransactionProxy.TransactionType.READ_WRITE.ordinal()){
53       return getContext().actorOf(
54           ShardTransaction.props( chain.newReadWriteTransaction(), getSelf(), schemaContext), transactionId);
55
56
57     }else if (createTransaction.getTransactionType()== TransactionProxy.TransactionType.WRITE_ONLY.ordinal()){
58       return getContext().actorOf(
59           ShardTransaction.props( chain.newWriteOnlyTransaction(), getSelf(), schemaContext), transactionId);
60     }else{
61       throw new IllegalArgumentException ("CreateTransaction message has unidentified transaction type="+createTransaction.getTransactionType()) ;
62     }
63   }
64
65     private void createTransaction(CreateTransaction createTransaction) {
66
67         ActorRef transactionActor = createTypedTransactionActor(createTransaction, "shard-" + createTransaction.getTransactionId());
68         getSender()
69             .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
70                 getSelf());
71     }
72
73     public static Props props(final DOMStoreTransactionChain chain, final SchemaContext schemaContext) {
74         return Props.create(new Creator<ShardTransactionChain>() {
75
76             @Override
77             public ShardTransactionChain create() throws Exception {
78                 return new ShardTransactionChain(chain, schemaContext);
79             }
80         });
81     }
82 }