CDS: split TransactionType from TransactionProxy
[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 com.google.common.base.Preconditions;
12 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import akka.japi.Creator;
15 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
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.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 ShardDataTreeTransactionChain chain;
29     private final DatastoreContext datastoreContext;
30     private final ShardStats shardStats;
31
32     public ShardTransactionChain(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
33             ShardStats shardStats) {
34         this.chain = Preconditions.checkNotNull(chain);
35         this.datastoreContext = datastoreContext;
36         this.shardStats = shardStats;
37     }
38
39     @Override
40     public void handleReceive(Object message) throws Exception {
41         if (message.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
42             CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
43             createTransaction(createTransaction);
44         } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
45             chain.close();
46             getSender().tell(CloseTransactionChainReply.INSTANCE.toSerializable(), getSelf());
47         }else{
48             unknownMessage(message);
49         }
50     }
51
52     private ActorRef getShardActor(){
53         return getContext().parent();
54     }
55
56     private ActorRef createTypedTransactionActor(CreateTransaction createTransaction) {
57         String transactionName = "shard-" + createTransaction.getTransactionId();
58
59         final TransactionType type = TransactionType.fromInt(createTransaction.getTransactionType());
60         final AbstractShardDataTreeTransaction<?> transaction;
61         switch (type) {
62         case READ_ONLY:
63             transaction = chain.newReadOnlyTransaction(transactionName);
64             break;
65         case READ_WRITE:
66         case WRITE_ONLY:
67             transaction = chain.newReadWriteTransaction(transactionName);
68             break;
69         default:
70             throw new IllegalArgumentException("Unhandled transaction type " + type);
71         }
72
73         return getContext().actorOf(
74             ShardTransaction.props(type, transaction, getShardActor(),
75                     datastoreContext, shardStats, createTransaction.getTransactionId(),
76                     createTransaction.getVersion()), transactionName);
77     }
78
79     private void createTransaction(CreateTransaction createTransaction) {
80
81         ActorRef transactionActor = createTypedTransactionActor(createTransaction);
82         getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
83                 createTransaction.getTransactionId()).toSerializable(), getSelf());
84     }
85
86     public static Props props(ShardDataTreeTransactionChain chain, SchemaContext schemaContext,
87         DatastoreContext datastoreContext, ShardStats shardStats) {
88         return Props.create(new ShardTransactionChainCreator(chain, datastoreContext, shardStats));
89     }
90
91     private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
92         private static final long serialVersionUID = 1L;
93
94         final ShardDataTreeTransactionChain chain;
95         final DatastoreContext datastoreContext;
96         final ShardStats shardStats;
97
98         ShardTransactionChainCreator(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
99                 ShardStats shardStats) {
100             this.chain = chain;
101             this.datastoreContext = datastoreContext;
102             this.shardStats = shardStats;
103         }
104
105         @Override
106         public ShardTransactionChain create() throws Exception {
107             return new ShardTransactionChain(chain, datastoreContext, shardStats);
108         }
109     }
110 }