Search in sources :

Example 6 with DataNodeContainer

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

the class NormalizedNodeStreamWriterStack method startAugmentationNode.

public AugmentationSchemaNode startAugmentationNode(final AugmentationIdentifier identifier) {
    LOG.debug("Enter augmentation {}", identifier);
    Object parent = getParent();
    checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
    if (parent instanceof ChoiceSchemaNode) {
        final QName name = Iterables.get(identifier.getPossibleChildNames(), 0);
        parent = findCaseByChild((ChoiceSchemaNode) parent, name);
    }
    checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer", parent);
    final AugmentationSchemaNode schema = findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
    final AugmentationSchemaNode resolvedSchema = new EffectiveAugmentationSchema(schema, (DataNodeContainer) parent);
    schemaStack.push(resolvedSchema);
    return resolvedSchema;
}
Also used : AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) EffectiveAugmentationSchema(org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema) QName(org.opendaylight.yangtools.yang.common.QName) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) AugmentationTarget(org.opendaylight.yangtools.yang.model.api.AugmentationTarget) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)

Example 7 with DataNodeContainer

use of org.opendaylight.yangtools.yang.model.api.DataNodeContainer in project cps by onap.

the class YangUtils method findDataSchemaNodeByXpathNodeIdSequence.

private static DataSchemaNode findDataSchemaNodeByXpathNodeIdSequence(final String[] xpathNodeIdSequence, final Collection<? extends DataSchemaNode> dataSchemaNodes) {
    final String currentXpathNodeId = xpathNodeIdSequence[0];
    final DataSchemaNode currentDataSchemaNode = dataSchemaNodes.stream().filter(dataSchemaNode -> currentXpathNodeId.equals(dataSchemaNode.getQName().getLocalName())).findFirst().orElseThrow(() -> schemaNodeNotFoundException(currentXpathNodeId));
    if (xpathNodeIdSequence.length <= 1) {
        return currentDataSchemaNode;
    }
    if (currentDataSchemaNode instanceof DataNodeContainer) {
        return findDataSchemaNodeByXpathNodeIdSequence(getNextLevelXpathNodeIdSequence(xpathNodeIdSequence), ((DataNodeContainer) currentDataSchemaNode).getChildNodes());
    }
    throw schemaNodeNotFoundException(xpathNodeIdSequence[1]);
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer)

Example 8 with DataNodeContainer

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

the class XmlNormalizedNodeBodyReader method findPathToSchemaNodeByName.

private static Deque<Object> findPathToSchemaNodeByName(final DataSchemaNode schemaNode, final String elementName, final String namespace) {
    final Deque<Object> result = new ArrayDeque<>();
    final ArrayList<ChoiceSchemaNode> choiceSchemaNodes = new ArrayList<>();
    for (final DataSchemaNode child : ((DataNodeContainer) schemaNode).getChildNodes()) {
        if (child instanceof ChoiceSchemaNode) {
            choiceSchemaNodes.add((ChoiceSchemaNode) child);
        } else if (child.getQName().getLocalName().equalsIgnoreCase(elementName) && child.getQName().getNamespace().toString().equalsIgnoreCase(namespace)) {
            // add child to result
            result.push(child);
            // find augmentation
            if (child.isAugmenting()) {
                final AugmentationSchemaNode augment = findCorrespondingAugment(schemaNode, child);
                if (augment != null) {
                    result.push(augment);
                }
            }
            // return result
            return result;
        }
    }
    for (final ChoiceSchemaNode choiceNode : choiceSchemaNodes) {
        for (final CaseSchemaNode caseNode : choiceNode.getCases()) {
            final Deque<Object> resultFromRecursion = findPathToSchemaNodeByName(caseNode, elementName, namespace);
            if (!resultFromRecursion.isEmpty()) {
                resultFromRecursion.push(choiceNode);
                if (choiceNode.isAugmenting()) {
                    final AugmentationSchemaNode augment = findCorrespondingAugment(schemaNode, choiceNode);
                    if (augment != null) {
                        resultFromRecursion.push(augment);
                    }
                }
                return resultFromRecursion;
            }
        }
    }
    return result;
}
Also used : AugmentationSchemaNode(org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ArrayList(java.util.ArrayList) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) ArrayDeque(java.util.ArrayDeque)

