Merge "BUG-2288: deprecate old binding Notification API elements"
[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.TransactionProxy.TransactionType;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
19 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
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 ShardDataTreeTransactionChain chain;
30     private final DatastoreContext datastoreContext;
31     private final ShardStats shardStats;
32
33     public ShardTransactionChain(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
34             ShardStats shardStats) {
35         this.chain = Preconditions.checkNotNull(chain);
36         this.datastoreContext = datastoreContext;
37         this.shardStats = shardStats;
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(CloseTransactionChainReply.INSTANCE.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 transactionName = "shard-" + createTransaction.getTransactionId();
59
60         final TransactionType type = TransactionType.fromInt(createTransaction.getTransactionType());
61         final AbstractShardDataTreeTransaction<?> transaction;
62         switch (type) {
63         case READ_ONLY:
64             transaction = chain.newReadOnlyTransaction(transactionName);
65             break;
66         case READ_WRITE:
67         case WRITE_ONLY:
68             transaction = chain.newReadWriteTransaction(transactionName);
69             break;
70         default:
71             throw new IllegalArgumentException("Unhandled transaction type " + type);
72         }
73
74         return getContext().actorOf(
75             ShardTransaction.props(type, transaction, getShardActor(),
76                     datastoreContext, shardStats, createTransaction.getTransactionId(),
77                     createTransaction.getVersion()), transactionName);
78     }
79
80     private void createTransaction(CreateTransaction createTransaction) {
81
82         ActorRef transactionActor = createTypedTransactionActor(createTransaction);
83         getSender().tell(new CreateTransactionReply(transactionActor.path().toString(),
84                 createTransaction.getTransactionId()).toSerializable(), getSelf());
85     }
86
87     public static Props props(ShardDataTreeTransactionChain chain, SchemaContext schemaContext,
88         DatastoreContext datastoreContext, ShardStats shardStats) {
89         return Props.create(new ShardTransactionChainCreator(chain, datastoreContext, shardStats));
90     }
91
92     private static class ShardTransactionChainCreator implements Creator<ShardTransactionChain> {
93         private static final long serialVersionUID = 1L;
94
95         final ShardDataTreeTransactionChain chain;
96         final DatastoreContext datastoreContext;
97         final ShardStats shardStats;
98
99         ShardTransactionChainCreator(ShardDataTreeTransactionChain chain, DatastoreContext datastoreContext,
100                 ShardStats shardStats) {
101             this.chain = chain;
102             this.datastoreContext = datastoreContext;
103             this.shardStats = shardStats;
104         }
105
106         @Override
107         public ShardTransactionChain create() throws Exception {
108             return new ShardTransactionChain(chain, datastoreContext, shardStats);
109         }
110     }
111 }

©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.