Search in sources :

Example 1 with OperationDefinition

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

the class JsonNormalizedNodeBodyReader method readFrom.

public static NormalizedNodePayload readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost) {
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    final EffectiveStatementInference parentSchema;
    final SchemaInferenceStack stack;
    if (path.getSchemaNode() instanceof RpcEffectiveStatement) {
        stack = SchemaInferenceStack.of(path.getSchemaContext(), Absolute.of(path.getSchemaNode().getQName()));
    } else {
        stack = SchemaInferenceStack.of(path.getSchemaContext());
        path.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
    }
    if (!isPost) {
        stack.exit();
    }
    parentSchema = stack.toInference();
    final JsonParserStream jsonParser = JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()), parentSchema);
    final JsonReader reader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
    jsonParser.parse(reader);
    NormalizedNode result = resultHolder.getResult();
    final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
    InstanceIdentifierContext<? extends SchemaNode> newIIContext;
    while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
        final Object childNode = ((DataContainerNode) result).body().iterator().next();
        if (isPost) {
            iiToDataList.add(result.getIdentifier());
        }
        result = (NormalizedNode) childNode;
    }
    if (isPost) {
        if (result instanceof MapEntryNode) {
            iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getIdentifier().getNodeType()));
            iiToDataList.add(result.getIdentifier());
        } else {
            final List<? extends @NonNull EffectiveStatement<?, ?>> parentPath = parentSchema.statementPath();
            if (parentPath.isEmpty() || !(parentPath.get(parentPath.size() - 1) instanceof OperationDefinition)) {
                iiToDataList.add(result.getIdentifier());
            }
        }
    } else {
        if (result instanceof MapNode) {
            result = Iterables.getOnlyElement(((MapNode) result).body());
        }
    }
    final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(path.getInstanceIdentifier().getPathArguments(), iiToDataList));
    newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(), path.getSchemaContext());
    // FIXME: can result really be null?
    return NormalizedNodePayload.ofNullable(newIIContext, result);
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) OperationDefinition(org.opendaylight.yangtools.yang.model.api.OperationDefinition) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) Iterables(com.google.common.collect.Iterables) Provider(javax.ws.rs.ext.Provider) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) EffectiveStatementInference(org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference) LoggerFactory(org.slf4j.LoggerFactory) DOMMountPointService(org.opendaylight.mdsal.dom.api.DOMMountPointService) NormalizedNodePayload(org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) ResultAlreadySetException(org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException) Consumes(javax.ws.rs.Consumes) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) SchemaContextHandler(org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) EffectiveStatement(org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) Logger(org.slf4j.Logger) SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) Throwables(com.google.common.base.Throwables) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) MediaTypes(org.opendaylight.restconf.nb.rfc8040.MediaTypes) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) List(java.util.List) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NonNull(org.eclipse.jdt.annotation.NonNull) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStream(java.io.InputStream) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) EffectiveStatementInference(org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference) ArrayList(java.util.ArrayList) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NonNull(org.eclipse.jdt.annotation.NonNull) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) JsonReader(com.google.gson.stream.JsonReader) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) OperationDefinition(org.opendaylight.yangtools.yang.model.api.OperationDefinition) InputStreamReader(java.io.InputStreamReader) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode)

Example 2 with OperationDefinition

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

the class XmlNormalizedNodeBodyReader method parse.

private NormalizedNodePayload parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
    final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
    DataSchemaNode schemaNode;
    final boolean isOperation;
    if (schemaNodeContext instanceof OperationDefinition) {
        schemaNode = ((OperationDefinition) schemaNodeContext).getInput();
        isOperation = true;
    } else if (schemaNodeContext instanceof DataSchemaNode) {
        schemaNode = (DataSchemaNode) schemaNodeContext;
        isOperation = false;
    } else {
        throw new IllegalStateException("Unknown SchemaNode " + schemaNodeContext);
    }
    final String docRootElm = doc.getDocumentElement().getLocalName();
    final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
    final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
    if (isPost() && !isOperation) {
        final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm, docRootNamespace);
        if (foundSchemaNodes.isEmpty()) {
            throw new IllegalStateException(String.format("Child \"%s\" was not found in parent schema node \"%s\"", docRootElm, schemaNode.getQName()));
        }
        while (!foundSchemaNodes.isEmpty()) {
            final Object child = foundSchemaNodes.pop();
            if (child instanceof AugmentationSchemaNode) {
                final AugmentationSchemaNode augmentSchemaNode = (AugmentationSchemaNode) child;
                iiToDataList.add(DataSchemaContextNode.augmentationIdentifierFrom(augmentSchemaNode));
            } else if (child instanceof DataSchemaNode) {
                schemaNode = (DataSchemaNode) child;
                iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(schemaNode.getQName()));
            }
        }
    // PUT
    } else if (!isOperation) {
        final QName scQName = schemaNode.getQName();
        checkState(docRootElm.equals(scQName.getLocalName()) && docRootNamespace.equals(scQName.getNamespace().toString()), "Not correct message root element \"%s\", should be \"%s\"", docRootElm, scQName);
    }
    NormalizedNode parsed;
    final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
    final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
    if (schemaNode instanceof ContainerLike || schemaNode instanceof ListSchemaNode || schemaNode instanceof LeafSchemaNode) {
        final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(pathContext.getSchemaContext(), schemaNode.getPath()).toInference());
        xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
        parsed = resultHolder.getResult();
        // Therefore we now have to extract the MapEntryNode from the parsed MapNode.
        if (parsed instanceof MapNode) {
            final MapNode mapNode = (MapNode) parsed;
            // extracting the MapEntryNode
            parsed = mapNode.body().iterator().next();
        }
        if (schemaNode instanceof ListSchemaNode && isPost()) {
            iiToDataList.add(parsed.getIdentifier());
        }
    } else {
        LOG.warn("Unknown schema node extension {} was not parsed", schemaNode.getClass());
        parsed = null;
    }
    final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(pathContext.getInstanceIdentifier().getPathArguments(), iiToDataList));
    final InstanceIdentifierContext<? extends SchemaNode> outIIContext = new InstanceIdentifierContext<>(fullIIToData, pathContext.getSchemaNode(), pathContext.getMountPoint(), pathContext.getSchemaContext());
    // FIXME: can result really be null?
    return NormalizedNodePayload.ofNullable(outIIContext, parsed);
}
Also used : ContainerLike(org.opendaylight.yangtools.yang.model.api.ContainerLike) XmlParserStream(org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream) DOMSource(javax.xml.transform.dom.DOMSource) ArrayList(java.util.ArrayList) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) OperationDefinition(org.opendaylight.yangtools.yang.model.api.OperationDefinition) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode)

Aggregations

ArrayList (java.util.ArrayList)2 InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)2 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)2 MapNode (org.opendaylight.yangtools.yang.data.api.schema.MapNode)2 NormalizedNode (org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)2 NormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter)2 ImmutableNormalizedNodeStreamWriter (org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter)2 NormalizedNodeResult (org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult)2 OperationDefinition (org.opendaylight.yangtools.yang.model.api.OperationDefinition)2 SchemaNode (org.opendaylight.yangtools.yang.model.api.SchemaNode)2 Throwables (com.google.common.base.Throwables)1 Iterables (com.google.common.collect.Iterables)1 JsonReader (com.google.gson.stream.JsonReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 MediaType (javax.ws.rs.core.MediaType)1