Merge "Fixed for bug 1168 : Issue while update subnet"
[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 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.getClass().equals(CreateTransaction.SERIALIZABLE_CLASS)) {
38             CreateTransaction createTransaction = CreateTransaction.fromSerializable( message);
39             createTransaction(createTransaction);
40         } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)) {
41             chain.close();
42             getSender().tell(new CloseTransactionChainReply().toSerializable(), getSelf());
43         }else{
44           throw new Exception("Not recognized message recieved="+message);
45         }
46     }
47
48     private void createTransaction(CreateTransaction createTransaction) {
49         DOMStoreReadWriteTransaction transaction =
50             chain.newReadWriteTransaction();
51         ActorRef transactionActor = getContext().actorOf(ShardTransaction
52             .props(chain, transaction, getContext().parent(), schemaContext), "shard-" + createTransaction.getTransactionId());
53         getSender()
54             .tell(new CreateTransactionReply(transactionActor.path().toString(),createTransaction.getTransactionId()).toSerializable(),
55                 getSelf());
56     }
57
58     public static Props props(final DOMStoreTransactionChain chain, final SchemaContext schemaContext) {
59         return Props.create(new Creator<ShardTransactionChain>() {
60
61             @Override
62             public ShardTransactionChain create() throws Exception {
63                 return new ShardTransactionChain(chain, schemaContext);
64             }
65         });
66     }
67 }