2 * Copyright (c) 2024 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.restconf.server.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.VerifyException;
14 import com.google.gson.stream.JsonWriter;
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import javax.xml.stream.XMLStreamException;
18 import javax.xml.stream.XMLStreamWriter;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.opendaylight.restconf.api.FormattableBody;
21 import org.opendaylight.restconf.api.query.PrettyPrintParam;
22 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.RestconfNormalizedNodeWriter;
23 import org.opendaylight.restconf.server.api.DatabindContext;
24 import org.opendaylight.restconf.server.api.DatabindFormattableBody;
25 import org.opendaylight.restconf.server.api.DatabindPath.Data;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
33 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
34 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
37 * A {@link FormattableBody} representing a data resource.
40 public abstract sealed class NormalizedFormattableBody<N extends NormalizedNode> extends DatabindFormattableBody
41 permits DataFormattableBody, RootFormattableBody {
42 private final NormalizedNodeWriterFactory writerFactory;
45 NormalizedFormattableBody(final DatabindContext databind, final NormalizedNodeWriterFactory writerFactory,
48 this.writerFactory = requireNonNull(writerFactory);
49 this.data = requireNonNull(data);
52 public static NormalizedFormattableBody<?> of(final Data path, final NormalizedNode data,
53 final NormalizedNodeWriterFactory writerFactory) {
54 final var inference = path.inference();
55 if (inference.isEmpty()) {
56 // Read of the entire /data resource
57 if (data instanceof ContainerNode container) {
58 return new RootFormattableBody(path.databind(), writerFactory, container);
60 throw new VerifyException("Unexpected root data contract " + data.contract());
63 // Read of a sub-resource. We need to adjust the inference to point to the NormalizedNode parent of the node
65 final Inference parentInference;
66 if (data instanceof MapEntryNode || data instanceof LeafSetEntryNode || data instanceof UnkeyedListEntryNode) {
67 parentInference = inference;
69 final var stack = inference.toSchemaInferenceStack();
70 stack.exitToDataTree();
71 parentInference = stack.toInference();
74 return new DataFormattableBody<>(path.databind(), parentInference, data, writerFactory);
82 public final N data() {
87 protected final void formatToJSON(final DatabindContext databind, final PrettyPrintParam prettyPrint,
88 final OutputStream out) throws IOException {
89 try (var writer = FormattableBodySupport.createJsonWriter(out, prettyPrint)) {
90 formatToJSON(databind.jsonCodecs(), data, writer);
94 protected abstract void formatToJSON(JSONCodecFactory codecs, N data, JsonWriter writer) throws IOException;
97 protected final void formatToXML(final DatabindContext databind, final PrettyPrintParam prettyPrint,
98 final OutputStream out) throws IOException {
99 final var writer = FormattableBodySupport.createXmlWriter(out, prettyPrint);
101 formatToXML(databind.xmlCodecs(), data, writer);
103 } catch (XMLStreamException e) {
104 throw new IOException("Failed to write data", e);
108 protected abstract void formatToXML(XmlCodecFactory codecs, N data, XMLStreamWriter writer)
109 throws IOException, XMLStreamException;
111 protected final RestconfNormalizedNodeWriter newWriter(final NormalizedNodeStreamWriter streamWriter) {
112 return writerFactory.newWriter(streamWriter);
115 final void writeTo(final NormalizedNode toWrite, final NormalizedNodeStreamWriter streamWriter)
117 try (var writer = newWriter(streamWriter)) {
118 writer.write(toWrite);
123 protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
124 return helper.add("body", data.prettyTree());