Fix forwarding in Shard.handleBatchedModifications
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadyLocalTransactionSerializer.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.controller.cluster.datastore.messages;
9
10 import akka.serialization.JSerializer;
11 import com.google.common.base.Preconditions;
12 import org.apache.commons.lang3.SerializationUtils;
13 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
14
15 /**
16  * Specialized message transformer, which transforms a {@link ReadyLocalTransaction}
17  * into a {@link BatchedModifications} message. This serializer needs to be plugged
18  * into akka serialization to allow forwarding of ReadyLocalTransaction to remote
19  * shards.
20  */
21 public final class ReadyLocalTransactionSerializer extends JSerializer {
22     @Override
23     public int identifier() {
24         return 97439437;
25     }
26
27     @Override
28     public boolean includeManifest() {
29         return false;
30     }
31
32     @Override
33     public byte[] toBinary(final Object obj) {
34         Preconditions.checkArgument(obj instanceof ReadyLocalTransaction, "Unsupported object type %s", obj.getClass());
35         final ReadyLocalTransaction readyLocal = (ReadyLocalTransaction) obj;
36         final BatchedModifications batched = new BatchedModifications(readyLocal.getTransactionID(),
37                 readyLocal.getRemoteVersion(), "");
38         batched.setDoCommitOnReady(readyLocal.isDoCommitOnReady());
39         batched.setTotalMessagesSent(1);
40         batched.setReady(true);
41
42         readyLocal.getModification().applyToCursor(new BatchedCursor(batched));
43
44         return SerializationUtils.serialize(batched);
45     }
46
47     @Override
48     public Object fromBinaryJava(final byte[] bytes, final Class<?> clazz) {
49         return SerializationUtils.deserialize(bytes);
50     }
51
52     private static final class BatchedCursor extends AbstractBatchedModificationsCursor {
53         private final BatchedModifications message;
54
55         BatchedCursor(final BatchedModifications message) {
56             this.message = Preconditions.checkNotNull(message);
57         }
58
59         @Override
60         protected BatchedModifications getModifications() {
61             return message;
62         }
63     }
64 }