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