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