Revert "Move SchemaNodeIdentifier to yang-common"
[yangtools.git] / codec / 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.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;
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 QNameAwareDataInput {
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     PathArgument readPathArgument() throws IOException;
72
73     @Deprecated(forRemoval = true)
74     SchemaPath readSchemaPath() throws IOException;
75
76     SchemaNodeIdentifier readSchemaNodeIdentifier() throws IOException;
77
78     /**
79      * Return the version of the underlying input stream.
80      *
81      * @return Stream version
82      * @throws IOException if the version cannot be ascertained
83      */
84     NormalizedNodeStreamVersion getVersion() throws IOException;
85
86     default Optional<NormalizedNode> readOptionalNormalizedNode() throws IOException {
87         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
88     }
89
90     /**
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.
93      *
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
98      */
99     static @NonNull NormalizedNodeDataInput newDataInput(final @NonNull DataInput input) throws IOException {
100         return new VersionedNormalizedNodeDataInput(input).delegate();
101     }
102
103     /**
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.
106      *
107      * @param input the DataInput to read from
108      * @return a new {@link NormalizedNodeDataInput} instance
109      * @deprecated Use {@link #newDataInput(DataInput)} instead.
110      */
111     @Deprecated(forRemoval = true)
112     static @NonNull NormalizedNodeDataInput newDataInputWithoutValidation(final @NonNull DataInput input) {
113         return new VersionedNormalizedNodeDataInput(input);
114     }
115 }