Merge changes I3e404877,Ida2a5c32,I9e6ce426,I6a4b90f6,I79717533
[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.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;
21
22 public class ReadDataReply implements VersionedSerializableMessage, Externalizable {
23     private static final long serialVersionUID = 1L;
24
25     public static final Class<ReadDataReply> SERIALIZABLE_CLASS = ReadDataReply.class;
26
27     private NormalizedNode<?, ?> normalizedNode;
28     private short version;
29
30     public ReadDataReply() {
31     }
32
33     public ReadDataReply(NormalizedNode<?, ?> normalizedNode) {
34         this.normalizedNode = normalizedNode;
35     }
36
37     public NormalizedNode<?, ?> getNormalizedNode() {
38         return normalizedNode;
39     }
40
41     @Override
42     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
43         version = in.readShort();
44         normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
45     }
46
47     @Override
48     public void writeExternal(ObjectOutput out) throws IOException {
49         out.writeShort(version);
50         SerializationUtils.serializeNormalizedNode(normalizedNode, out);
51     }
52
53     @Override
54     public Object toSerializable(short toVersion) {
55         if(toVersion >= DataStoreVersions.LITHIUM_VERSION) {
56             version = toVersion;
57             return this;
58         } else {
59             return toSerializableReadDataReply(normalizedNode);
60         }
61     }
62
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();
69         } else {
70             return ShardTransactionMessages.ReadDataReply.newBuilder().build();
71
72         }
73     }
74
75     public static ReadDataReply fromSerializable(Object serializable) {
76         if(serializable instanceof ReadDataReply) {
77             return (ReadDataReply) serializable;
78         } else {
79             ShardTransactionMessages.ReadDataReply o =
80                     (ShardTransactionMessages.ReadDataReply) serializable;
81             return new ReadDataReply(new NormalizedNodeToNodeCodec(null).decode(o.getNormalizedNode()));
82         }
83     }
84
85     public static ByteString fromSerializableAsByteString(Object serializable) {
86         if(serializable instanceof ReadDataReply) {
87             ReadDataReply r = (ReadDataReply)serializable;
88             return toSerializableReadDataReply(r.getNormalizedNode()).toByteString();
89         } else {
90             ShardTransactionMessages.ReadDataReply o =
91                     (ShardTransactionMessages.ReadDataReply) serializable;
92             return o.getNormalizedNode().toByteString();
93         }
94     }
95
96     public static boolean isSerializedType(Object message) {
97         return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
98                message instanceof ShardTransactionMessages.ReadDataReply;
99     }
100 }