6735cdb684ccaf3f2abc18c1460381c1de61a27b
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / SerializationUtils.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
9
10 import java.io.DataInput;
11 import java.io.DataOutput;
12 import java.io.IOException;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18
19 /**
20  * Provides various utility methods for serialization and de-serialization.
21  *
22  * @author Thomas Pantelis
23  */
24 public final class SerializationUtils {
25     private SerializationUtils() {
26
27     }
28
29     @FunctionalInterface
30     public interface Applier<T> {
31         void apply(T instance, YangInstanceIdentifier path, NormalizedNode<?, ?> node);
32     }
33
34     public static Optional<NormalizedNode<?, ?>> readNormalizedNode(final DataInput in) throws IOException {
35         if (!in.readBoolean()) {
36             return Optional.empty();
37         }
38         return Optional.of(NormalizedNodeInputOutput.newDataInput(in).readNormalizedNode());
39     }
40
41     public static void writeNormalizedNode(final DataOutput out, final @Nullable NormalizedNode<?, ?> node)
42             throws IOException {
43         if (node != null) {
44             out.writeBoolean(true);
45
46             try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
47                 stream.writeNormalizedNode(node);
48             }
49         } else {
50             out.writeBoolean(false);
51         }
52     }
53
54     public static void writeNormalizedNode(final DataOutput out, final NormalizedNodeStreamVersion version,
55             final @Nullable NormalizedNode<?, ?> node) throws IOException {
56         if (node != null) {
57             out.writeBoolean(true);
58
59             try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out, version)) {
60                 stream.writeNormalizedNode(node);
61             }
62         } else {
63             out.writeBoolean(false);
64         }
65     }
66
67     public static YangInstanceIdentifier readPath(final DataInput in) throws IOException {
68         return NormalizedNodeInputOutput.newDataInput(in).readYangInstanceIdentifier();
69     }
70
71     public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
72             throws IOException {
73         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
74             stream.writeYangInstanceIdentifier(path);
75         }
76     }
77
78     public static void writePath(final DataOutput out, final NormalizedNodeStreamVersion version,
79             final @NonNull YangInstanceIdentifier path) throws IOException {
80         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out, version)) {
81             stream.writeYangInstanceIdentifier(path);
82         }
83     }
84
85     public static <T> void readNodeAndPath(final DataInput in, final T instance, final Applier<T> applier)
86             throws IOException {
87         final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
88         NormalizedNode<?, ?> node = stream.readNormalizedNode();
89         YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
90         applier.apply(instance, path, node);
91     }
92
93     public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
94             final NormalizedNode<?, ?> node) throws IOException {
95         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
96             stream.writeNormalizedNode(node);
97             stream.writeYangInstanceIdentifier(path);
98         }
99     }
100
101     public static <T> void readPathAndNode(final DataInput in, final T instance, final Applier<T> applier)
102             throws IOException {
103         final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
104         YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
105         NormalizedNode<?, ?> node = stream.readNormalizedNode();
106         applier.apply(instance, path, node);
107     }
108
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);
114         }
115     }
116 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.