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.Externalizable;
13 import java.io.IOException;
14 import java.io.ObjectInput;
15 import java.io.ObjectOutput;
16 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
17 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
18 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
19 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 public class ReadDataReply implements VersionedSerializableMessage, Externalizable {
23 private static final long serialVersionUID = 1L;
25 public static final Class<ReadDataReply> SERIALIZABLE_CLASS = ReadDataReply.class;
27 private NormalizedNode<?, ?> normalizedNode;
28 private short version;
30 public ReadDataReply() {
33 public ReadDataReply(NormalizedNode<?, ?> normalizedNode) {
34 this.normalizedNode = normalizedNode;
37 public NormalizedNode<?, ?> getNormalizedNode() {
38 return normalizedNode;
42 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
43 version = in.readShort();
44 normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
48 public void writeExternal(ObjectOutput out) throws IOException {
49 out.writeShort(version);
50 SerializationUtils.serializeNormalizedNode(normalizedNode, out);
54 public Object toSerializable(short toVersion) {
55 if(toVersion >= DataStoreVersions.LITHIUM_VERSION) {
59 return toSerializableReadDataReply(normalizedNode);
63 private static ShardTransactionMessages.ReadDataReply toSerializableReadDataReply(
64 NormalizedNode<?, ?> normalizedNode) {
65 if(normalizedNode != null) {
66 return ShardTransactionMessages.ReadDataReply.newBuilder()
67 .setNormalizedNode(new NormalizedNodeToNodeCodec(null)
68 .encode(normalizedNode).getNormalizedNode()).build();
70 return ShardTransactionMessages.ReadDataReply.newBuilder().build();
75 public static ReadDataReply fromSerializable(Object serializable) {
76 if(serializable instanceof ReadDataReply) {
77 return (ReadDataReply) serializable;
79 ShardTransactionMessages.ReadDataReply o =
80 (ShardTransactionMessages.ReadDataReply) serializable;
81 return new ReadDataReply(new NormalizedNodeToNodeCodec(null).decode(o.getNormalizedNode()));
85 public static ByteString fromSerializableAsByteString(Object serializable) {
86 if(serializable instanceof ReadDataReply) {
87 ReadDataReply r = (ReadDataReply)serializable;
88 return toSerializableReadDataReply(r.getNormalizedNode()).toByteString();
90 ShardTransactionMessages.ReadDataReply o =
91 (ShardTransactionMessages.ReadDataReply) serializable;
92 return o.getNormalizedNode().toByteString();
96 public static boolean isSerializedType(Object message) {
97 return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
98 message instanceof ShardTransactionMessages.ReadDataReply;