Merge "Bug 2265: Use new NormalizedNode streaming in messages"
[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 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(CloseTransactionChainReply.INSTANCE.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 transactionName = "shard-" + createTransaction.getTransactionId();
60         if(createTransaction.getTransactionType() ==
61                 TransactionProxy.TransactionType.READ_ONLY.ordinal()) {
62             return getContext().actorOf(
63                     ShardTransaction.props( chain.newReadOnlyTransaction(), getShardActor(),
64                             schemaContext, datastoreContext, shardStats,
65                             createTransaction.getTransactionId(),
66                             createTransaction.getVersion()), transactionName);
67         } else if (createTransaction.getTransactionType() ==
68                 TransactionProxy.TransactionType.READ_WRITE.ordinal()) {
69             return getContext().actorOf(
70                     ShardTransaction.props( chain.newReadWriteTransaction(), getShardActor(),
71                             schemaContext, datastoreContext, shardStats,
72                             createTransaction.getTransactionId(),
73                             createTransaction.getVersion()), transactionName);
74         } else if (createTransaction.getTransactionType() ==
75                 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
76             return getContext().actorOf(
77                     ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
78                             schemaContext, datastoreContext, shardStats,
79                             createTransaction.getTransactionId(),
80                             createTransaction.getVersion()), transactionName);
81         } else {
82             throw new IllegalArgumentException (
83                     "CreateTransaction message has unidentified transaction type=" +
84                              createTransaction.getTransactionType());
85         }
86     }
87
88     private void createTransaction(CreateTransaction createTransaction) {
89
90         ActorRef transactionActor = createTypedTransactionActor(createTransaction);
91         getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
92                 createTransaction.getTransactionId()).toSerializable(), getSelf());
93     }
94
95     public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
96         DatastoreContext datastoreContext, ShardStats shardStats) {
97         return Props.create(new ShardTransactionChainCreator(chain, schemaContext,
98                 datastoreContext, shardStats));
99     }
100
101     private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
102         private static final long serialVersionUID = 1L;
103
104         final DOMStoreTransactionChain chain;
105         final DatastoreContext datastoreContext;
106         final SchemaContext schemaContext;
107         final ShardStats shardStats;
108
109
110         ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
111             DatastoreContext datastoreContext, ShardStats shardStats) {
112             this.chain = chain;
113             this.datastoreContext = datastoreContext;
114             this.schemaContext = schemaContext;
115             this.shardStats = shardStats;
116         }
117
118         @Override
119         public ShardTransactionChain create() throws Exception {
120             return new ShardTransactionChain(chain, schemaContext, datastoreContext, shardStats);
121         }
122     }
123 }