Rework NormalizedNode type hierarchy
[yangtools.git] / yang / 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 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;
24
25 /**
26  * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
27  * and {@link SchemaPath}s.
28  */
29 @Beta
30 public interface NormalizedNodeDataInput extends DataInput {
31     /**
32      * Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
33      *
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
38      */
39     void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
40
41     /**
42      * Read a normalized node from the reader.
43      *
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
47      */
48     default NormalizedNode readNormalizedNode() throws IOException {
49         return readNormalizedNode(ReusableImmutableNormalizedNodeStreamWriter.create());
50     }
51
52     /**
53      * Read a normalized node from the reader, using specified writer to construct the result.
54      *
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
59      */
60     default NormalizedNode readNormalizedNode(final ReusableStreamReceiver receiver) throws IOException {
61         try {
62             streamNormalizedNode(receiver);
63             return receiver.getResult();
64         } finally {
65             receiver.reset();
66         }
67     }
68
69     YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
70
71     @NonNull QName readQName() throws IOException;
72
73     PathArgument readPathArgument() throws IOException;
74
75     @Deprecated(forRemoval = true)
76     SchemaPath readSchemaPath() throws IOException;
77
78     SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
79
80     /**
81      * Return the version of the underlying input stream.
82      *
83      * @return Stream version
84      * @throws IOException if the version cannot be ascertained
85      */
86     NormalizedNodeStreamVersion getVersion() throws IOException;
87
88     default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
89         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
90     }
91
92     /**
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.
95      *
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
100      */
101     static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
102         return new VersionedNormalizedNodeDataInput(input).delegate();
103     }
104
105     /**
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.
108      *
109      * @param input the DataInput to read from
110      * @return a new {@link NormalizedNodeDataInput} instance
111      * @deprecated Use {@link #newDataInput(DataInput)} instead.
112      */
113     @Deprecated(forRemoval = true)
114     static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
115         return new VersionedNormalizedNodeDataInput(input);
116     }
117 }