Merge "Remove isCloseMsg check for each rpc"
[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.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;
22
23 /**
24  * The ShardTransactionChain Actor represents a remote TransactionChain
25  */
26 public class ShardTransactionChain extends AbstractUntypedActor {
27
28     private final DOMStoreTransactionChain chain;
29     private final DatastoreContext datastoreContext;
30     private final ShardStats shardStats;
31
32     public ShardTransactionChain(DOMStoreTransactionChain chain, DatastoreContext datastoreContext,
33             ShardStats shardStats) {
34         this.chain = chain;
35         this.datastoreContext = datastoreContext;
36         this.shardStats = shardStats;
37     }
38
39     @Override
40     public void handleReceive(Object message) throws Exception {
41         if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
42             CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
43             createTransaction(createTransaction);
44         } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
45             chain.close();
46             getSender().tell(CloseTransactionChainReply.INSTANCE.toSerializable(), getSelf());
47         }else{
48             unknownMessage(message);
49         }
50     }
51
52     private ActorRef getShardActor(){
53         return getContext().parent();
54     }
55
56     private ActorRef createTypedTransactionActor(CreateTransaction createTransaction) {
57         String transactionName = "shard-" + createTransaction.getTransactionId();
58         if(createTransaction.getTransactionType() ==
59                 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
60             return getContext().actorOf(
61                     ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
62                             datastoreContext, shardStats, createTransaction.getTransactionId(),
63                             createTransaction.getVersion()), transactionName);
64         } else if (createTransaction.getTransactionType() ==
65                 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
66             return getContext().actorOf(
67                     ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
68                             datastoreContext, shardStats, createTransaction.getTransactionId(),
69                             createTransaction.getVersion()), transactionName);
70         } else if (createTransaction.getTransactionType() ==
71                 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
72             return getContext().actorOf(
73                     ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
74                             datastoreContext, shardStats, createTransaction.getTransactionId(),
75                             createTransaction.getVersion()), transactionName);
76         } else {
77             throw new IllegalArgumentException (
78                     "CreateTransaction message has unidentified transaction type=" +
79                              createTransaction.getTransactionType());
80         }
81     }
82
83     private void createTransaction(CreateTransaction createTransaction) {
84
85         ActorRef transactionActor = createTypedTransactionActor(createTransaction);
86         getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
87                 createTransaction.getTransactionId()).toSerializable(), getSelf());
88     }
89
90     public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
91         DatastoreContext datastoreContext, ShardStats shardStats) {
92         return Props.create(new ShardTransactionChainCreator(chain, datastoreContext, shardStats));
93     }
94
95     private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
96         private static final long serialVersionUID = 1L;
97
98         final DOMStoreTransactionChain chain;
99         final DatastoreContext datastoreContext;
100         final ShardStats shardStats;
101
102
103         ShardTransactionChainCreator(DOMStoreTransactionChain chain, DatastoreContext datastoreContext,
104                 ShardStats shardStats) {
105             this.chain = chain;
106             this.datastoreContext = datastoreContext;
107             this.shardStats = shardStats;
108         }
109
110         @Override
111         public ShardTransactionChain create() throws Exception {
112             return new ShardTransactionChain(chain, datastoreContext, shardStats);
113         }
114     }
115 }