Merge "Add support for metadata to the Match/Action classes"
[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()), 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()), transactionName);
73         } else if (createTransaction.getTransactionType() ==
74                 TransactionProxy.TransactionType.WRITE_ONLY.ordinal()) {
75             return getContext().actorOf(
76                     ShardTransaction.props( chain.newWriteOnlyTransaction(), getShardActor(),
77                             schemaContext, datastoreContext, shardStats,
78                             createTransaction.getTransactionId()), transactionName);
79         } else {
80             throw new IllegalArgumentException (
81                     "CreateTransaction message has unidentified transaction type=" +
82                              createTransaction.getTransactionType());
83         }
84     }
85
86     private void createTransaction(CreateTransaction createTransaction) {
87
88         ActorRef transactionActor = createTypedTransactionActor(createTransaction);
89         getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
90                 createTransaction.getTransactionId()).toSerializable(), getSelf());
91     }
92
93     public static Props props(DOMStoreTransactionChain chain, SchemaContext schemaContext,
94         DatastoreContext datastoreContext, ShardStats shardStats) {
95         return Props.create(new ShardTransactionChainCreator(chain, schemaContext,
96                 datastoreContext, shardStats));
97     }
98
99     private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
100         private static final long serialVersionUID = 1L;
101
102         final DOMStoreTransactionChain chain;
103         final DatastoreContext datastoreContext;
104         final SchemaContext schemaContext;
105         final ShardStats shardStats;
106
107
108         ShardTransactionChainCreator(DOMStoreTransactionChain chain, SchemaContext schemaContext,
109             DatastoreContext datastoreContext, ShardStats shardStats) {
110             this.chain = chain;
111             this.datastoreContext = datastoreContext;
112             this.schemaContext = schemaContext;
113             this.shardStats = shardStats;
114         }
115
116         @Override
117         public ShardTransactionChain create() throws Exception {
118             return new ShardTransactionChain(chain, schemaContext, datastoreContext, shardStats);
119         }
120     }
121 }