*/
package org.opendaylight.controller.cluster.datastore.node.utils.stream;
-import java.io.ByteArrayInputStream;
import java.io.DataInput;
-import java.io.DataInputStream;
import java.io.DataOutput;
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;
* @author Thomas Pantelis
*/
public final class SerializationUtils {
- public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
-
private SerializationUtils() {
+
}
+ @FunctionalInterface
public interface Applier<T> {
void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
}
- private static NormalizedNodeDataInput streamReader(final DataInput in) throws IOException {
- NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
- if (streamReader == null) {
- streamReader = NormalizedNodeInputOutput.newDataInput(in);
- }
-
- return streamReader;
- }
-
- public static <T> void deserializePathAndNode(final DataInput in, final T instance, final Applier<T> 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);
- }
- }
-
- private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(final DataInput in) throws IOException {
- boolean present = in.readBoolean();
- if (present) {
- NormalizedNodeDataInput streamReader = streamReader(in);
- return streamReader.readNormalizedNode();
- }
-
- return null;
- }
-
- public static NormalizedNode<?, ?> deserializeNormalizedNode(final DataInput in) {
- try {
- return tryDeserializeNormalizedNode(in);
- } catch (IOException e) {
- throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
- }
- }
-
- public static NormalizedNode<?, ?> deserializeNormalizedNode(final byte [] bytes) {
- try {
- return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
- } catch (IOException e) {
- throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
+ public static Optional<NormalizedNode<?, ?>> readNormalizedNode(final DataInput in) throws IOException {
+ if (!in.readBoolean()) {
+ return Optional.empty();
}
+ return Optional.of(NormalizedNodeInputOutput.newDataInput(in).readNormalizedNode());
}
public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode<?, ?> node)
}
}
+ public static YangInstanceIdentifier readPath(final DataInput in) throws IOException {
+ return NormalizedNodeInputOutput.newDataInput(in).readYangInstanceIdentifier();
+ }
+
public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
throws IOException {
try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
}
}
+ public static <T> void readNodeAndPath(final DataInput in, final T instance, final Applier<T> 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 writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
final NormalizedNode<?, ?> node) throws IOException {
try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
}
}
+ public static <T> void readPathAndNode(final DataInput in, final T instance, final Applier<T> 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.writeNormalizedNode(node);
}
}
-
- public static YangInstanceIdentifier deserializePath(final DataInput in) {
- try {
- NormalizedNodeDataInput streamReader = streamReader(in);
- return streamReader.readYangInstanceIdentifier();
- } catch (IOException e) {
- throw new IllegalArgumentException("Error deserializing path", e);
- }
- }
}
public void testSerializeDeserializeNodes() throws IOException {
final NormalizedNode<?, ?> normalizedNode = createNormalizedNode();
final byte[] bytes = serializeNormalizedNode(normalizedNode);
- Assert.assertEquals(normalizedNode, SerializationUtils.deserializeNormalizedNode(bytes));
+ Assert.assertEquals(normalizedNode, deserializeNormalizedNode(bytes));
}
@Test
.withValue(new DOMSource(parse))
.build();
final byte[] bytes = serializeNormalizedNode(anyXmlNode);
- final NormalizedNode<?, ?> deserialized = SerializationUtils.deserializeNormalizedNode(bytes);
+ final NormalizedNode<?, ?> deserialized = deserializeNormalizedNode(bytes);
final DOMSource value = (DOMSource) deserialized.getValue();
final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(),
value.getNode().getOwnerDocument());
.build();
SerializationUtils.writePath(out, path);
final YangInstanceIdentifier deserialized =
- SerializationUtils.deserializePath(new DataInputStream(new ByteArrayInputStream(bos.toByteArray())));
+ SerializationUtils.readPath(new DataInputStream(new ByteArrayInputStream(bos.toByteArray())));
Assert.assertEquals(path, deserialized);
}
SerializationUtils.writeNodeAndPath(out, path, node);
final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
final AtomicBoolean applierCalled = new AtomicBoolean(false);
- SerializationUtils.deserializePathAndNode(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
+ SerializationUtils.readNodeAndPath(in, applierCalled, (instance, deserializedPath, deserializedNode) -> {
Assert.assertEquals(path, deserializedPath);
Assert.assertEquals(node, deserializedNode);
applierCalled.set(true);
Assert.assertTrue(applierCalled.get());
}
+ private static NormalizedNode<?, ?> deserializeNormalizedNode(final byte [] bytes) throws IOException {
+ return SerializationUtils.readNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes))).get();
+ }
+
private static byte[] serializeNormalizedNode(final NormalizedNode<?, ?> node) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
SerializationUtils.writeNormalizedNode(new DataOutputStream(bos), node);
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.SerializationUtils;
-import org.opendaylight.controller.cluster.datastore.node.utils.stream.SerializationUtils.Applier;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
}
@Override
- public void readExternal(final ObjectInput in) {
- SerializationUtils.deserializePathAndNode(in, this, APPLIER);
+ public void readExternal(final ObjectInput in) throws IOException {
+ SerializationUtils.readNodeAndPath(in, this, (instance, path, node) -> {
+ instance.setPath(path);
+ instance.data = node;
+ });
}
@Override
out.writeNormalizedNode(data);
out.writeYangInstanceIdentifier(getPath());
}
-
- private static final Applier<WriteModification> APPLIER = (instance, path, node) -> {
- instance.setPath(path);
- instance.data = node;
- };
}