Example 9 with DataNodeContainer

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

the class RestconfDocumentedExceptionMapper method toJsonResponseBody.

private static Object toJsonResponseBody(final NormalizedNodeContext errorsNode, final DataNodeContainer errorsSchemaNode) {
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    NormalizedNode data = errorsNode.getData();
    final InstanceIdentifierContext<?> context = errorsNode.getInstanceIdentifierContext();
    final DataSchemaNode schema = (DataSchemaNode) context.getSchemaNode();
    final OutputStreamWriter outputWriter = new OutputStreamWriter(outStream, StandardCharsets.UTF_8);
    if (data == null) {
        throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
    }
    boolean isDataRoot = false;
    XMLNamespace initialNs = null;
    SchemaPath path;
    if (context.getSchemaNode() instanceof SchemaContext) {
        isDataRoot = true;
        path = SchemaPath.ROOT;
    } else {
        final List<QName> qNames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
        path = SchemaPath.of(Absolute.of(qNames)).getParent();
    }
    if (!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
        initialNs = schema.getQName().getNamespace();
    }
    final JsonWriter jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
    final NormalizedNodeStreamWriter jsonStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext()), path, initialNs, jsonWriter);
    // We create a delegating writer to special-case error-info as error-info is defined as an empty
    // container in the restconf yang schema but we create a leaf node so we can output it. The delegate
    // stream writer validates the node type against the schema and thus will expect a LeafSchemaNode but
    // the schema has a ContainerSchemaNode so, to avoid an error, we override the leafNode behavior
    // for error-info.
    final NormalizedNodeStreamWriter streamWriter = new ForwardingNormalizedNodeStreamWriter() {

        private boolean inOurLeaf;

        @Override
        protected NormalizedNodeStreamWriter delegate() {
            return jsonStreamWriter;
        }

        @Override
        public void startLeafNode(final NodeIdentifier name) throws IOException {
            if (name.getNodeType().equals(RestConfModule.ERROR_INFO_QNAME)) {
                inOurLeaf = true;
                jsonWriter.name(RestConfModule.ERROR_INFO_QNAME.getLocalName());
            } else {
                super.startLeafNode(name);
            }
        }

        @Override
        public void scalarValue(final Object value) throws IOException {
            if (inOurLeaf) {
                jsonWriter.value(value.toString());
            } else {
                super.scalarValue(value);
            }
        }

        @Override
        public void endNode() throws IOException {
            if (inOurLeaf) {
                inOurLeaf = false;
            } else {
                super.endNode();
            }
        }
    };
    final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(streamWriter);
    try {
        if (isDataRoot) {
            writeDataRoot(outputWriter, nnWriter, (ContainerNode) data);
        } else {
            if (data instanceof MapEntryNode) {
                data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build();
            }
            nnWriter.write(data);
        }
        nnWriter.flush();
        outputWriter.flush();
    } catch (final IOException e) {
        LOG.warn("Error writing error response body", e);
    }
    try {
        streamWriter.close();
    } catch (IOException e) {
        LOG.warn("Failed to close stream writer", e);
    }
    return outStream.toString(StandardCharsets.UTF_8);
}
Also used : JSONNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) Provider(javax.ws.rs.ext.Provider) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ForwardingNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) LoggerFactory(org.slf4j.LoggerFactory) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ERRORS_CONTAINER_QNAME(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule.ERRORS_CONTAINER_QNAME) MediaType(javax.ws.rs.core.MediaType) NormalizedNodeWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter) XMLStreamException(javax.xml.stream.XMLStreamException) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) CollectionNodeBuilder(org.opendaylight.yangtools.yang.data.api.schema.builder.CollectionNodeBuilder) Context(javax.ws.rs.core.Context) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) DataContainerChild(org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ErrorTags(org.opendaylight.restconf.common.ErrorTags) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) List(java.util.List) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) ImmutableNodes(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) Iterables(com.google.common.collect.Iterables) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) ArrayList(java.util.ArrayList) RestConfModule(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule) JsonWriterFactory(org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory) Objects.requireNonNull(java.util.Objects.requireNonNull) OutputStreamWriter(java.io.OutputStreamWriter) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) XMLConstants(javax.xml.XMLConstants) JsonWriter(com.google.gson.stream.JsonWriter) Status(javax.ws.rs.core.Response.Status) SchemaAwareBuilders(org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders) Logger(org.slf4j.Logger) DataContainerNodeBuilder(org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder) IOException(java.io.IOException) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) QName(org.opendaylight.yangtools.yang.common.QName) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ControllerContext(org.opendaylight.netconf.sal.restconf.impl.ControllerContext) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) LeafSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafSchemaNode) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) NormalizedNodeWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter) QName(org.opendaylight.yangtools.yang.common.QName) JSONNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter) ForwardingNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter) XMLStreamNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NodeIdentifierWithPredicates(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) JsonWriter(com.google.gson.stream.JsonWriter) SchemaPath(org.opendaylight.yangtools.yang.model.api.SchemaPath) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) ForwardingNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) SchemaContext(org.opendaylight.yangtools.yang.model.api.SchemaContext) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode)

