Fix ReadyLocalTransactionSerializer
[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.actor.ExtendedActorSystem;
11 import akka.serialization.JSerializer;
12 import akka.util.ClassLoaderObjectInputStream;
13 import com.google.common.base.Preconditions;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import org.apache.commons.lang3.SerializationUtils;
17 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
18
19 /**
20  * Specialized message transformer, which transforms a {@link ReadyLocalTransaction}
21  * into a {@link BatchedModifications} message. This serializer needs to be plugged
22  * into akka serialization to allow forwarding of ReadyLocalTransaction to remote
23  * shards.
24  */
25 public final class ReadyLocalTransactionSerializer extends JSerializer {
26
27     private final ExtendedActorSystem system;
28
29     public ReadyLocalTransactionSerializer(final ExtendedActorSystem system) {
30         this.system = Preconditions.checkNotNull(system);
31     }
32
33     @Override
34     public int identifier() {
35         return 97439437;
36     }
37
38     @Override
39     public boolean includeManifest() {
40         return false;
41     }
42
43     @Override
44     public byte[] toBinary(final Object obj) {
45         Preconditions.checkArgument(obj instanceof ReadyLocalTransaction, "Unsupported object type %s", obj.getClass());
46         final ReadyLocalTransaction readyLocal = (ReadyLocalTransaction) obj;
47         final BatchedModifications batched = new BatchedModifications(readyLocal.getTransactionId(),
48                 readyLocal.getRemoteVersion());
49         batched.setDoCommitOnReady(readyLocal.isDoCommitOnReady());
50         batched.setTotalMessagesSent(1);
51         batched.setReady(true);
52
53         readyLocal.getModification().applyToCursor(new BatchedCursor(batched));
54
55         return SerializationUtils.serialize(batched);
56     }
57
58     @Override
59     public Object fromBinaryJava(final byte[] bytes, final Class<?> clazz) {
60         try (ClassLoaderObjectInputStream is = new ClassLoaderObjectInputStream(system.dynamicAccess().classLoader(),
61             new ByteArrayInputStream(bytes))) {
62             return is.readObject();
63         } catch (IOException | ClassNotFoundException e) {
64             throw new IllegalStateException("Failed to deserialize object", e);
65         }
66     }
67
68     private static final class BatchedCursor extends AbstractBatchedModificationsCursor {
69         private final BatchedModifications message;
70
71         BatchedCursor(final BatchedModifications message) {
72             this.message = Preconditions.checkNotNull(message);
73         }
74
75         @Override
76         protected BatchedModifications getModifications() {
77             return message;
78         }
79     }
80 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.