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