76838dd2f83dc4bec45730e741dc5b8b47ad1686
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransactionFactory.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorRef;
11 import akka.actor.UntypedActorContext;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier;
14 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
15
16 /**
17  * A factory for creating ShardTransaction actors.
18  *
19  * @author Thomas Pantelis
20  */
21 class ShardTransactionActorFactory {
22
23     private final ShardDataTree dataTree;
24     private final DatastoreContext datastoreContext;
25     private final String txnDispatcherPath;
26     private final ShardStats shardMBean;
27     private final UntypedActorContext actorContext;
28     private final ActorRef shardActor;
29
30     ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
31             String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean) {
32         this.dataTree = Preconditions.checkNotNull(dataTree);
33         this.datastoreContext = datastoreContext;
34         this.txnDispatcherPath = txnDispatcherPath;
35         this.shardMBean = shardMBean;
36         this.actorContext = actorContext;
37         this.shardActor = shardActor;
38     }
39
40     ActorRef newShardTransaction(TransactionType type, ShardTransactionIdentifier transactionID,
41             String transactionChainID) {
42         final AbstractShardDataTreeTransaction<?> transaction;
43         switch (type) {
44         case READ_ONLY:
45             transaction = dataTree.newReadOnlyTransaction(transactionID.toString(), transactionChainID);
46             shardMBean.incrementReadOnlyTransactionCount();
47             break;
48         case READ_WRITE:
49             transaction = dataTree.newReadWriteTransaction(transactionID.toString(), transactionChainID);
50             shardMBean.incrementReadWriteTransactionCount();
51             break;
52         case WRITE_ONLY:
53             transaction = dataTree.newReadWriteTransaction(transactionID.toString(), transactionChainID);
54             shardMBean.incrementWriteOnlyTransactionCount();
55             break;
56         default:
57             throw new IllegalArgumentException("Unsupported transaction type " + type);
58         }
59
60         return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean,
61                 transactionID.getRemoteTransactionId()).withDispatcher(txnDispatcherPath),
62                 transactionID.toString());
63     }
64 }