0ba36640a8f6368451478d0f28451a2c7a437db4
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransactionActorFactory.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.access.concepts.ClientIdentifier;
14 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
15 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats;
18
19 /**
20  * A factory for creating ShardTransaction actors.
21  *
22  * @author Thomas Pantelis
23  */
24 class ShardTransactionActorFactory {
25
26     private final ShardDataTree dataTree;
27     private final DatastoreContext datastoreContext;
28     private final String txnDispatcherPath;
29     private final ShardStats shardMBean;
30     private final UntypedActorContext actorContext;
31     private final ActorRef shardActor;
32
33     ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
34             String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean) {
35         this.dataTree = Preconditions.checkNotNull(dataTree);
36         this.datastoreContext = datastoreContext;
37         this.txnDispatcherPath = txnDispatcherPath;
38         this.shardMBean = shardMBean;
39         this.actorContext = actorContext;
40         this.shardActor = shardActor;
41     }
42
43     private static String actorNameFor(final TransactionIdentifier txId) {
44         final LocalHistoryIdentifier historyId = txId.getHistoryId();
45         final ClientIdentifier clientId = historyId.getClientId();
46         final FrontendIdentifier frontendId = clientId.getFrontendId();
47
48         final StringBuilder sb = new StringBuilder("shard-");
49         sb.append(frontendId.getMemberName().getName()).append(':');
50         sb.append(frontendId.getClientType().getName()).append('@');
51         sb.append(clientId.getGeneration()).append(':');
52         if (historyId.getHistoryId() != 0) {
53             sb.append(historyId.getHistoryId()).append('-');
54         }
55
56         return sb.append(txId.getTransactionId()).toString();
57     }
58
59     ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
60         final AbstractShardDataTreeTransaction<?> transaction;
61         switch (type) {
62             case READ_ONLY:
63                 transaction = dataTree.newReadOnlyTransaction(transactionID);
64                 shardMBean.incrementReadOnlyTransactionCount();
65                 break;
66             case READ_WRITE:
67                 transaction = dataTree.newReadWriteTransaction(transactionID);
68                 shardMBean.incrementReadWriteTransactionCount();
69                 break;
70             case WRITE_ONLY:
71                 transaction = dataTree.newReadWriteTransaction(transactionID);
72                 shardMBean.incrementWriteOnlyTransactionCount();
73                 break;
74             default:
75                 throw new IllegalArgumentException("Unsupported transaction type " + type);
76         }
77
78         return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
79             .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));
80     }
81 }