Remove @NotThreadSafe annotations
[yangtools.git] / yang / rfc7952-data-util / src / main / java / org / opendaylight / yangtools / rfc7952 / data / util / NormalizedMetadataWriter.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.rfc7952.data.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.io.Closeable;
15 import java.io.Flushable;
16 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadata;
19 import org.opendaylight.yangtools.rfc7952.data.api.NormalizedMetadataStreamWriter;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
24
25 /**
26  * A utility class to attach {@link NormalizedMetadata} into a NormalizedNode stream, such as the one produced by
27  * {@link NormalizedNodeWriter}, so that a target {@link NormalizedNodeStreamWriter} sees both data and metadata in
28  * the stream. A typical use would like this:
29  *
30  * <p>
31  * <code>
32  *   // Data for output
33  *   NormalizedNode&lt;?, ?&gt; data;
34  *   // Metadata for output
35  *   NormalizedMetadata metadata;
36  *
37  *   // Target output writer
38  *   NormalizedNodeStreamWriter output = ...;
39  *   // Metadata writer
40  *   NormalizedMetadataStreamWriter metaWriter = NormalizedMetadataWriter.forStreamWriter(output);
41  *
42  *   // Write a normalized node and its metadata
43  *   dataWriter.write(data, metadata);
44  * </code>
45  *
46  * <p>
47  * This class is NOT thread-safe.
48  *
49  * @author Robert Varga
50  */
51 @Beta
52 public final class NormalizedMetadataWriter implements Closeable, Flushable {
53     private final NormalizedNodeStreamWriter writer;
54     private final boolean orderKeyLeaves;
55
56     private NormalizedMetadataWriter(final NormalizedNodeStreamWriter writer, final boolean orderKeyLeaves) {
57         this.writer = requireNonNull(writer);
58         this.orderKeyLeaves = orderKeyLeaves;
59     }
60
61     /**
62      * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. Unlike the simple
63      * {@link #forStreamWriter(NormalizedNodeStreamWriter)} method, this allows the caller to switch off RFC6020 XML
64      * compliance, providing better throughput. The reason is that the XML mapping rules in RFC6020 require
65      * the encoding to emit leaf nodes which participate in a list's key first and in the order in which they are
66      * defined in the key. For JSON, this requirement is completely relaxed and leaves can be ordered in any way we
67      * see fit. The former requires a bit of work: first a lookup for each key and then for each emitted node we need
68      * to check whether it was already emitted.
69      *
70      * @param writer Back-end writer
71      * @param orderKeyLeaves whether the returned instance should be RFC6020 XML compliant.
72      * @return A new instance.
73      */
74     public static @NonNull NormalizedMetadataWriter forStreamWriter(final NormalizedNodeStreamWriter writer,
75             final boolean orderKeyLeaves) {
76         return new NormalizedMetadataWriter(writer, orderKeyLeaves);
77     }
78
79     /**
80      * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. This is a convenience method for
81      * {@code forStreamWriter(writer, true)}.
82      *
83      * @param writer Back-end writer
84      * @return A new instance.
85      */
86     public static @NonNull NormalizedMetadataWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
87         return forStreamWriter(writer, true);
88     }
89
90     /**
91      * Iterate over the provided {@link NormalizedNode} and {@link NormalizedMetadata} and emit write events to the
92      * encapsulated {@link NormalizedNodeStreamWriter}.
93      *
94      * @param data NormalizedNode data
95      * @param metadata {@link NormalizedMetadata} metadata
96      * @return NormalizedNodeWriter this
97      * @throws NullPointerException if any argument is null
98      * @throws IllegalArgumentException if metadata does not match data
99      * @throws IOException when thrown from the backing writer.
100      */
101     public @NonNull NormalizedMetadataWriter write(final NormalizedNode<?, ?> data, final NormalizedMetadata metadata)
102             throws IOException {
103         final PathArgument dataId = data.getIdentifier();
104         final PathArgument metaId = metadata.getIdentifier();
105         checkArgument(dataId.equals(metaId), "Mismatched data %s and metadata %s", dataId, metaId);
106
107         final NormalizedMetadataStreamWriter metaWriter = writer.getExtensions()
108                 .getInstance(NormalizedMetadataStreamWriter.class);
109         final NormalizedNodeStreamWriter delegate = metaWriter == null ? writer
110                 : new NormalizedNodeStreamWriterMetadataDecorator(writer, metaWriter, metadata);
111
112         final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(delegate, orderKeyLeaves);
113         nnWriter.write(data);
114         nnWriter.flush();
115         return this;
116     }
117
118     @Override
119     public void close() throws IOException {
120         writer.flush();
121         writer.close();
122     }
123
124     @Override
125     public void flush() throws IOException {
126         writer.flush();
127     }
128 }