Example 10 with DataNodeContainer

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

the class ControllerContext method toFullRestconfIdentifier.

public String toFullRestconfIdentifier(final YangInstanceIdentifier path, final DOMMountPoint mount) {
    checkPreconditions();
    final Iterable<PathArgument> elements = path.getPathArguments();
    final StringBuilder builder = new StringBuilder();
    final PathArgument head = elements.iterator().next();
    final QName startQName = head.getNodeType();
    final EffectiveModelContext schemaContext;
    if (mount != null) {
        schemaContext = getModelContext(mount);
    } else {
        schemaContext = globalSchema;
    }
    final Module initialModule = schemaContext.findModule(startQName.getModule()).orElse(null);
    DataNodeContainer node = initialModule;
    for (final PathArgument element : elements) {
        if (!(element instanceof AugmentationIdentifier)) {
            final QName _nodeType = element.getNodeType();
            final DataSchemaNode potentialNode = childByQName(node, _nodeType);
            if ((!(element instanceof NodeIdentifier) || !(potentialNode instanceof ListSchemaNode)) && !(potentialNode instanceof ChoiceSchemaNode)) {
                builder.append(convertToRestconfIdentifier(element, potentialNode, mount));
                if (potentialNode instanceof DataNodeContainer) {
                    node = (DataNodeContainer) potentialNode;
                }
            }
        }
    }
    return builder.toString();
}
Also used : AugmentationIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) PathArgument(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument) Module(org.opendaylight.yangtools.yang.model.api.Module) RestConfModule(org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Aggregations

DataNodeContainer (org.opendaylight.yangtools.yang.model.api.DataNodeContainer)41 DataSchemaNode (org.opendaylight.yangtools.yang.model.api.DataSchemaNode)34 QName (org.opendaylight.yangtools.yang.common.QName)15 ListSchemaNode (org.opendaylight.yangtools.yang.model.api.ListSchemaNode)14 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)12 ChoiceSchemaNode (org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode)12 CaseSchemaNode (org.opendaylight.yangtools.yang.model.api.CaseSchemaNode)11 ContainerSchemaNode (org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode)10 LeafListSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode)9 LeafSchemaNode (org.opendaylight.yangtools.yang.model.api.LeafSchemaNode)9 ArrayList (java.util.ArrayList)7 NodeIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier)7 RestConfModule (org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 Test (org.junit.Test)5 AugmentationSchemaNode (org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode)5 Module (org.opendaylight.yangtools.yang.model.api.Module)5 ContainerNode (org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ArrayDeque (java.util.ArrayDeque)3