a5827238941a20668dfe25a05943714f9d6ce821
[yangtools.git] / codec / yang-data-codec-binfmt / src / main / java / org / opendaylight / yangtools / yang / data / codec / binfmt / NormalizedNodeDataInput.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco 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.yangtools.yang.data.codec.binfmt;
9
10 import java.io.DataInput;
11 import java.io.IOException;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.Either;
15 import org.opendaylight.yangtools.yang.common.QNameAwareDataInput;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
20 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
23
24 /**
25  * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
26  * and {@link SchemaNodeIdentifier}s.
27  */
28 public interface NormalizedNodeDataInput extends QNameAwareDataInput {
29     /**
30      * Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
31      *
32      * @param writer Writer to emit events to
33      * @throws IOException if an error occurs
34      * @throws IllegalStateException if the dictionary has been detached
35      * @throws NullPointerException if {@code writer} is {@code null}
36      */
37     void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
38
39     /**
40      * Read a normalized node from the reader.
41      *
42      * @return Next node from the stream, or null if end of stream has been reached.
43      * @throws IOException if an error occurs
44      * @throws IllegalStateException if the dictionary has been detached
45      */
46     default NormalizedNode readNormalizedNode() throws IOException {
47         return readNormalizedNode(ReusableImmutableNormalizedNodeStreamWriter.create());
48     }
49
50     /**
51      * Read a normalized node from the reader, using specified writer to construct the result.
52      *
53      * @param receiver Reusable receiver to, expected to be reset
54      * @return Next node from the stream, or null if end of stream has been reached.
55      * @throws IOException if an error occurs
56      * @throws IllegalStateException if the dictionary has been detached
57      * @throws NullPointerException if {@code receiver} is {@code null}
58      */
59     default NormalizedNode readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
60         try {
61             streamNormalizedNode(receiver);
62             return receiver.getResult().data();
63         } finally {
64             receiver.reset();
65         }
66     }
67
68     /**
69      * Read a {@link YangInstanceIdentifier} from the reader.
70      *
71      * @return A YangInstanceIdentifier
72      * @throws IOException if an error occurs
73      */
74     @NonNull YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
75
76     /**
77      * Read a {@link PathArgument} from the reader.
78      *
79      * @return A PathArgument
80      * @throws IOException if an error occurs
81      */
82     @NonNull PathArgument readPathArgument() throws IOException;
83
84     /**
85      * Read a {@link PathArgument} or a {@link LegacyPathArgument} from the reader.
86      *
87      * @return {@link Either} a {@link PathArgument} or a {@link LegacyPathArgument}
88      * @throws IOException if an error occurs
89      */
90     @Deprecated(since = "11.0.0")
91     default Either<PathArgument, LegacyPathArgument> readLegacyPathArgument() throws IOException {
92         return Either.ofFirst(readPathArgument());
93     }
94
95     @NonNull SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
96
97     /**
98      * Return the version of the underlying input stream.
99      *
100      * @return Stream version
101      * @throws IOException if the version cannot be ascertained
102      */
103     @NonNull NormalizedNodeStreamVersion getVersion() throws IOException;
104
105     default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
106         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
107     }
108
109     /**
110      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads
111      * and validates that the input contains a valid NormalizedNode stream.
112      *
113      * @param input the DataInput to read from
114      * @return a new {@link NormalizedNodeDataInput} instance
115      * @throws InvalidNormalizedNodeStreamException if the stream version is not supported
116      * @throws IOException if an error occurs reading from the input
117      * @throws NullPointerException if {@code input} is {@code null}
118      */
119     static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
120         return new VersionedNormalizedNodeDataInput(input).delegate();
121     }
122
123     /**
124      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not
125      * perform any initial validation of the input stream.
126      *
127      * @param input the DataInput to read from
128      * @return a new {@link NormalizedNodeDataInput} instance
129      * @deprecated Use {@link #newDataInput(DataInput)} instead.
130      */
131     @Deprecated(since = "5.0.0", forRemoval = true)
132     static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
133         return new VersionedNormalizedNodeDataInput(input);
134     }
135 }