Enhancements to actor naming, logging and monitoring
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardTransactionChain.java
1 /*
2  * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import akka.japi.Creator;
14 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
15 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChainReply;
16 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
17 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
20
21 /**
22  * The ShardTransactionChain Actor represents a remote TransactionChain
23  */
24 public class ShardTransactionChain extends AbstractUntypedActor {
25
26     private final DOMStoreTransactionChain chain;
27
28     public ShardTransactionChain(DOMStoreTransactionChain chain) {
29         this.chain = chain;
30     }
31
32     @Override
33     public void handleReceive(Object message) throws Exception {
34         if (message instanceof CreateTransaction) {
35             CreateTransaction createTransaction = (CreateTransaction) message;
36             createTransaction(createTransaction);
37         } else if (message instanceof CloseTransactionChain) {
38             chain.close();
39             getSender().tell(new CloseTransactionChainReply(), getSelf());
40         }
41     }
42
43     private void createTransaction(CreateTransaction createTransaction) {
44         DOMStoreReadWriteTransaction transaction =
45             chain.newReadWriteTransaction();
46         ActorRef transactionActor = getContext().actorOf(ShardTransaction
47             .props(chain, transaction, getContext().parent()), "shard-" + createTransaction.getTransactionId());
48         getSender()
49             .tell(new CreateTransactionReply(transactionActor.path(), createTransaction.getTransactionId()),
50                 getSelf());
51     }
52
53     public static Props props(final DOMStoreTransactionChain chain) {
54         return Props.create(new Creator<ShardTransactionChain>() {
55
56             @Override
57             public ShardTransactionChain create() throws Exception {
58                 return new ShardTransactionChain(chain);
59             }
60         });
61     }
62 }