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 @NonNull PathArgument readPathArgument() throws IOException;
85 * Read a {@link PathArgument} or a {@link LegacyPathArgument} from the reader.
87 * @return {@link Either} a {@link PathArgument} or a {@link LegacyPathArgument}
88 * @throws IOException if an error occurs
90 @Deprecated(since = "11.0.0")
91 default Either<PathArgument, LegacyPathArgument> readLegacyPathArgument() throws IOException {
92 return Either.ofFirst(readPathArgument());
95 @NonNull SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
98 * Return the version of the underlying input stream.
100 * @return Stream version
101 * @throws IOException if the version cannot be ascertained
103 @NonNull NormalizedNodeStreamVersion getVersion() throws IOException;
105 default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
106 return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
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.
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}
119 static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
120 return new VersionedNormalizedNodeDataInput(input).delegate();
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.
127 * @param input the DataInput to read from
128 * @return a new {@link NormalizedNodeDataInput} instance
129 * @deprecated Use {@link #newDataInput(DataInput)} instead.
131 @Deprecated(since = "5.0.0", forRemoval = true)
132 static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
133 return new VersionedNormalizedNodeDataInput(input);