Improve segmented journal actor metrics
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
20
21 /**
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
25  * shards.
26  */
27 @Deprecated(since = "9.0.0", forRemoval = true)
28 public final class ReadyLocalTransactionSerializer extends JSerializer {
29     private final ExtendedActorSystem system;
30
31     public ReadyLocalTransactionSerializer(final ExtendedActorSystem system) {
32         this.system = requireNonNull(system);
33     }
34
35     @Override
36     public int identifier() {
37         return 97439437;
38     }
39
40     @Override
41     public boolean includeManifest() {
42         return false;
43     }
44
45     @Override
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());
54
55         readyLocal.getModification().applyToCursor(new BatchedCursor(batched));
56
57         return SerializationUtils.serialize(batched);
58     }
59
60     @Override
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);
67         }
68     }
69
70     private static final class BatchedCursor extends AbstractBatchedModificationsCursor {
71         private final BatchedModifications message;
72
73         BatchedCursor(final BatchedModifications message) {
74             this.message = requireNonNull(message);
75         }
76
77         @Override
78         protected BatchedModifications getModifications() {
79             return message;
80         }
81     }
82 }