Add UnsignedLongBitmap
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardWriteTransaction.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2015 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore;
11
12 import akka.actor.ActorRef;
13 import akka.actor.PoisonPill;
14 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
15 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
16 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
17 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
18 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.cluster.datastore.modification.Modification;
21
22 /**
23  * Actor for a shard write-only transaction.
24  *
25  * @author syedbahm
26  */
27 public class ShardWriteTransaction extends ShardTransaction {
28
29     private int totalBatchedModificationsReceived;
30     private Exception lastBatchedModificationsException;
31     private final ReadWriteShardDataTreeTransaction transaction;
32
33     public ShardWriteTransaction(ReadWriteShardDataTreeTransaction transaction, ActorRef shardActor,
34             ShardStats shardStats) {
35         super(shardActor, shardStats, transaction.getIdentifier());
36         this.transaction = transaction;
37     }
38
39     @Override
40     protected ReadWriteShardDataTreeTransaction getDOMStoreTransaction() {
41         return transaction;
42     }
43
44     @Override
45     public void handleReceive(Object message) {
46         if (message instanceof BatchedModifications) {
47             batchedModifications((BatchedModifications)message);
48         } else {
49             super.handleReceive(message);
50         }
51     }
52
53     @SuppressWarnings("checkstyle:IllegalCatch")
54     private void batchedModifications(BatchedModifications batched) {
55         if (checkClosed()) {
56             if (batched.isReady()) {
57                 getSelf().tell(PoisonPill.getInstance(), getSelf());
58             }
59             return;
60         }
61
62         try {
63             for (Modification modification: batched.getModifications()) {
64                 modification.apply(transaction.getSnapshot());
65             }
66
67             totalBatchedModificationsReceived++;
68             if (batched.isReady()) {
69                 if (lastBatchedModificationsException != null) {
70                     throw lastBatchedModificationsException;
71                 }
72
73                 if (totalBatchedModificationsReceived != batched.getTotalMessagesSent()) {
74                     throw new IllegalStateException(String.format(
75                             "The total number of batched messages received %d does not match the number sent %d",
76                             totalBatchedModificationsReceived, batched.getTotalMessagesSent()));
77                 }
78
79                 readyTransaction(batched);
80             } else {
81                 getSender().tell(new BatchedModificationsReply(batched.getModifications().size()), getSelf());
82             }
83         } catch (Exception e) {
84             lastBatchedModificationsException = e;
85             getSender().tell(new akka.actor.Status.Failure(e), getSelf());
86
87             if (batched.isReady()) {
88                 getSelf().tell(PoisonPill.getInstance(), getSelf());
89             }
90         }
91     }
92
93     protected final void dataExists(DataExists message) {
94         super.dataExists(transaction, message);
95     }
96
97     protected final void readData(ReadData message) {
98         super.readData(transaction, message);
99     }
100
101     private boolean checkClosed() {
102         if (transaction.isClosed()) {
103             getSender().tell(new akka.actor.Status.Failure(new IllegalStateException(
104                     "Transaction is closed, no modifications allowed")), getSelf());
105             return true;
106         } else {
107             return false;
108         }
109     }
110
111     private void readyTransaction(BatchedModifications batched) {
112         TransactionIdentifier transactionID = getTransactionId();
113
114         LOG.debug("readyTransaction : {}", transactionID);
115
116         getShardActor().forward(new ForwardedReadyTransaction(transactionID, batched.getVersion(),
117                 transaction, batched.isDoCommitOnReady(), batched.getParticipatingShardNames()), getContext());
118
119         // The shard will handle the commit from here so we're no longer needed - self-destruct.
120         getSelf().tell(PoisonPill.getInstance(), getSelf());
121     }
122 }