Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / RootFormattableBody.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 com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import javax.xml.XMLConstants;
13 import javax.xml.stream.XMLStreamException;
14 import javax.xml.stream.XMLStreamWriter;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.opendaylight.restconf.server.api.DatabindContext;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.codec.xml.XmlCodecFactory;
23
24 /**
25  * A {@link NormalizedFormattableBody} for a data root.
26  */
27 @NonNullByDefault
28 final class RootFormattableBody extends NormalizedFormattableBody<ContainerNode> {
29     RootFormattableBody(final DatabindContext databind, final NormalizedNodeWriterFactory writerFactory,
30             final ContainerNode data) {
31         super(databind, writerFactory, data);
32     }
33
34     @Override
35     protected void formatToJSON(final JSONCodecFactory codecs, final ContainerNode data, final JsonWriter writer)
36             throws IOException {
37         writer.beginObject();
38
39         final var nnWriter = newWriter(JSONNormalizedNodeStreamWriter.createNestedWriter(codecs, writer, null));
40         // FIXME: we should be remapping namespace here
41         nnWriter.write(data);
42         nnWriter.flush();
43
44         writer.endObject();
45     }
46
47     @Override
48     protected void formatToXML(final XmlCodecFactory codecs, final ContainerNode data, final XMLStreamWriter writer)
49             throws IOException, XMLStreamException {
50         // FIXME: we should be remapping namespace here
51         final QName nodeType = data.name().getNodeType();
52         final String namespace = nodeType.getNamespace().toString();
53
54         writer.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
55         writer.writeDefaultNamespace(namespace);
56
57         if (!data.isEmpty()) {
58             final var nnWriter = newWriter(
59                 XMLStreamNormalizedNodeStreamWriter.create(writer, codecs.modelContext()));
60             nnWriter.write(data);
61             nnWriter.flush();
62         }
63
64         writer.writeEndElement();
65     }
66 }