Merge "BUG 2412 - restconf @GET getModule(identifier,uri) method migration"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / messages / ReadDataReply.java
index a8926be77979e19882c84be8a1a5fd8fd4ac1ea2..b0c163d87f346ccaefc300ce38f88573ab033b17 100644 (file)
@@ -8,41 +8,92 @@
 
 package org.opendaylight.controller.cluster.datastore.messages;
 
+import com.google.protobuf.ByteString;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
+import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
-public class ReadDataReply implements SerializableMessage{
-  private final NormalizedNode<?, ?> normalizedNode;
-  private final SchemaContext schemaContext;
-  public static final Class SERIALIZABLE_CLASS = ShardTransactionMessages.ReadDataReply.class;
-  public ReadDataReply(SchemaContext context,NormalizedNode<?, ?> normalizedNode){
+public class ReadDataReply extends VersionedExternalizableMessage {
+    private static final long serialVersionUID = 1L;
 
-    this.normalizedNode = normalizedNode;
-    this.schemaContext = context;
-  }
+    public static final Class<ReadDataReply> SERIALIZABLE_CLASS = ReadDataReply.class;
 
-  public NormalizedNode<?, ?> getNormalizedNode() {
-    return normalizedNode;
-  }
+    private NormalizedNode<?, ?> normalizedNode;
 
-  public Object toSerializable(){
-    if(normalizedNode != null) {
-      return ShardTransactionMessages.ReadDataReply.newBuilder()
-          .setNormalizedNode(new NormalizedNodeToNodeCodec(schemaContext)
-                  .encode(InstanceIdentifier.builder().build(), normalizedNode).getNormalizedNode()
-          ).build();
-    }else{
-      return ShardTransactionMessages.ReadDataReply.newBuilder().build();
+    public ReadDataReply() {
+    }
+
+    public ReadDataReply(NormalizedNode<?, ?> normalizedNode, short version) {
+        super(version);
+        this.normalizedNode = normalizedNode;
+    }
+
+    public NormalizedNode<?, ?> getNormalizedNode() {
+        return normalizedNode;
+    }
+
+    @Override
+    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        super.readExternal(in);
+        normalizedNode = SerializationUtils.deserializeNormalizedNode(in);
+    }
+
+    @Override
+    public void writeExternal(ObjectOutput out) throws IOException {
+        super.writeExternal(out);
+        SerializationUtils.serializeNormalizedNode(normalizedNode, out);
+    }
+
+    @Override
+    public Object toSerializable() {
+        if(getVersion() >= DataStoreVersions.LITHIUM_VERSION) {
+            return this;
+        } else {
+            return toSerializableReadDataReply(normalizedNode);
+        }
+    }
+
+    private static ShardTransactionMessages.ReadDataReply toSerializableReadDataReply(
+            NormalizedNode<?, ?> normalizedNode) {
+        if(normalizedNode != null) {
+            return ShardTransactionMessages.ReadDataReply.newBuilder()
+                    .setNormalizedNode(new NormalizedNodeToNodeCodec(null)
+                    .encode(normalizedNode).getNormalizedNode()).build();
+        } else {
+            return ShardTransactionMessages.ReadDataReply.newBuilder().build();
 
+        }
     }
 
-  }
+    public static ReadDataReply fromSerializable(Object serializable) {
+        if(serializable instanceof ReadDataReply) {
+            return (ReadDataReply) serializable;
+        } else {
+            ShardTransactionMessages.ReadDataReply o =
+                    (ShardTransactionMessages.ReadDataReply) serializable;
+            return new ReadDataReply(new NormalizedNodeToNodeCodec(null).decode(o.getNormalizedNode()),
+                    DataStoreVersions.HELIUM_2_VERSION);
+        }
+    }
+
+    public static ByteString fromSerializableAsByteString(Object serializable) {
+        if(serializable instanceof ReadDataReply) {
+            ReadDataReply r = (ReadDataReply)serializable;
+            return toSerializableReadDataReply(r.getNormalizedNode()).toByteString();
+        } else {
+            ShardTransactionMessages.ReadDataReply o =
+                    (ShardTransactionMessages.ReadDataReply) serializable;
+            return o.getNormalizedNode().toByteString();
+        }
+    }
 
-  public static ReadDataReply fromSerializable(SchemaContext schemaContext,InstanceIdentifier id,Object serializable){
-    ShardTransactionMessages.ReadDataReply o = (ShardTransactionMessages.ReadDataReply) serializable;
-    return new ReadDataReply(schemaContext,new NormalizedNodeToNodeCodec(schemaContext).decode(id, o.getNormalizedNode()));
-  }
+    public static boolean isSerializedType(Object message) {
+        return SERIALIZABLE_CLASS.isAssignableFrom(message.getClass()) ||
+               message instanceof ShardTransactionMessages.ReadDataReply;
+    }
 }