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 com.google.common.annotations.Beta;
11 import java.io.DataInput;
12 import java.io.IOException;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName.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.SchemaPath;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
26 * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
27 * and {@link SchemaPath}s.
30 public interface NormalizedNodeDataInput extends QNameAwareDataInput {
32 * Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
34 * @param writer Writer to emit events to
35 * @throws IOException if an error occurs
36 * @throws IllegalStateException if the dictionary has been detached
37 * @throws NullPointerException if {@code writer} is null
39 void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
42 * Read a normalized node from the reader.
44 * @return Next node from the stream, or null if end of stream has been reached.
45 * @throws IOException if an error occurs
46 * @throws IllegalStateException if the dictionary has been detached
48 default NormalizedNode readNormalizedNode() throws IOException {
49 return readNormalizedNode(ReusableImmutableNormalizedNodeStreamWriter.create());
53 * Read a normalized node from the reader, using specified writer to construct the result.
55 * @param receiver Reusable receiver to, expected to be reset
56 * @return Next node from the stream, or null if end of stream has been reached.
57 * @throws IOException if an error occurs
58 * @throws IllegalStateException if the dictionary has been detached
60 default NormalizedNode readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
62 streamNormalizedNode(receiver);
63 return receiver.getResult();
69 YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
71 PathArgument readPathArgument() throws IOException;
73 @Deprecated(forRemoval = true)
74 SchemaPath readSchemaPath() throws IOException;
76 SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
79 * Return the version of the underlying input stream.
81 * @return Stream version
82 * @throws IOException if the version cannot be ascertained
84 NormalizedNodeStreamVersion getVersion() throws IOException;
86 default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
87 return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
91 * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads
92 * and validates that the input contains a valid NormalizedNode stream.
94 * @param input the DataInput to read from
95 * @return a new {@link NormalizedNodeDataInput} instance
96 * @throws InvalidNormalizedNodeStreamException if the stream version is not supported
97 * @throws IOException if an error occurs reading from the input
99 static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
100 return new VersionedNormalizedNodeDataInput(input).delegate();
104 * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not
105 * perform any initial validation of the input stream.
107 * @param input the DataInput to read from
108 * @return a new {@link NormalizedNodeDataInput} instance
109 * @deprecated Use {@link #newDataInput(DataInput)} instead.
111 @Deprecated(forRemoval = true)
112 static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
113 return new VersionedNormalizedNodeDataInput(input);