Bug 7814: Fix InvalidActorNameException
[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     private final String shardName;
33
34     ShardTransactionActorFactory(ShardDataTree dataTree, DatastoreContext datastoreContext,
35             String txnDispatcherPath, ActorRef shardActor, UntypedActorContext actorContext, ShardStats shardMBean,
36             String shardName) {
37         this.dataTree = Preconditions.checkNotNull(dataTree);
38         this.datastoreContext = Preconditions.checkNotNull(datastoreContext);
39         this.txnDispatcherPath = Preconditions.checkNotNull(txnDispatcherPath);
40         this.shardMBean = Preconditions.checkNotNull(shardMBean);
41         this.actorContext = Preconditions.checkNotNull(actorContext);
42         this.shardActor = Preconditions.checkNotNull(shardActor);
43         this.shardName = Preconditions.checkNotNull(shardName);
44     }
45
46     private String actorNameFor(final TransactionIdentifier txId) {
47         final LocalHistoryIdentifier historyId = txId.getHistoryId();
48         final ClientIdentifier clientId = historyId.getClientId();
49         final FrontendIdentifier frontendId = clientId.getFrontendId();
50
51         final StringBuilder sb = new StringBuilder("shard-");
52         sb.append(shardName).append('-')
53             .append(frontendId.getMemberName().getName()).append(':')
54             .append(frontendId.getClientType().getName()).append('@')
55             .append(clientId.getGeneration()).append(':');
56         if (historyId.getHistoryId() != 0) {
57             sb.append(historyId.getHistoryId()).append('-');
58         }
59
60         return sb.append(txId.getTransactionId()).toString();
61     }
62
63     ActorRef newShardTransaction(TransactionType type, TransactionIdentifier transactionID) {
64         final AbstractShardDataTreeTransaction<?> transaction;
65         switch (type) {
66             case READ_ONLY:
67                 transaction = dataTree.newReadOnlyTransaction(transactionID);
68                 shardMBean.incrementReadOnlyTransactionCount();
69                 break;
70             case READ_WRITE:
71                 transaction = dataTree.newReadWriteTransaction(transactionID);
72                 shardMBean.incrementReadWriteTransactionCount();
73                 break;
74             case WRITE_ONLY:
75                 transaction = dataTree.newReadWriteTransaction(transactionID);
76                 shardMBean.incrementWriteOnlyTransactionCount();
77                 break;
78             default:
79                 throw new IllegalArgumentException("Unsupported transaction type " + type);
80         }
81
82         return actorContext.actorOf(ShardTransaction.props(type, transaction, shardActor, datastoreContext, shardMBean)
83             .withDispatcher(txnDispatcherPath), actorNameFor(transactionID));
84     }
85 }