Merge "Fixed unneeded wrapping of guava for karaf"
[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.protobuff.messages.transaction.ShardTransactionMessages;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 /**
23  * The ShardTransactionChain Actor represents a remote TransactionChain
24  */
25 public class ShardTransactionChain extends AbstractUntypedActor {
26
27     private final DOMStoreTransactionChain chain;
28     private final SchemaContext schemaContext;
29
30     public ShardTransactionChain(DOMStoreTransactionChain chain, SchemaContext schemaContext) {
31         this.chain = chain;
32         this.schemaContext = schemaContext;
33     }
34
35     @Override
36     public void handleReceive(Object message) throws Exception {
37         if (message instanceof CreateTransaction) {
38             CreateTransaction createTransaction = (CreateTransaction) message;
39             createTransaction(createTransaction);
40         } else if (message instanceof CloseTransactionChain) {
41             chain.close();
42             getSender().tell(new CloseTransactionChainReply(), getSelf());
43         }
44     }
45
46     private void createTransaction(CreateTransaction createTransaction) {
47         DOMStoreReadWriteTransaction transaction =
48             chain.newReadWriteTransaction();
49         ActorRef transactionActor = getContext().actorOf(ShardTransaction
50             .props(chain, transaction, getContext().parent(), schemaContext), "shard-" + createTransaction.getTransactionId());
51         getSender()
52             .tell(ShardTransactionMessages.CreateTransactionReply.newBuilder()
53                 .setTransactionActorPath(transactionActor.path().toString())
54                 .setTransactionId(createTransaction.getTransactionId())
55                 .build(),
56                 getSelf());
57     }
58
59     public static Props props(final DOMStoreTransactionChain chain, final SchemaContext schemaContext) {
60         return Props.create(new Creator<ShardTransactionChain>() {
61
62             @Override
63             public ShardTransactionChain create() throws Exception {
64                 return new ShardTransactionChain(chain, schemaContext);
65             }
66         });
67     }
68 }