Search in sources :

Example 21 with DataNodeContainer

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

the class InMemoryDataTreeFactory method getRootSchemaNode.

private static DataSchemaNode getRootSchemaNode(final EffectiveModelContext schemaContext, final YangInstanceIdentifier rootPath) {
    final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
    final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
    checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
    final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
    checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s", rootPath, rootSchemaNode);
    return rootSchemaNode;
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DataSchemaContextNode(org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) DataSchemaContextTree(org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree)

Example 22 with DataNodeContainer

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

the class NormalizedNodeStreamWriterStack method leafSetEntryNode.

public LeafListSchemaNode leafSetEntryNode(final QName qname) {
    final Object parent = getParent();
    if (parent instanceof LeafListSchemaNode) {
        return (LeafListSchemaNode) parent;
    }
    checkArgument(parent instanceof DataNodeContainer, "Cannot lookup %s in parent %s", qname, parent);
    final DataSchemaNode child = ((DataNodeContainer) parent).dataChildByName(qname);
    checkArgument(child instanceof LeafListSchemaNode, "Node %s is neither a leaf-list nor currently in a leaf-list", child);
    return (LeafListSchemaNode) child;
}
Also used : LeafListSchemaNode(org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer)

Example 23 with DataNodeContainer

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

the class ParserStreamUtils method findSchemaNodeByNameAndNamespace.

/**
 * Returns stack of schema nodes via which it was necessary to pass to get schema node with specified
 * {@code childName} and {@code namespace}.
 *
 * @return stack of schema nodes via which it was passed through. If found schema node is direct child then stack
 *         contains only one node. If it is found under choice and case then stack should contains 2*n+1 element
 *         (where n is number of choices through it was passed)
 */
public static Deque<DataSchemaNode> findSchemaNodeByNameAndNamespace(final DataSchemaNode dataSchemaNode, final String childName, final XMLNamespace namespace) {
    final Deque<DataSchemaNode> result = new ArrayDeque<>();
    final List<ChoiceSchemaNode> childChoices = new ArrayList<>();
    DataSchemaNode potentialChildNode = null;
    if (dataSchemaNode instanceof DataNodeContainer) {
        for (final DataSchemaNode childNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
            if (childNode instanceof ChoiceSchemaNode) {
                childChoices.add((ChoiceSchemaNode) childNode);
            } else {
                final QName childQName = childNode.getQName();
                if (childQName.getLocalName().equals(childName) && childQName.getNamespace().equals(namespace) && (potentialChildNode == null || Revision.compare(childQName.getRevision(), potentialChildNode.getQName().getRevision()) > 0)) {
                    potentialChildNode = childNode;
                }
            }
        }
    }
    if (potentialChildNode != null) {
        result.push(potentialChildNode);
        return result;
    }
    // try to find data schema node in choice (looking for first match)
    for (final ChoiceSchemaNode choiceNode : childChoices) {
        for (final CaseSchemaNode concreteCase : choiceNode.getCases()) {
            final Deque<DataSchemaNode> resultFromRecursion = findSchemaNodeByNameAndNamespace(concreteCase, childName, namespace);
            if (!resultFromRecursion.isEmpty()) {
                resultFromRecursion.push(concreteCase);
                resultFromRecursion.push(choiceNode);
                return resultFromRecursion;
            }
        }
    }
    return result;
}
Also used : DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) QName(org.opendaylight.yangtools.yang.common.QName) 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 24 with DataNodeContainer

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

the class JsonParserStream method resolveAllPotentialNamespaces.

