2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore.messages;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.ExtendedActorSystem;
14 import akka.serialization.JSerializer;
15 import akka.util.ClassLoaderObjectInputStream;
16 import java.io.ByteArrayInputStream;
17 import java.io.IOException;
18 import org.apache.commons.lang3.SerializationUtils;
19 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
22 * Specialized message transformer, which transforms a {@link ReadyLocalTransaction}
23 * into a {@link BatchedModifications} message. This serializer needs to be plugged
24 * into akka serialization to allow forwarding of ReadyLocalTransaction to remote
27 public final class ReadyLocalTransactionSerializer extends JSerializer {
29 private final ExtendedActorSystem system;
31 public ReadyLocalTransactionSerializer(final ExtendedActorSystem system) {
32 this.system = requireNonNull(system);
36 public int identifier() {
41 public boolean includeManifest() {
46 public byte[] toBinary(final Object obj) {
47 checkArgument(obj instanceof ReadyLocalTransaction, "Unsupported object type %s", obj.getClass());
48 final ReadyLocalTransaction readyLocal = (ReadyLocalTransaction) obj;
49 final BatchedModifications batched = new BatchedModifications(readyLocal.getTransactionId(),
50 readyLocal.getRemoteVersion());
51 batched.setDoCommitOnReady(readyLocal.isDoCommitOnReady());
52 batched.setTotalMessagesSent(1);
53 batched.setReady(readyLocal.getParticipatingShardNames());
55 readyLocal.getModification().applyToCursor(new BatchedCursor(batched));
57 return SerializationUtils.serialize(batched);
61 public Object fromBinaryJava(final byte[] bytes, final Class<?> clazz) {
62 try (ClassLoaderObjectInputStream is = new ClassLoaderObjectInputStream(system.dynamicAccess().classLoader(),
63 new ByteArrayInputStream(bytes))) {
64 return is.readObject();
65 } catch (IOException | ClassNotFoundException e) {
66 throw new IllegalStateException("Failed to deserialize object", e);
70 private static final class BatchedCursor extends AbstractBatchedModificationsCursor {
71 private final BatchedModifications message;
73 BatchedCursor(final BatchedModifications message) {
74 this.message = requireNonNull(message);
78 protected BatchedModifications getModifications() {