X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FSerializationUtils.java;h=6735cdb684ccaf3f2abc18c1460381c1de61a27b;hb=fba327c801062a6e802f212a0e8b6efee2f6a90e;hp=fc0497280cc59212f4feed17f1b7460c6a215af6;hpb=0ed60f58d8a8d73544df74f5bb4f93c13cd3ca7d;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtils.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtils.java index fc0497280c..6735cdb684 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtils.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtils.java @@ -7,14 +7,12 @@ */ package org.opendaylight.controller.cluster.datastore.node.utils.stream; -import com.google.common.base.Preconditions; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.DataInput; -import java.io.DataInputStream; import java.io.DataOutput; -import java.io.DataOutputStream; import java.io.IOException; +import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @@ -24,117 +22,95 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; * @author Thomas Pantelis */ public final class SerializationUtils { - public static final ThreadLocal REUSABLE_WRITER_TL = new ThreadLocal<>(); - public static final ThreadLocal REUSABLE_READER_TL = new ThreadLocal<>(); + private SerializationUtils() { + } + + @FunctionalInterface public interface Applier { void apply(T instance, YangInstanceIdentifier path, NormalizedNode node); } - private static NormalizedNodeDataOutput streamWriter(DataOutput out) throws IOException { - NormalizedNodeDataOutput streamWriter = REUSABLE_WRITER_TL.get(); - if (streamWriter == null) { - streamWriter = NormalizedNodeInputOutput.newDataOutput(out); + public static Optional> readNormalizedNode(final DataInput in) throws IOException { + if (!in.readBoolean()) { + return Optional.empty(); } - - return streamWriter; + return Optional.of(NormalizedNodeInputOutput.newDataInput(in).readNormalizedNode()); } - private static NormalizedNodeDataInput streamReader(DataInput in) throws IOException { - NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get(); - if (streamReader == null) { - streamReader = NormalizedNodeInputOutput.newDataInput(in); - } - - return streamReader; - } + public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode node) + throws IOException { + if (node != null) { + out.writeBoolean(true); - public static void serializePathAndNode(YangInstanceIdentifier path, NormalizedNode node, - DataOutput out) { - Preconditions.checkNotNull(path); - Preconditions.checkNotNull(node); - try { - NormalizedNodeDataOutput streamWriter = streamWriter(out); - streamWriter.writeNormalizedNode(node); - streamWriter.writeYangInstanceIdentifier(path); - } catch (IOException e) { - throw new IllegalArgumentException(String.format("Error serializing path %s and Node %s", - path, node), e); + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) { + stream.writeNormalizedNode(node); + } + } else { + out.writeBoolean(false); } } - public static void deserializePathAndNode(DataInput in, T instance, Applier applier) { - try { - NormalizedNodeDataInput streamReader = streamReader(in); - NormalizedNode node = streamReader.readNormalizedNode(); - YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier(); - applier.apply(instance, path, node); - } catch (IOException e) { - throw new IllegalArgumentException("Error deserializing path and Node", e); - } - } + public static void writeNormalizedNode(final DataOutput out, final NormalizedNodeStreamVersion version, + final @Nullable NormalizedNode node) throws IOException { + if (node != null) { + out.writeBoolean(true); - private static NormalizedNode tryDeserializeNormalizedNode(DataInput in) throws IOException { - boolean present = in.readBoolean(); - if (present) { - NormalizedNodeDataInput streamReader = streamReader(in); - return streamReader.readNormalizedNode(); + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out, version)) { + stream.writeNormalizedNode(node); + } + } else { + out.writeBoolean(false); } - - return null; } - public static NormalizedNode deserializeNormalizedNode(DataInput in) { - try { - return tryDeserializeNormalizedNode(in); - } catch (IOException e) { - throw new IllegalArgumentException("Error deserializing NormalizedNode", e); - } + public static YangInstanceIdentifier readPath(final DataInput in) throws IOException { + return NormalizedNodeInputOutput.newDataInput(in).readYangInstanceIdentifier(); } - public static NormalizedNode deserializeNormalizedNode(byte [] bytes) { - try { - return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes))); - } catch (IOException e) { - throw new IllegalArgumentException("Error deserializing NormalizedNode", e); + public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path) + throws IOException { + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) { + stream.writeYangInstanceIdentifier(path); } } - public static void serializeNormalizedNode(NormalizedNode node, DataOutput out) { - try { - out.writeBoolean(node != null); - if (node != null) { - NormalizedNodeDataOutput streamWriter = streamWriter(out); - streamWriter.writeNormalizedNode(node); - } - } catch (IOException e) { - throw new IllegalArgumentException(String.format("Error serializing NormalizedNode %s", - node), e); + public static void writePath(final DataOutput out, final NormalizedNodeStreamVersion version, + final @NonNull YangInstanceIdentifier path) throws IOException { + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out, version)) { + stream.writeYangInstanceIdentifier(path); } } - public static byte [] serializeNormalizedNode(NormalizedNode node) { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - serializeNormalizedNode(node, new DataOutputStream(bos)); - return bos.toByteArray(); + public static void readNodeAndPath(final DataInput in, final T instance, final Applier applier) + throws IOException { + final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in); + NormalizedNode node = stream.readNormalizedNode(); + YangInstanceIdentifier path = stream.readYangInstanceIdentifier(); + applier.apply(instance, path, node); } - public static void serializePath(YangInstanceIdentifier path, DataOutput out) { - Preconditions.checkNotNull(path); - try { - NormalizedNodeDataOutput streamWriter = streamWriter(out); - streamWriter.writeYangInstanceIdentifier(path); - } catch (IOException e) { - throw new IllegalArgumentException(String.format("Error serializing path %s", path), e); + public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path, + final NormalizedNode node) throws IOException { + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) { + stream.writeNormalizedNode(node); + stream.writeYangInstanceIdentifier(path); } } - public static YangInstanceIdentifier deserializePath(DataInput in) { - try { - NormalizedNodeDataInput streamReader = streamReader(in); - return streamReader.readYangInstanceIdentifier(); - } catch (IOException e) { - throw new IllegalArgumentException("Error deserializing path", e); + public static void readPathAndNode(final DataInput in, final T instance, final Applier applier) + throws IOException { + final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in); + YangInstanceIdentifier path = stream.readYangInstanceIdentifier(); + NormalizedNode node = stream.readNormalizedNode(); + applier.apply(instance, path, node); + } + + public static void writePathAndNode(final DataOutput out, final YangInstanceIdentifier path, + final NormalizedNode node) throws IOException { + try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) { + stream.writeYangInstanceIdentifier(path); + stream.writeNormalizedNode(node); } } }