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 static org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion.MAGNESIUM;
12 import java.io.DataInput;
13 import java.io.DataOutput;
14 import java.io.IOException;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
21 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
22 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
25 * Provides various utility methods for serialization and de-serialization.
27 * @author Thomas Pantelis
29 public final class SerializationUtils {
30 private SerializationUtils() {
35 public interface Applier<T> {
36 void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
39 public static Optional<NormalizedNode<?, ?>> readNormalizedNode(final DataInput in) throws IOException {
40 if (!in.readBoolean()) {
41 return Optional.empty();
43 return Optional.of(NormalizedNodeDataInput.newDataInput(in).readNormalizedNode());
46 public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode<?, ?> node)
48 writeNormalizedNode(out, MAGNESIUM, node);
51 public static void writeNormalizedNode(final DataOutput out,
52 final org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion version,
53 final @Nullable NormalizedNode<?, ?> node) throws IOException {
55 out.writeBoolean(true);
57 try (NormalizedNodeDataOutput stream = version.newDataOutput(out)) {
58 stream.writeNormalizedNode(node);
61 out.writeBoolean(false);
65 public static YangInstanceIdentifier readPath(final DataInput in) throws IOException {
66 return NormalizedNodeDataInput.newDataInput(in).readYangInstanceIdentifier();
69 public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
71 writePath(out, MAGNESIUM, path);
74 public static void writePath(final DataOutput out, final NormalizedNodeStreamVersion version,
75 final @NonNull YangInstanceIdentifier path) throws IOException {
76 try (NormalizedNodeDataOutput stream = version.newDataOutput(out)) {
77 stream.writeYangInstanceIdentifier(path);
81 public static <T> void readNodeAndPath(final DataInput in, final T instance, final Applier<T> applier)
83 final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
84 NormalizedNode<?, ?> node = stream.readNormalizedNode();
85 YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
86 applier.apply(instance, path, node);
89 public static void writeNodeAndPath(final DataOutput out, final NormalizedNodeStreamVersion version,
90 final YangInstanceIdentifier path, final NormalizedNode<?, ?> node) throws IOException {
91 try (NormalizedNodeDataOutput stream = version.newDataOutput(out)) {
92 stream.writeNormalizedNode(node);
93 stream.writeYangInstanceIdentifier(path);
97 public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
98 final NormalizedNode<?, ?> node) throws IOException {
99 writeNodeAndPath(out, MAGNESIUM, path, node);
102 public static <T> void readPathAndNode(final DataInput in, final T instance, final Applier<T> applier)
104 final NormalizedNodeDataInput stream = NormalizedNodeDataInput.newDataInput(in);
105 YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
106 NormalizedNode<?, ?> node = stream.readNormalizedNode();
107 applier.apply(instance, path, node);
110 public static void writePathAndNode(final DataOutput out,
111 final org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion version,
112 final YangInstanceIdentifier path, final NormalizedNode<?, ?> node) throws IOException {
113 try (NormalizedNodeDataOutput stream = version.newDataOutput(out)) {
114 stream.writeYangInstanceIdentifier(path);
115 stream.writeNormalizedNode(node);
119 public static void writePathAndNode(final DataOutput out, final YangInstanceIdentifier path,
120 final NormalizedNode<?, ?> node) throws IOException {
121 writePathAndNode(out, MAGNESIUM, path, node);