private Set<XMLNamespace> resolveAllPotentialNamespaces(final String elementName, final DataSchemaNode dataSchemaNode) {
    final Set<XMLNamespace> potentialUris = new HashSet<>();
    final Set<ChoiceSchemaNode> choices = new HashSet<>();
    if (dataSchemaNode instanceof DataNodeContainer) {
        for (final DataSchemaNode childSchemaNode : ((DataNodeContainer) dataSchemaNode).getChildNodes()) {
            if (childSchemaNode instanceof ChoiceSchemaNode) {
                choices.add((ChoiceSchemaNode) childSchemaNode);
            } else if (childSchemaNode.getQName().getLocalName().equals(elementName)) {
                potentialUris.add(childSchemaNode.getQName().getNamespace());
            }
        }
        for (final ChoiceSchemaNode choiceNode : choices) {
            for (final CaseSchemaNode concreteCase : choiceNode.getCases()) {
                potentialUris.addAll(resolveAllPotentialNamespaces(elementName, concreteCase));
            }
        }
    }
    return potentialUris;
}
Also used : TypedDataSchemaNode(org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) CaseSchemaNode(org.opendaylight.yangtools.yang.model.api.CaseSchemaNode) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) ChoiceSchemaNode(org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode) HashSet(java.util.HashSet)

Example 25 with DataNodeContainer

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

the class RestconfDocumentedExceptionMapper method toResponse.

@Override
public Response toResponse(final RestconfDocumentedException exception) {
    LOG.debug("In toResponse: {}", exception.getMessage());
    final List<MediaType> mediaTypeList = new ArrayList<>();
    if (headers.getMediaType() != null) {
        mediaTypeList.add(headers.getMediaType());
    }
    mediaTypeList.addAll(headers.getAcceptableMediaTypes());
    final MediaType mediaType = mediaTypeList.stream().filter(type -> !type.equals(MediaType.WILDCARD_TYPE)).findFirst().orElse(MediaType.APPLICATION_JSON_TYPE);
    LOG.debug("Using MediaType: {}", mediaType);
    final List<RestconfError> errors = exception.getErrors();
    if (errors.isEmpty()) {
        return Response.status(exception.getStatus()).type(MediaType.TEXT_PLAIN_TYPE).entity(" ").build();
    }
    final Status status = ErrorTags.statusOf(errors.iterator().next().getErrorTag());
    final DataNodeContainer errorsSchemaNode = (DataNodeContainer) controllerContext.getRestconfModuleErrorsSchemaNode();
    if (errorsSchemaNode == null) {
        return Response.status(status).type(MediaType.TEXT_PLAIN_TYPE).entity(exception.getMessage()).build();
    }
    checkState(errorsSchemaNode instanceof ContainerSchemaNode, "Found Errors SchemaNode isn't ContainerNode");
    final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> errContBuild = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) errorsSchemaNode);
    final List<DataSchemaNode> schemaList = ControllerContext.findInstanceDataChildrenByName(errorsSchemaNode, RestConfModule.ERROR_LIST_SCHEMA_NODE);
    final DataSchemaNode errListSchemaNode = Iterables.getFirst(schemaList, null);
    checkState(errListSchemaNode instanceof ListSchemaNode, "Found Error SchemaNode isn't ListSchemaNode");
    final CollectionNodeBuilder<MapEntryNode, SystemMapNode> listErorsBuilder = SchemaAwareBuilders.mapBuilder((ListSchemaNode) errListSchemaNode);
    for (final RestconfError error : errors) {
        listErorsBuilder.withChild(toErrorEntryNode(error, errListSchemaNode));
    }
    errContBuild.withChild(listErorsBuilder.build());
    final NormalizedNodeContext errContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(ERRORS, (DataSchemaNode) errorsSchemaNode, null, controllerContext.getGlobalSchema()), errContBuild.build());
    Object responseBody;
    if (mediaType.getSubtype().endsWith("json")) {
        responseBody = toJsonResponseBody(errContext, errorsSchemaNode);
    } else {
        responseBody = toXMLResponseBody(errContext, errorsSchemaNode);
    }
    return Response.status(status).type(mediaType).entity(responseBody).build();
}
Also used : Status(javax.ws.rs.core.Response.Status) SystemMapNode(org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode) DataSchemaNode(org.opendaylight.yangtools.yang.model.api.DataSchemaNode) ArrayList(java.util.ArrayList) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) ContainerSchemaNode(org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode) NodeIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier) ListSchemaNode(org.opendaylight.yangtools.yang.model.api.ListSchemaNode) RestconfError(org.opendaylight.restconf.common.errors.RestconfError) MediaType(javax.ws.rs.core.MediaType) DataNodeContainer(org.opendaylight.yangtools.yang.model.api.DataNodeContainer) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode)

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