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