Merge "Optimizations, Monitoring and Logging"
[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             unknownMessage(message);
44         }
45     }
46
47     private ActorRef getShardActor(){
48         return getContext().parent();
49     }
50
51   private ActorRef createTypedTransactionActor(CreateTransaction createTransaction,String transactionId){
52     if(createTransaction.getTransactionType()== TransactionProxy.TransactionType.READ_ONLY.ordinal()){
53       return getContext().actorOf(
54           ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(), schemaContext), transactionId);
55
56     }else if (createTransaction.getTransactionType()== TransactionProxy.TransactionType.READ_WRITE.ordinal()){
57       return getContext().actorOf(
58           ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(), schemaContext), transactionId);
59
60
61     }else if (createTransaction.getTransactionType()== TransactionProxy.TransactionType.WRITE_ONLY.ordinal()){
62       return getContext().actorOf(
63           ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(), schemaContext), transactionId);
64     }else{
65       throw new IllegalArgumentException ("CreateTransaction message has unidentified transaction type="+createTransaction.getTransactionType()) ;
66     }
67   }
68
69     private void createTransaction(CreateTransaction createTransaction) {
70
71         ActorRef transactionActor = createTypedTransactionActor(createTransaction, "shard-" + createTransaction.getTransactionId());
72         getSender()
73             .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
74                 getSelf());
75     }
76
77     public static Props props(final DOMStoreTransactionChain chain, final SchemaContext schemaContext) {
78         return Props.create(new Creator<ShardTransactionChain>() {
79
80             @Override
81             public ShardTransactionChain create() throws Exception {
82                 return new ShardTransactionChain(chain, schemaContext);
83             }
84         });
85     }
86 }