Remove MountPointIdentifier
[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     default @NonNull PathArgument readPathArgument() throws IOException {
83         final var legacy = readLegacyPathArgument();
84         if (legacy.isFirst()) {
85             return legacy.getFirst();
86         }
87         throw new IOException(legacy.getSecond() + " does not have a representation");
88     }
89
90     /**
91      * Read a {@link PathArgument} or a {@link LegacyPathArgument} from the reader.
92      *
93      * @return {@link Either} a {@link PathArgument} or a {@link LegacyPathArgument}
94      * @throws IOException if an error occurs
95      */
96     @Deprecated(since = "11.0.0")
97     @NonNull Either<PathArgument, LegacyPathArgument> readLegacyPathArgument() throws IOException;
98
99     @NonNull SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
100
101     /**
102      * Return the version of the underlying input stream.
103      *
104      * @return Stream version
105      * @throws IOException if the version cannot be ascertained
106      */
107     @NonNull NormalizedNodeStreamVersion getVersion() throws IOException;
108
109     default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
110         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
111     }
112
113     /**
114      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads
115      * and validates that the input contains a valid NormalizedNode stream.
116      *
117      * @param input the DataInput to read from
118      * @return a new {@link NormalizedNodeDataInput} instance
119      * @throws InvalidNormalizedNodeStreamException if the stream version is not supported
120      * @throws IOException if an error occurs reading from the input
121      * @throws NullPointerException if {@code input} is {@code null}
122      */
123     static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
124         return new VersionedNormalizedNodeDataInput(input).delegate();
125     }
126
127     /**
128      * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not
129      * perform any initial validation of the input stream.
130      *
131      * @param input the DataInput to read from
132      * @return a new {@link NormalizedNodeDataInput} instance
133      * @deprecated Use {@link #newDataInput(DataInput)} instead.
134      */
135     @Deprecated(since = "5.0.0", forRemoval = true)
136     static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
137         return new VersionedNormalizedNodeDataInput(input);
138     }
139 }