Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / NormalizedFormattableBody.java
1 /*
2  * Copyright (c) 2024 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.restconf.server.spi;
9
10 import static java.util.Objects.requireNonNull;
11
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;
35
36 /**
37  * A {@link FormattableBody} representing a data resource.
38  */
39 @NonNullByDefault
40 public abstract sealed class NormalizedFormattableBody<N extends NormalizedNode> extends DatabindFormattableBody
41         permits DataFormattableBody, RootFormattableBody {
42     private final NormalizedNodeWriterFactory writerFactory;
43     private final N data;
44
45     NormalizedFormattableBody(final DatabindContext databind, final NormalizedNodeWriterFactory writerFactory,
46             final N data) {
47         super(databind);
48         this.writerFactory = requireNonNull(writerFactory);
49         this.data = requireNonNull(data);
50     }
51
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);
59             }
60             throw new VerifyException("Unexpected root data contract " + data.contract());
61         }
62
63         // Read of a sub-resource. We need to adjust the inference to point to the NormalizedNode parent of the node
64         // being output.
65         final Inference parentInference;
66         if (data instanceof MapEntryNode || data instanceof LeafSetEntryNode || data instanceof UnkeyedListEntryNode) {
67             parentInference = inference;
68         } else {
69             final var stack = inference.toSchemaInferenceStack();
70             stack.exitToDataTree();
71             parentInference = stack.toInference();
72         }
73
74         return new DataFormattableBody<>(path.databind(), parentInference, data, writerFactory);
75     }
76
77     /**
78      * Return data.
79      *
80      * @return data
81      */
82     public final N data() {
83         return data;
84     }
85
86     @Override
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);
91         }
92     }
93
94     protected abstract void formatToJSON(JSONCodecFactory codecs, N data, JsonWriter writer) throws IOException;
95
96     @Override
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);
100         try {
101             formatToXML(databind.xmlCodecs(), data, writer);
102             writer.close();
103         } catch (XMLStreamException e) {
104             throw new IOException("Failed to write data", e);
105         }
106     }
107
108     protected abstract void formatToXML(XmlCodecFactory codecs, N data, XMLStreamWriter writer)
109         throws IOException, XMLStreamException;
110
111     protected final RestconfNormalizedNodeWriter newWriter(final NormalizedNodeStreamWriter streamWriter) {
112         return writerFactory.newWriter(streamWriter);
113     }
114
115     final void writeTo(final NormalizedNode toWrite, final NormalizedNodeStreamWriter streamWriter)
116             throws IOException {
117         try (var writer = newWriter(streamWriter)) {
118             writer.write(toWrite);
119         }
120     }
121
122     @Override
123     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
124         return helper.add("body", data.prettyTree());
125     }
126
127
128 }