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