2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.rfc7952.data.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
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:
34 * NormalizedNode<?, ?> data;
35 * // Metadata for output
36 * NormalizedMetadata metadata;
38 * // Target output writer
39 * NormalizedNodeStreamWriter output = ...;
41 * NormalizedMetadataStreamWriter metaWriter = NormalizedMetadataWriter.forStreamWriter(output);
43 * // Write a normalized node and its metadata
44 * dataWriter.write(data, metadata);
47 * @author Robert Varga
51 public final class NormalizedMetadataWriter implements Closeable, Flushable {
52 private final NormalizedNodeStreamWriter writer;
53 private final boolean orderKeyLeaves;
55 private NormalizedMetadataWriter(final NormalizedNodeStreamWriter writer, final boolean orderKeyLeaves) {
56 this.writer = requireNonNull(writer);
57 this.orderKeyLeaves = orderKeyLeaves;
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.
69 * @param writer Back-end writer
70 * @param orderKeyLeaves whether the returned instance should be RFC6020 XML compliant.
71 * @return A new instance.
73 public static @NonNull NormalizedMetadataWriter forStreamWriter(final NormalizedNodeStreamWriter writer,
74 final boolean orderKeyLeaves) {
75 return new NormalizedMetadataWriter(writer, orderKeyLeaves);
79 * Create a new writer backed by a {@link NormalizedNodeStreamWriter}. This is a convenience method for
80 * {@code forStreamWriter(writer, true)}.
82 * @param writer Back-end writer
83 * @return A new instance.
85 public static @NonNull NormalizedMetadataWriter forStreamWriter(final NormalizedNodeStreamWriter writer) {
86 return forStreamWriter(writer, true);
90 * Iterate over the provided {@link NormalizedNode} and {@link NormalizedMetadata} and emit write events to the
91 * encapsulated {@link NormalizedNodeStreamWriter}.
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.
100 public @NonNull NormalizedMetadataWriter write(final NormalizedNode<?, ?> data, final NormalizedMetadata metadata)
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);
106 final NormalizedMetadataStreamWriter metaWriter = writer.getExtensions()
107 .getInstance(NormalizedMetadataStreamWriter.class);
108 final NormalizedNodeStreamWriter delegate = metaWriter == null ? writer
109 : new NormalizedNodeStreamWriterMetadataDecorator(writer, metaWriter, metadata);
111 final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(delegate, orderKeyLeaves);
112 nnWriter.write(data);
118 public void close() throws IOException {
124 public void flush() throws IOException {