Hide NormalizedNodeOutputStreamWriter
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / SerializationUtils.java
index 5854932a6fa0d999fe368aa61bfdd252821739c6..b416b922de75be07c9b1e392dd21757444ddcde9 100644 (file)
@@ -17,8 +17,11 @@ import java.io.DataOutput;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import org.opendaylight.controller.cluster.datastore.node.NormalizedNodeToNodeCodec;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.InvalidNormalizedNodeStreamException;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataInput;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
+import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputStreamReader;
-import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeOutputStreamWriter;
 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -29,23 +32,23 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
  * @author Thomas Pantelis
  */
 public final class SerializationUtils {
-    public static ThreadLocal<NormalizedNodeOutputStreamWriter> REUSABLE_WRITER_TL = new ThreadLocal<>();
-    public static ThreadLocal<NormalizedNodeInputStreamReader> REUSABLE_READER_TL = new ThreadLocal<>();
+    public static final ThreadLocal<NormalizedNodeDataOutput> REUSABLE_WRITER_TL = new ThreadLocal<>();
+    public static final ThreadLocal<NormalizedNodeInputStreamReader> REUSABLE_READER_TL = new ThreadLocal<>();
 
     public static interface Applier<T> {
         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
     }
 
-    private static NormalizedNodeOutputStreamWriter streamWriter(DataOutput out) throws IOException {
-        NormalizedNodeOutputStreamWriter streamWriter = REUSABLE_WRITER_TL.get();
+    private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException {
+        NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get();
         if(streamWriter == null) {
-            streamWriter = new NormalizedNodeOutputStreamWriter(out);
+            streamWriter = NormalizedNodeInputOutput.newDataOutput(out);
         }
 
         return streamWriter;
     }
 
-    private static NormalizedNodeInputStreamReader streamReader(DataInput in) throws IOException {
+    private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException {
         NormalizedNodeInputStreamReader streamWriter = REUSABLE_READER_TL.get();
         if(streamWriter == null) {
             streamWriter = new NormalizedNodeInputStreamReader(in);
@@ -59,7 +62,7 @@ public final class SerializationUtils {
         Preconditions.checkNotNull(path);
         Preconditions.checkNotNull(node);
         try {
-            NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
+            NormalizedNodeDataOutput streamWriter = streamWriter(out);
             streamWriter.writeNormalizedNode(node);
             streamWriter.writeYangInstanceIdentifier(path);
         } catch (IOException e) {
@@ -70,7 +73,7 @@ public final class SerializationUtils {
 
     public static <T> void deserializePathAndNode(DataInput in, T instance, Applier<T> applier) {
         try {
-            NormalizedNodeInputStreamReader streamReader = streamReader(in);
+            NormalizedNodeDataInput streamReader = streamReader(in);
             NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
             YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
             applier.apply(instance, path, node);
@@ -83,7 +86,7 @@ public final class SerializationUtils {
         try {
             out.writeBoolean(node != null);
             if(node != null) {
-                NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
+                NormalizedNodeDataOutput streamWriter = streamWriter(out);
                 streamWriter.writeNormalizedNode(node);
             }
         } catch (IOException e) {
@@ -93,15 +96,19 @@ public final class SerializationUtils {
     }
 
     public static NormalizedNode<?, ?> deserializeNormalizedNode(DataInput in) {
-            try {
-                boolean present = in.readBoolean();
-                if(present) {
-                    NormalizedNodeInputStreamReader streamReader = streamReader(in);
-                    return streamReader.readNormalizedNode();
-                }
-            } catch (IOException e) {
-                throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
-            }
+        try {
+            return tryDeserializeNormalizedNode(in);
+        } catch (IOException e) {
+            throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
+        }
+    }
+
+    private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(DataInput in) throws IOException {
+        boolean present = in.readBoolean();
+        if(present) {
+            NormalizedNodeDataInput streamReader = streamReader(in);
+            return streamReader.readNormalizedNode();
+        }
 
         return null;
     }
@@ -109,18 +116,17 @@ public final class SerializationUtils {
     public static NormalizedNode<?, ?> deserializeNormalizedNode(byte [] bytes) {
         NormalizedNode<?, ?> node = null;
         try {
-            node = deserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
-        } catch(Exception e) {
-        }
-
-        if(node == null) {
-            // Must be from legacy protobuf serialization - try that.
+            node = tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
+        } catch(InvalidNormalizedNodeStreamException e) {
+            // Probably from legacy protobuf serialization - try that.
             try {
                 NormalizedNodeMessages.Node serializedNode = NormalizedNodeMessages.Node.parseFrom(bytes);
                 node =  new NormalizedNodeToNodeCodec(null).decode(serializedNode);
-            } catch (InvalidProtocolBufferException e) {
+            } catch (InvalidProtocolBufferException e2) {
                 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
             }
+        } catch (IOException e) {
+            throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
         }
 
         return node;
@@ -135,7 +141,7 @@ public final class SerializationUtils {
     public static void serializePath(YangInstanceIdentifier path, DataOutput out) {
         Preconditions.checkNotNull(path);
         try {
-            NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out);
+            NormalizedNodeDataOutput streamWriter = streamWriter(out);
             streamWriter.writeYangInstanceIdentifier(path);
         } catch (IOException e) {
             throw new IllegalArgumentException(String.format("Error serializing path %s", path), e);
@@ -144,7 +150,7 @@ public final class SerializationUtils {
 
     public static YangInstanceIdentifier deserializePath(DataInput in) {
         try {
-            NormalizedNodeInputStreamReader streamReader = streamReader(in);
+            NormalizedNodeDataInput streamReader = streamReader(in);
             return streamReader.readYangInstanceIdentifier();
         } catch (IOException e) {
             throw new IllegalArgumentException("Error deserializing path", e);