Merge "Removing { } from NormalizedNodeJsonBodyWriter"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadDataReply.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.datastore.messages;
10
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;
20
21 public class ReadDataReply extends VersionedExternalizableMessage {
22     private static final long serialVersionUID = 1L;
23
24     public static final Class<ReadDataReply> SERIALIZABLE_CLASS = ReadDataReply.class;
25
26     private NormalizedNode<?, ?> normalizedNode;
27
28     public ReadDataReply() {
29     }
30
31     public ReadDataReply(NormalizedNode<?, ?> normalizedNode, short version) {
32         super(version);
33         this.normalizedNode = normalizedNode;
34     }
35
36     public NormalizedNode<?, ?> getNormalizedNode() {
37         return normalizedNode;
38     }
39
40     @Override
41     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
42         super.readExternal(in);
43         normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
44     }
45
46     @Override
47     public void writeExternal(ObjectOutput out) throws IOException {
48         super.writeExternal(out);
49         SerializationUtils.serializeNormalizedNode(normalizedNode, out);
50     }
51
52     @Override
53     public Object toSerializable() {
54         if(getVersion() >= DataStoreVersions.LITHIUM_VERSION) {
55             return this;
56         } else {
57             return toSerializableReadDataReply(normalizedNode);
58         }
59     }
60
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();
67         } else {
68             return ShardTransactionMessages.ReadDataReply.newBuilder().build();
69
70         }
71     }
72
73     public static ReadDataReply fromSerializable(Object serializable) {
74         if(serializable instanceof ReadDataReply) {
75             return (ReadDataReply) serializable;
76         } else {
77             ShardTransactionMessages.ReadDataReply o =
78                     (ShardTransactionMessages.ReadDataReply) serializable;
79             return new ReadDataReply(new NormalizedNodeToNodeCodec(null).decode(o.getNormalizedNode()),
80                     DataStoreVersions.HELIUM_2_VERSION);
81         }
82     }
83
84     public static ByteString fromSerializableAsByteString(Object serializable) {
85         if(serializable instanceof ReadDataReply) {
86             ReadDataReply r = (ReadDataReply)serializable;
87             return toSerializableReadDataReply(r.getNormalizedNode()).toByteString();
88         } else {
89             ShardTransactionMessages.ReadDataReply o =
90                     (ShardTransactionMessages.ReadDataReply) serializable;
91             return o.getNormalizedNode().toByteString();
92         }
93     }
94
95     public static boolean isSerializedType(Object message) {
96         return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
97                message instanceof ShardTransactionMessages.ReadDataReply;
98     }
99 }