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;
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 DataInput {
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 @NonNull QName readQName() throws IOException;
73 PathArgument readPathArgument() throws IOException;
75 @Deprecated(forRemoval = true)
76 SchemaPath readSchemaPath() throws IOException;
78 SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
81 * Return the version of the underlying input stream.
83 * @return Stream version
84 * @throws IOException if the version cannot be ascertained
86 NormalizedNodeStreamVersion getVersion() throws IOException;
88 default Optional<NormalizedNode<?, ?>> readOptionalNormalizedNode() throws IOException {
89 return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
93 * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method first reads
94 * and validates that the input contains a valid NormalizedNode stream.
96 * @param input the DataInput to read from
97 * @return a new {@link NormalizedNodeDataInput} instance
98 * @throws InvalidNormalizedNodeStreamException if the stream version is not supported
99 * @throws IOException if an error occurs reading from the input
101 static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
102 return new VersionedNormalizedNodeDataInput(input).delegate();
106 * Creates a new {@link NormalizedNodeDataInput} instance that reads from the given input. This method does not
107 * perform any initial validation of the input stream.
109 * @param input the DataInput to read from
110 * @return a new {@link NormalizedNodeDataInput} instance
111 * @deprecated Use {@link #newDataInput(DataInput)} instead.
113 @Deprecated(forRemoval = true)
114 static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
115 return new VersionedNormalizedNodeDataInput(input);