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;
}
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;
}
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;
}
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;
}
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();
}
Aggregations