Search in sources :

Example 1 with RestconfNormalizedNodeWriter

use of org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter in project netconf by opendaylight.

the class NormalizedNodeXmlBodyWriter method writeNormalizedNode.

private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final InstanceIdentifierContext<?> pathContext, NormalizedNode data, @Nullable final Integer depth) throws IOException {
    final RestconfNormalizedNodeWriter nnWriter;
    final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
    if (pathContext.getSchemaNode() instanceof SchemaContext) {
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth);
        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) {
        final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
        final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
        writeElements(xmlWriter, nnWriter, (ContainerNode) data);
    } else {
        final List<QName> qNames = pathContext.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
        final SchemaPath path = SchemaPath.of(Absolute.of(qNames)).getParent();
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, 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.getIdentifier().getNodeType()).addChild((MapEntryNode) data).build();
        }
        nnWriter.write(data);
    }
    nnWriter.flush();
}
Also used : SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) Produces(javax.ws.rs.Produces) Provider(javax.ws.rs.ext.Provider) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) URISyntaxException(java.net.URISyntaxException) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) Draft02(org.opendaylight.netconf.sal.rest.api.Draft02) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) NetconfUtil(org.opendaylight.netconf.util.NetconfUtil) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) Nullable(org.eclipse.jdt.annotation.Nullable) XMLStreamException(javax.xml.stream.XMLStreamException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLConstants(javax.xml.XMLConstants) RestconfService(org.opendaylight.netconf.sal.rest.api.RestconfService) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) OutputStream(java.io.OutputStream) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) IOException(java.io.IOException) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) QName(org.opendaylight.yangtools.yang.common.QName) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) List(java.util.List) IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Type(java.lang.reflect.Type) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) SAXException(org.xml.sax.SAXException) Annotation(java.lang.annotation.Annotation) Entry(java.util.Map.Entry) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) List(java.util.List) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 2 with RestconfNormalizedNodeWriter

use of org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter in project netconf by opendaylight.

the class NormalizedNodeJsonBodyWriter method writeNormalizedNode.

private static void writeNormalizedNode(final JsonWriter jsonWriter, final InstanceIdentifierContext<SchemaNode> context, NormalizedNode data, @Nullable final Integer depth) throws IOException {
    final RestconfNormalizedNodeWriter nnWriter;
    if (context.getSchemaNode() instanceof SchemaContext) {
        /*
             *  Creates writer without initialNs and we write children of root data container
             *  which is not visible in restconf
             */
        nnWriter = createNormalizedNodeWriter(context, SchemaPath.ROOT, jsonWriter, depth);
        if (data instanceof ContainerNode) {
            writeChildren(nnWriter, (ContainerNode) data);
        } else if (data instanceof DOMSourceAnyxmlNode) {
            try {
                writeChildren(nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(context.getSchemaContext(), ((DOMSourceAnyxmlNode) data).body()).getResult());
            } catch (XMLStreamException | URISyntaxException | SAXException e) {
                throw new IOException("Cannot write anyxml.", e);
            }
        }
    } else if (context.getSchemaNode() instanceof RpcDefinition) {
        /*
             *  RpcDefinition is not supported as initial codec in JSONStreamWriter,
             *  so we need to emit initial output declaratation..
             */
        final RpcDefinition rpc = (RpcDefinition) context.getSchemaNode();
        final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
        nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
        jsonWriter.name("output");
        jsonWriter.beginObject();
        writeChildren(nnWriter, (ContainerNode) data);
        jsonWriter.endObject();
    } else {
        final List<QName> qnames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
        final SchemaPath path = SchemaPath.of(Absolute.of(qnames)).getParent();
        if (data instanceof MapEntryNode) {
            data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build();
        }
        nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
        nnWriter.write(data);
    }
    nnWriter.flush();
}
Also used : JSONNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) Produces(javax.ws.rs.Produces) Provider(javax.ws.rs.ext.Provider) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) URISyntaxException(java.net.URISyntaxException) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) Draft02(org.opendaylight.netconf.sal.rest.api.Draft02) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) NetconfUtil(org.opendaylight.netconf.util.NetconfUtil) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) JsonWriterFactory(org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) Nullable(org.eclipse.jdt.annotation.Nullable) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStreamWriter(java.io.OutputStreamWriter) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RestconfService(org.opendaylight.netconf.sal.rest.api.RestconfService) JsonWriter(com.google.gson.stream.JsonWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) OutputStream(java.io.OutputStream) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) IOException(java.io.IOException) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) QName(org.opendaylight.yangtools.yang.common.QName) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Type(java.lang.reflect.Type) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) SAXException(org.xml.sax.SAXException) Annotation(java.lang.annotation.Annotation) Entry(java.util.Map.Entry) JSONCodecFactory(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfNormalizedNodeWriter(org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter) DOMSourceAnyxmlNode(org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) IOException(java.io.IOException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) List(java.util.List) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)

Aggregations

IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 Annotation (java.lang.annotation.Annotation)2 Type (java.lang.reflect.Type)2 URISyntaxException (java.net.URISyntaxException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Collectors (java.util.stream.Collectors)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 MediaType (javax.ws.rs.core.MediaType)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)2 Provider (javax.ws.rs.ext.Provider)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 Draft02 (org.opendaylight.netconf.sal.rest.api.Draft02)2 RestconfNormalizedNodeWriter (org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter)2 RestconfService (org.opendaylight.netconf.sal.rest.api.RestconfService)2