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