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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.