Merge "Fix exception when netconf device provides YIN schemas"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardWriteTransaction.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.cluster.datastore;
12
13 import akka.actor.ActorRef;
14 import akka.actor.PoisonPill;
15 import akka.event.Logging;
16 import akka.event.LoggingAdapter;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
20 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26
27 /**
28  * @author: syedbahm
29  * Date: 8/6/14
30  */
31 public class ShardWriteTransaction extends ShardTransaction {
32   private final DOMStoreWriteTransaction transaction;
33   private final LoggingAdapter log =
34       Logging.getLogger(getContext().system(), this);
35   public ShardWriteTransaction(DOMStoreWriteTransaction transaction, ActorRef shardActor, SchemaContext schemaContext) {
36     super( shardActor, schemaContext);
37     this.transaction = transaction;
38
39   }
40
41   public ShardWriteTransaction(DOMStoreTransactionChain transactionChain, DOMStoreWriteTransaction transaction, ActorRef shardActor, SchemaContext schemaContext) {
42     super(transactionChain, shardActor, schemaContext);
43     this.transaction = transaction;
44   }
45
46   @Override
47   public void handleReceive(Object message) throws Exception {
48     if (WriteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
49       writeData(transaction, WriteData.fromSerializable(message, schemaContext));
50     } else if (MergeData.SERIALIZABLE_CLASS.equals(message.getClass())) {
51       mergeData(transaction, MergeData.fromSerializable(message, schemaContext));
52     } else if (DeleteData.SERIALIZABLE_CLASS.equals(message.getClass())) {
53       deleteData(transaction,DeleteData.fromSerializable(message));
54     } else if (ReadyTransaction.SERIALIZABLE_CLASS.equals(message.getClass())) {
55       readyTransaction(transaction,new ReadyTransaction());
56     }else {
57       super.handleReceive(message);
58     }
59   }
60
61   protected void closeTransaction(CloseTransaction message) {
62     transaction.close();
63     getSender().tell(new CloseTransactionReply().toSerializable(), getSelf());
64     getSelf().tell(PoisonPill.getInstance(), getSelf());
65   }
66
67     /**
68      * The following method is used in unit testing only
69      * hence the default scope.
70      * This is done to test out failure cases.
71      */
72     public void forUnitTestOnlyExplicitTransactionClose() {
73         transaction.close();
74     }
75 }