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