Add support for reusable streaming
[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.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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         final NormalizedNodeResult result = new NormalizedNodeResult();
50         try (NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(result)) {
51             streamNormalizedNode(writer);
52         }
53         return result.getResult();
54     }
55
56     /**
57      * Read a normalized node from the reader, using specified writer to construct the result.
58      *
59      * @param writer Reusable writer to
60      * @return Next node from the stream, or null if end of stream has been reached.
61      * @throws IOException if an error occurs
62      * @throws IllegalStateException if the dictionary has been detached
63      */
64     default NormalizedNode<?, ?> readNormalizedNode(final ReusableImmutableNormalizedNodeStreamWriter writer)
65             throws IOException {
66         try {
67             streamNormalizedNode(writer);
68             return writer.getResult();
69         } finally {
70             writer.reset();
71         }
72     }
73
74     YangInstanceIdentifier readYangInstanceIdentifier() throws IOException;
75
76     @NonNull QName readQName() throws IOException;
77
78     PathArgument readPathArgument() throws IOException;
79
80     SchemaPath readSchemaPath() throws IOException;
81
82     /**
83      * Return the version of the underlying input stream.
84      *
85      * @return Stream version
86      * @throws IOException if the version cannot be ascertained
87      */
88     NormalizedNodeStreamVersion getVersion() throws IOException;
89
90     default Optional<NormalizedNode<?, ?>> readOptionalNormalizedNode() throws IOException {
91         return readBoolean() ? Optional.of(readNormalizedNode()) : Optional.empty();
92     }
93 }