9dda8bf6e47f5c1664b1060d7cc085196f6a64fa
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / 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.controller.cluster.datastore.node.utils.stream;
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.impl.schema.ImmutableNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23
24 /**
25  * Interface for reading {@link NormalizedNode}s, {@link YangInstanceIdentifier}s, {@link PathArgument}s
26  * and {@link SchemaPath}s.
27  */
28 @Beta
29 public interface NormalizedNodeDataInput extends DataInput {
30     /**
31      * Interpret current stream position as a NormalizedNode, stream its events into a NormalizedNodeStreamWriter.
32      *
33      * @param writer Writer to emit events to
34      * @throws IOException if an error occurs
35      * @throws IllegalStateException if the dictionary has been detached
36      * @throws NullPointerException if {@code writer} is null
37      */
38     void streamNormalizedNode(NormalizedNodeStreamWriter writer) throws IOException;
39
40     /**
41      * Read a normalized node from the reader.
42      *
43      * @return Next node from the stream, or null if end of stream has been reached.
44      * @throws IOException if an error occurs
45      * @throws IllegalStateException if the dictionary has been detached
46      */
47     default NormalizedNode<?, ?> readNormalizedNode() throws IOException {
48         final NormalizedNodeResult result = new NormalizedNodeResult();
49         try (NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(result)) {
50             streamNormalizedNode(writer);
51         }
52         return result.getResult();
53     }
54
55     YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
56
57     @NonNull QName readQName() throws IOException;
58
59     PathArgument readPathArgument() throws IOException;
60
61     SchemaPath readSchemaPath() throws IOException;
62
63     /**
64      * Return the version of the underlying input stream.
65      *
66      * @return Stream version
67      * @throws IOException if the version cannot be ascertained
68      */
69     NormalizedNodeStreamVersion getVersion() throws IOException;
70
71     default Optional<NormalizedNode<?, ?>> readOptionalNormalizedNode() throws IOException {
72         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
73     }
74 }