2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore.messages;
11 import com.google.protobuf.ByteString;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
16 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
17 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
18 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 public class ReadDataReply extends VersionedExternalizableMessage {
22 private static final long serialVersionUID = 1L;
24 public static final Class<ReadDataReply> SERIALIZABLE_CLASS = ReadDataReply.class;
26 private NormalizedNode<?, ?> normalizedNode;
28 public ReadDataReply() {
31 public ReadDataReply(NormalizedNode<?, ?> normalizedNode, short version) {
33 this.normalizedNode = normalizedNode;
36 public NormalizedNode<?, ?> getNormalizedNode() {
37 return normalizedNode;
41 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
42 super.readExternal(in);
43 normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
47 public void writeExternal(ObjectOutput out) throws IOException {
48 super.writeExternal(out);
49 SerializationUtils.serializeNormalizedNode(normalizedNode, out);
53 public Object toSerializable() {
54 if(getVersion() >= DataStoreVersions.LITHIUM_VERSION) {
57 return toSerializableReadDataReply(normalizedNode);
61 private static ShardTransactionMessages.ReadDataReply toSerializableReadDataReply(
62 NormalizedNode<?, ?> normalizedNode) {
63 if(normalizedNode != null) {
64 return ShardTransactionMessages.ReadDataReply.newBuilder()
65 .setNormalizedNode(new NormalizedNodeToNodeCodec(null)
66 .encode(normalizedNode).getNormalizedNode()).build();
68 return ShardTransactionMessages.ReadDataReply.newBuilder().build();
73 public static ReadDataReply fromSerializable(Object serializable) {
74 if(serializable instanceof ReadDataReply) {
75 return (ReadDataReply) serializable;
77 ShardTransactionMessages.ReadDataReply o =
78 (ShardTransactionMessages.ReadDataReply) serializable;
79 return new ReadDataReply(new NormalizedNodeToNodeCodec(null).decode(o.getNormalizedNode()),
80 DataStoreVersions.HELIUM_2_VERSION);
84 public static ByteString fromSerializableAsByteString(Object serializable) {
85 if(serializable instanceof ReadDataReply) {
86 ReadDataReply r = (ReadDataReply)serializable;
87 return toSerializableReadDataReply(r.getNormalizedNode()).toByteString();
89 ShardTransactionMessages.ReadDataReply o =
90 (ShardTransactionMessages.ReadDataReply) serializable;
91 return o.getNormalizedNode().toByteString();
95 public static boolean isSerializedType(Object message) {
96 return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
97 message instanceof ShardTransactionMessages.ReadDataReply;