Remove use of thread-local input
[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 YangInstanceIdentifier readPath(final DataInput in) throws IOException {
55         return NormalizedNodeInputOutput.newDataInput(in).readYangInstanceIdentifier();
56     }
57
58     public static void writePath(final DataOutput out, final @NonNull YangInstanceIdentifier path)
59             throws IOException {
60         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
61             stream.writeYangInstanceIdentifier(path);
62         }
63     }
64
65     public static <T> void readNodeAndPath(final DataInput in, final T instance, final Applier<T> applier)
66             throws IOException {
67         final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
68         NormalizedNode<?, ?> node = stream.readNormalizedNode();
69         YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
70         applier.apply(instance, path, node);
71     }
72
73     public static void writeNodeAndPath(final DataOutput out, final YangInstanceIdentifier path,
74             final NormalizedNode<?, ?> node) throws IOException {
75         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
76             stream.writeNormalizedNode(node);
77             stream.writeYangInstanceIdentifier(path);
78         }
79     }
80
81     public static <T> void readPathAndNode(final DataInput in, final T instance, final Applier<T> applier)
82             throws IOException {
83         final NormalizedNodeDataInput stream = NormalizedNodeInputOutput.newDataInput(in);
84         YangInstanceIdentifier path = stream.readYangInstanceIdentifier();
85         NormalizedNode<?, ?> node = stream.readNormalizedNode();
86         applier.apply(instance, path, node);
87     }
88
89     public static void writePathAndNode(final DataOutput out, final YangInstanceIdentifier path,
90             final NormalizedNode<?, ?> node) throws IOException {
91         try (NormalizedNodeDataOutput stream = NormalizedNodeInputOutput.newDataOutput(out)) {
92             stream.writeYangInstanceIdentifier(path);
93             stream.writeNormalizedNode(node);
94         }
95     }
96 }