X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Futils%2FSerializationUtils.java;h=2e3cfd0e3a1f96d62b329c30ed1c34473235d52c;hp=bf9f8d803ac00f0e8ce2cbe983f81bfb9e9a44d0;hb=599f10fc4a52c41583b719582b6ff36a0d53ccae;hpb=fdc585863663ca5a4baf83ef146390e3c845a567 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/SerializationUtils.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/SerializationUtils.java index bf9f8d803a..2e3cfd0e3a 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/SerializationUtils.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/SerializationUtils.java @@ -8,7 +8,6 @@ package org.opendaylight.controller.cluster.datastore.utils; import com.google.common.base.Preconditions; -import com.google.protobuf.InvalidProtocolBufferException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInput; @@ -16,11 +15,9 @@ import java.io.DataInputStream; 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.NormalizedNodeInputStreamReader; -import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeOutputStreamWriter; -import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages; +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.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -28,31 +25,33 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; * Provides various utility methods for serialization and de-serialization. * * @author Thomas Pantelis + * @deprecated use {@link org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils} instead */ +@Deprecated public final class SerializationUtils { - public static ThreadLocal REUSABLE_WRITER_TL = new ThreadLocal<>(); - public static ThreadLocal REUSABLE_READER_TL = new ThreadLocal<>(); + public static final ThreadLocal REUSABLE_WRITER_TL = new ThreadLocal<>(); + public static final ThreadLocal REUSABLE_READER_TL = new ThreadLocal<>(); - public static interface Applier { + public interface Applier { void apply(T instance, YangInstanceIdentifier path, NormalizedNode node); } - private static NormalizedNodeOutputStreamWriter streamWriter(DataOutput out) throws IOException { - NormalizedNodeOutputStreamWriter streamWriter = REUSABLE_WRITER_TL.get(); - if(streamWriter == null) { - streamWriter = new NormalizedNodeOutputStreamWriter(out); + private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException { + NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get(); + if (streamWriter == null) { + streamWriter = NormalizedNodeInputOutput.newDataOutput(out); } return streamWriter; } - private static NormalizedNodeInputStreamReader streamReader(DataInput in) throws IOException { - NormalizedNodeInputStreamReader streamWriter = REUSABLE_READER_TL.get(); - if(streamWriter == null) { - streamWriter = new NormalizedNodeInputStreamReader(in); + private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException { + NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get(); + if (streamReader == null) { + streamReader = NormalizedNodeInputOutput.newDataInput(in); } - return streamWriter; + return streamReader; } public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode node, @@ -60,7 +59,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) { @@ -71,7 +70,7 @@ public final class SerializationUtils { public static void deserializePathAndNode(DataInput in, T instance, Applier applier) { try { - NormalizedNodeInputStreamReader streamReader = streamReader(in); + NormalizedNodeDataInput streamReader = streamReader(in); NormalizedNode node = streamReader.readNormalizedNode(); YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier(); applier.apply(instance, path, node); @@ -80,17 +79,14 @@ public final class SerializationUtils { } } - public static void serializeNormalizedNode(NormalizedNode node, DataOutput out) { - try { - out.writeBoolean(node != null); - if(node != null) { - NormalizedNodeOutputStreamWriter streamWriter = streamWriter(out); - streamWriter.writeNormalizedNode(node); - } - } catch (IOException e) { - throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s", - node), e); + private static NormalizedNode tryDeserializeNormalizedNode(DataInput in) throws IOException { + boolean present = in.readBoolean(); + if (present) { + NormalizedNodeDataInput streamReader = streamReader(in); + return streamReader.readNormalizedNode(); } + + return null; } public static NormalizedNode deserializeNormalizedNode(DataInput in) { @@ -101,33 +97,25 @@ public final class SerializationUtils { } } - private static NormalizedNode tryDeserializeNormalizedNode(DataInput in) throws IOException { - boolean present = in.readBoolean(); - if(present) { - NormalizedNodeInputStreamReader streamReader = streamReader(in); - return streamReader.readNormalizedNode(); + public static NormalizedNode deserializeNormalizedNode(byte [] bytes) { + try { + return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes))); + } catch (IOException e) { + throw new IllegalArgumentException("Error deserializing NormalizedNode", e); } - - return null; } - public static NormalizedNode deserializeNormalizedNode(byte [] bytes) { - NormalizedNode node = null; + public static void serializeNormalizedNode(NormalizedNode node, DataOutput out) { try { - 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 e2) { - throw new IllegalArgumentException("Error deserializing NormalizedNode", e); + out.writeBoolean(node != null); + if (node != null) { + NormalizedNodeDataOutput streamWriter = streamWriter(out); + streamWriter.writeNormalizedNode(node); } } catch (IOException e) { - throw new IllegalArgumentException("Error deserializing NormalizedNode", e); + throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s", + node), e); } - - return node; } public static byte [] serializeNormalizedNode(NormalizedNode node) { @@ -139,7 +127,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); @@ -148,7 +136,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);