2 * Copyright (c) 2014, 2015 Cisco 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.yangtools.yang.data.codec.binfmt;
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;
25 * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
26 * and {@link SchemaNodeIdentifier}s.
28 public interface NormalizedNodeDataInput extends QNameAwareDataInput {
30 * Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
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}
37 void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
40 * Read a normalized node from the reader.
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
46 default NormalizedNode readNormalizedNode() throws IOException {
47 return readNormalizedNode(ReusableImmutableNormalizedNodeStreamWriter.create());
51 * Read a normalized node from the reader, using specified writer to construct the result.
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}
59 default NormalizedNode readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
61 streamNormalizedNode(receiver);
62 return receiver.getResult().data();
69 * Read a {@link YangInstanceIdentifier} from the reader.
71 * @return A YangInstanceIdentifier
72 * @throws IOException if an error occurs
74 @NonNull YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
77 * Read a {@link PathArgument} from the reader.
79 * @return A PathArgument
80 * @throws IOException if an error occurs
82 default @NonNull PathArgument readPathArgument() throws IOException {
83 final var legacy = readLegacyPathArgument();
84 if (legacy.isFirst()) {
85 return legacy.getFirst();
87 throw new IOException(legacy.getSecond() + " does not have a representation");
91 * Read a {@link PathArgument} or a {@link LegacyPathArgument} from the reader.
93 * @return {@link Either} a {@link PathArgument} or a {@link LegacyPathArgument}
94 * @throws IOException if an error occurs
96 @Deprecated(since = "11.0.0")
97 @NonNull Either<PathArgument, LegacyPathArgument> readLegacyPathArgument() throws IOException;
99 @NonNull SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
102 * Return the version of the underlying input stream.
104 * @return Stream version
105 * @throws IOException if the version cannot be ascertained
107 @NonNull NormalizedNodeStreamVersion getVersion() throws IOException;
109 default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
110 return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
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.
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}
123 static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
124 return new VersionedNormalizedNodeDataInput(input).delegate();
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.
131 * @param input the DataInput to read from
132 * @return a new {@link NormalizedNodeDataInput} instance
133 * @deprecated Use {@link #newDataInput(DataInput)} instead.
135 @Deprecated(since = "5.0.0", forRemoval = true)
136 static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
137 return new VersionedNormalizedNodeDataInput(input);