*/
package org.opendaylight.netconf.sal.rest.impl;
-import com.google.common.base.Optional;
-import com.google.common.base.Throwables;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
+import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map.Entry;
import javanet.staxutils.IndentingXMLStreamWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.netconf.sal.rest.api.Draft02;
import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
import org.opendaylight.netconf.sal.rest.api.RestconfService;
+import org.opendaylight.netconf.util.NetconfUtil;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.xml.sax.SAXException;
/**
* Normalized node writer for XML.
*/
@Deprecated
@Provider
-@Produces({ Draft02.MediaTypes.API + RestconfService.XML, Draft02.MediaTypes.DATA + RestconfService.XML,
- Draft02.MediaTypes.OPERATION + RestconfService.XML, MediaType.APPLICATION_XML, MediaType.TEXT_XML })
+@Produces({
+ Draft02.MediaTypes.API + RestconfService.XML,
+ Draft02.MediaTypes.DATA + RestconfService.XML,
+ Draft02.MediaTypes.OPERATION + RestconfService.XML,
+ MediaType.APPLICATION_XML,
+ MediaType.TEXT_XML
+})
public class NormalizedNodeXmlBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
private static final XMLOutputFactory XML_FACTORY;
} catch (final XMLStreamException | FactoryConfigurationError e) {
throw new IllegalStateException(e);
}
- final NormalizedNode<?, ?> data = context.getData();
+ final NormalizedNode data = context.getData();
final SchemaPath schemaPath = pathContext.getSchemaNode().getPath();
- writeNormalizedNode(xmlWriter, schemaPath, pathContext, data,
- Optional.fromNullable(context.getWriterParameters().getDepth()));
+ writeNormalizedNode(xmlWriter, schemaPath, pathContext, data, context.getWriterParameters().getDepth());
}
private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final SchemaPath schemaPath,
- final InstanceIdentifierContext<?> pathContext, NormalizedNode<?, ?> data, final Optional<Integer> depth)
+ final InstanceIdentifierContext<?> pathContext, NormalizedNode data, final @Nullable Integer depth)
throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
- final SchemaContext schemaCtx = pathContext.getSchemaContext();
+ final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
if (SchemaPath.ROOT.equals(schemaPath)) {
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, schemaPath, depth);
- writeElements(xmlWriter, nnWriter, (ContainerNode) data);
+ if (data instanceof DOMSourceAnyxmlNode) {
+ try {
+ writeElements(xmlWriter, nnWriter,
+ (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(schemaCtx,
+ ((DOMSourceAnyxmlNode)data).body()).getResult());
+ } catch (XMLStreamException | URISyntaxException | SAXException e) {
+ throw new IOException("Cannot write anyxml", e);
+ }
+ } else {
+ writeElements(xmlWriter, nnWriter, (ContainerNode) data);
+ }
} else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx,
((RpcDefinition) pathContext.getSchemaNode()).getOutput().getPath(), depth);
if (data instanceof MapEntryNode) {
// Restconf allows returning one list item. We need to wrap it
// in map node in order to serialize it properly
- data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).addChild((MapEntryNode) data).build();
+ data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
+ .addChild((MapEntryNode) data)
+ .build();
}
nnWriter.write(data);
}
}
private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(final XMLStreamWriter xmlWriter,
- final SchemaContext schemaContext, final SchemaPath schemaPath, final Optional<Integer> depth) {
+ final EffectiveModelContext schemaContext, final SchemaPath schemaPath, final @Nullable Integer depth) {
final NormalizedNodeStreamWriter xmlStreamWriter =
XMLStreamNormalizedNodeStreamWriter.create(xmlWriter, schemaContext, schemaPath);
- if (depth.isPresent()) {
- return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth.get());
+ if (depth != null) {
+ return DepthAwareNormalizedNodeWriter.forStreamWriter(xmlStreamWriter, depth);
}
return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(xmlStreamWriter);
}
private static void writeElements(final XMLStreamWriter xmlWriter, final RestconfNormalizedNodeWriter nnWriter,
- final ContainerNode data)
- throws IOException {
+ final ContainerNode data) throws IOException {
+ final QName name = data.getIdentifier().getNodeType();
try {
- final QName name = data.getNodeType();
xmlWriter.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, name.getLocalName(),
name.getNamespace().toString());
xmlWriter.writeDefaultNamespace(name.getNamespace().toString());
- for (final NormalizedNode<?,?> child : data.getValue()) {
+ for (final NormalizedNode child : data.body()) {
nnWriter.write(child);
}
nnWriter.flush();
xmlWriter.writeEndElement();
xmlWriter.flush();
} catch (final XMLStreamException e) {
- Throwables.propagate(e);
+ throw new IOException("Failed to write elements", e);
}
}
}