2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10 import java.io.ByteArrayInputStream;
11 import java.io.DataInput;
12 import java.io.DataInputStream;
13 import java.io.DataOutput;
14 import java.io.IOException;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 * Provides various utility methods for serialization and de-serialization.
23 * @author Thomas Pantelis
25 public final class SerializationUtils {
26 public static final ThreadLocal<NormalizedNodeDataInput> REUSABLE_READER_TL = new ThreadLocal<>();
28 private SerializationUtils() {
31 public interface Applier<T> {
32 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
35 private static NormalizedNodeDataInput streamReader(final DataInput in) throws IOException {
36 NormalizedNodeDataInput streamReader = REUSABLE_READER_TL.get();
37 if (streamReader == null) {
38 streamReader = NormalizedNodeInputOutput.newDataInput(in);
44 public static <T> void deserializePathAndNode(final DataInput in, final T instance, final Applier<T> applier) {
46 NormalizedNodeDataInput streamReader = streamReader(in);
47 NormalizedNode<?, ?> node = streamReader.readNormalizedNode();
48 YangInstanceIdentifier path = streamReader.readYangInstanceIdentifier();
49 applier.apply(instance, path, node);
50 } catch (IOException e) {
51 throw new IllegalArgumentException("Error deserializing path and Node", e);
55 private static NormalizedNode<?, ?> tryDeserializeNormalizedNode(final DataInput in) throws IOException {
56 boolean present = in.readBoolean();
58 NormalizedNodeDataInput streamReader = streamReader(in);
59 return streamReader.readNormalizedNode();
65 public static NormalizedNode<?, ?> deserializeNormalizedNode(final DataInput in) {
67 return tryDeserializeNormalizedNode(in);
68 } catch (IOException e) {
69 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
73 public static NormalizedNode<?, ?> deserializeNormalizedNode(final byte [] bytes) {
75 return tryDeserializeNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes)));
76 } catch (IOException e) {
77 throw new IllegalArgumentException("Error deserializing NormalizedNode", e);
81 public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode<?, ?> node)
84 out.writeBoolean(true);
86 try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
87 stream.writeNormalizedNode(node);
90 out.writeBoolean(false);
94 public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
96 try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
97 stream.writeYangInstanceIdentifier(path);
101 public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
102 final NormalizedNode<?, ?> node) throws IOException {
103 try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
104 stream.writeNormalizedNode(node);
105 stream.writeYangInstanceIdentifier(path);
109 public static void writePathAndNode(final DataOutput out, final YangInstanceIdentifier path,
110 final NormalizedNode<?, ?> node) throws IOException {
111 try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
112 stream.writeYangInstanceIdentifier(path);
113 stream.writeNormalizedNode(node);
117 public static YangInstanceIdentifier deserializePath(final DataInput in) {
119 NormalizedNodeDataInput streamReader = streamReader(in);
120 return streamReader.readYangInstanceIdentifier();
121 } catch (IOException e) {
122 throw new IllegalArgumentException("Error deserializing path", e);