3182ba5c9bb8cd643167757a951d7666bdc88fdc
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / XmlNormalizedNodeBodyWriter.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.nb.rfc8040.jersey.providers;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import java.nio.charset.StandardCharsets;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import javanet.staxutils.IndentingXMLStreamWriter;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.Provider;
24 import javax.xml.XMLConstants;
25 import javax.xml.stream.FactoryConfigurationError;
26 import javax.xml.stream.XMLOutputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamWriter;
29 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
30 import org.opendaylight.restconf.nb.rfc8040.DepthParam;
31 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
32 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter;
33 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
40 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
41 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
42 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
43 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
44 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
45
46 @Provider
47 @Produces({ MediaTypes.APPLICATION_YANG_DATA_XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
48 public class XmlNormalizedNodeBodyWriter extends AbstractNormalizedNodeBodyWriter {
49     private static final XMLOutputFactory XML_FACTORY;
50
51     static {
52         XML_FACTORY = XMLOutputFactory.newFactory();
53         XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
54     }
55
56     @Override
57     public void writeTo(final NormalizedNodePayload context,
58                         final Class<?> type,
59                         final Type genericType,
60                         final Annotation[] annotations,
61                         final MediaType mediaType,
62                         final MultivaluedMap<String, Object> httpHeaders,
63                         final OutputStream entityStream) throws IOException, WebApplicationException {
64         final InstanceIdentifierContext pathContext = context.getInstanceIdentifierContext();
65         if (context.getData() == null) {
66             return;
67         }
68         if (httpHeaders != null) {
69             for (final Map.Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
70                 httpHeaders.add(entry.getKey(), entry.getValue());
71             }
72         }
73
74         XMLStreamWriter xmlWriter;
75         try {
76             xmlWriter = XML_FACTORY.createXMLStreamWriter(entityStream, StandardCharsets.UTF_8.name());
77
78             final var prettyPrint = context.getWriterParameters().prettyPrint();
79             if (prettyPrint != null && prettyPrint.value()) {
80                 xmlWriter = new IndentingXMLStreamWriter(xmlWriter);
81             }
82         } catch (final XMLStreamException | FactoryConfigurationError e) {
83             throw new IllegalStateException(e);
84         }
85         final NormalizedNode data = context.getData();
86
87         writeNormalizedNode(xmlWriter,  pathContext, data, context.getWriterParameters().depth(),
88                 context.getWriterParameters().fields());
89     }
90
91     private static void writeNormalizedNode(final XMLStreamWriter xmlWriter,
92             final InstanceIdentifierContext pathContext, final NormalizedNode data, final DepthParam depth,
93             final List<Set<QName>> fields) throws IOException {
94         final RestconfNormalizedNodeWriter nnWriter;
95         final SchemaNode schemaNode = pathContext.getSchemaNode();
96
97         if (schemaNode instanceof RpcDefinition) {
98             // RpcDefinition is not supported as initial codec in XMLStreamWriter, so we need to emit initial output
99             // declaration.
100             final var rpc = (RpcDefinition) schemaNode;
101             final var stack = SchemaInferenceStack.of(pathContext.getSchemaContext());
102             stack.enterSchemaTree(rpc.getQName());
103             stack.enterSchemaTree(rpc.getOutput().getQName());
104
105             nnWriter = createNormalizedNodeWriter(xmlWriter, stack.toInference(), depth, fields);
106             writeElements(xmlWriter, nnWriter, (ContainerNode) data);
107         } else if (schemaNode instanceof ActionDefinition) {
108             // ActionDefinition is not supported as initial codec in XMLStreamWriter, so we need to emit initial output
109             // declaration.
110             final var action = (ActionDefinition) schemaNode;
111             final var stack = pathContext.inference().toSchemaInferenceStack();
112             stack.enterSchemaTree(action.getOutput().getQName());
113
114             nnWriter = createNormalizedNodeWriter(xmlWriter, stack.toInference(), depth, fields);
115             writeElements(xmlWriter, nnWriter, (ContainerNode) data);
116         } else {
117             final var stack = pathContext.inference().toSchemaInferenceStack();
118             final boolean isRoot;
119             if (!stack.isEmpty()) {
120                 stack.exit();
121                 isRoot = false;
122             } else {
123                 isRoot = true;
124             }
125             nnWriter = createNormalizedNodeWriter(xmlWriter, stack.toInference(), depth, fields);
126
127             if (data instanceof MapEntryNode) {
128                 // Restconf allows returning one list item. We need to wrap it
129                 // in map node in order to serialize it properly
130                 nnWriter.write(ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
131                     .addChild((MapEntryNode) data)
132                     .build());
133             } else if (isRoot) {
134                 if (data instanceof ContainerNode && ((ContainerNode) data).isEmpty()) {
135                     writeEmptyDataNode(xmlWriter, data);
136                 } else {
137                     writeAndWrapInDataNode(xmlWriter, nnWriter, data);
138                 }
139             } else {
140                 nnWriter.write(data);
141             }
142         }
143
144         nnWriter.flush();
145     }
146
147     private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
148             final Inference inference, final DepthParam depth,
149             final List<Set<QName>> fields) {
150         return ParameterAwareNormalizedNodeWriter.forStreamWriter(
151             XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, inference), depth, fields);
152     }
153
154     private static void writeAndWrapInDataNode(final XMLStreamWriter xmlWriter,
155             final RestconfNormalizedNodeWriter nnWriter, final NormalizedNode data) throws IOException {
156         final QName nodeType = data.getIdentifier().getNodeType();
157         final String namespace = nodeType.getNamespace().toString();
158         try {
159             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
160             xmlWriter.writeDefaultNamespace(namespace);
161             nnWriter.write(data);
162             xmlWriter.writeEndElement();
163             xmlWriter.flush();
164         } catch (XMLStreamException e) {
165             throw new IOException("Failed to write elements", e);
166         }
167     }
168
169     private static void writeEmptyDataNode(final XMLStreamWriter xmlWriter, final NormalizedNode data)
170             throws IOException {
171         final QName nodeType = data.getIdentifier().getNodeType();
172         final String namespace = nodeType.getNamespace().toString();
173         try {
174             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
175             xmlWriter.writeDefaultNamespace(namespace);
176             xmlWriter.writeEndElement();
177             xmlWriter.flush();
178         } catch (XMLStreamException e) {
179             throw new IOException("Failed to write elements", e);
180         }
181     }
182
183     private static void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
184             final ContainerNode data) throws IOException {
185         final QName nodeType = data.getIdentifier().getNodeType();
186         final String namespace = nodeType.getNamespace().toString();
187         try {
188             xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, nodeType.getLocalName(), namespace);
189             xmlWriter.writeDefaultNamespace(namespace);
190             for (final NormalizedNode child : data.body()) {
191                 nnWriter.write(child);
192             }
193             nnWriter.flush();
194             xmlWriter.writeEndElement();
195             xmlWriter.flush();
196         } catch (final XMLStreamException e) {
197             throw new IOException("Failed to write elements", e);
198         }
199     }
200 }