Search in sources :

Example 11 with ActionDefinition

use of org.opendaylight.yangtools.yang.model.api.ActionDefinition in project netconf by opendaylight.

the class XmlNormalizedNodeBodyWriter method writeNormalizedNode.

private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final InstanceIdentifierContext<?> pathContext, final NormalizedNode data, final DepthParam depth, final List<Set<QName>> fields) throws IOException {
    final RestconfNormalizedNodeWriter nnWriter;
    final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
    if (pathContext.getSchemaNode() instanceof RpcDefinition) {
        /*
             *  RpcDefinition is not supported as initial codec in XMLStreamWriter,
             *  so we need to emit initial output declaration..
             */
        final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
        final SchemaPath rpcPath = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, rpcPath, depth, fields);
        writeElements(xmlWriter, nnWriter, (ContainerNode) data);
    } else if (pathContext.getSchemaNode() instanceof ActionDefinition) {
        /*
             *  ActionDefinition is not supported as initial codec in XMLStreamWriter,
             *  so we need to emit initial output declaration..
             */
        final ActionDefinition actDef = (ActionDefinition) pathContext.getSchemaNode();
        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());
        qNames.add(actDef.getQName());
        qNames.add(actDef.getOutput().getQName());
        final SchemaPath actPath = SchemaPath.of(Absolute.of(qNames));
        nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, actPath, depth, fields);
        writeElements(xmlWriter, nnWriter, (ContainerNode) data);
    } else {
        final boolean isRoot = pathContext.getInstanceIdentifier().isEmpty();
        if (isRoot) {
            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth, fields);
        } 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));
            nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path.getParent(), depth, fields);
        }
        if (data instanceof MapEntryNode) {
            // Restconf allows returning one list item. We need to wrap it
            // in map node in order to serialize it properly
            nnWriter.write(ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).addChild((MapEntryNode) data).build());
        } else if (isRoot) {
            if (data instanceof ContainerNode && ((ContainerNode) data).isEmpty()) {
                writeEmptyDataNode(xmlWriter, data);
            } else {
                writeAndWrapInDataNode(xmlWriter, nnWriter, data);
            }
        } else {
            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) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) NormalizedNodePayload(org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) XMLConstants(javax.xml.XMLConstants) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) OutputStream(java.io.OutputStream) DepthParam(org.opendaylight.restconf.nb.rfc8040.DepthParam) Set(java.util.Set) 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) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) MediaTypes(org.opendaylight.restconf.nb.rfc8040.MediaTypes) RestconfNormalizedNodeWriter(org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter) 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) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) RestconfNormalizedNodeWriter(org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter) RpcDefinition(org.opendaylight.yangtools.yang.model.api.RpcDefinition) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActionDefinition(org.opendaylight.yangtools.yang.model.api.ActionDefinition) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) List(java.util.List) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Aggregations

ActionDefinition (org.opendaylight.yangtools.yang.model.api.ActionDefinition)11 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)5 QName (org.opendaylight.yangtools.yang.common.QName)4 IOException (java.io.IOException)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)3 NormalizedNodePayload (org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload)3 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)3 PathArgument (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument)3 MapEntryNode (org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode)3 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)3 Absolute (org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute)3 OutputStream (java.io.OutputStream)2 Annotation (java.lang.annotation.Annotation)2 Type (java.lang.reflect.Type)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Set (java.util.Set)2 Test (org.junit.Test)2