use of org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter in project netconf by opendaylight.
the class NormalizedNodeXmlBodyWriter method writeNormalizedNode.
private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final InstanceIdentifierContext<?> pathContext, NormalizedNode data, @Nullable final Integer depth) throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
if (pathContext.getSchemaNode() instanceof SchemaContext) {
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth);
if (data instanceof DOMSourceAnyxmlNode) {
try {
writeElements(xmlWriter, nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(schemaCtx, ((DOMSourceAnyxmlNode) data).body()).getResult());
} catch (XMLStreamException | URISyntaxException | SAXException e) {
throw new IOException("Cannot write anyxml", e);
}
} else {
writeElements(xmlWriter, nnWriter, (ContainerNode) data);
}
} else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
writeElements(xmlWriter, nnWriter, (ContainerNode) data);
} else {
final List<QName> qNames = pathContext.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
final SchemaPath path = SchemaPath.of(Absolute.of(qNames)).getParent();
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
if (data instanceof MapEntryNode) {
// Restconf allows returning one list item. We need to wrap it
// in map node in order to serialize it properly
data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).addChild((MapEntryNode) data).build();
}
nnWriter.write(data);
}
nnWriter.flush();
}
use of org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter in project netconf by opendaylight.
the class NormalizedNodeJsonBodyWriter method writeNormalizedNode.
private static void writeNormalizedNode(final JsonWriter jsonWriter, final InstanceIdentifierContext<SchemaNode> context, NormalizedNode data, @Nullable final Integer depth) throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
if (context.getSchemaNode() instanceof SchemaContext) {
/*
* Creates writer without initialNs and we write children of root data container
* which is not visible in restconf
*/
nnWriter = createNormalizedNodeWriter(context, SchemaPath.ROOT, jsonWriter, depth);
if (data instanceof ContainerNode) {
writeChildren(nnWriter, (ContainerNode) data);
} else if (data instanceof DOMSourceAnyxmlNode) {
try {
writeChildren(nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(context.getSchemaContext(), ((DOMSourceAnyxmlNode) data).body()).getResult());
} catch (XMLStreamException | URISyntaxException | SAXException e) {
throw new IOException("Cannot write anyxml.", e);
}
}
} else if (context.getSchemaNode() instanceof RpcDefinition) {
/*
* RpcDefinition is not supported as initial codec in JSONStreamWriter,
* so we need to emit initial output declaratation..
*/
final RpcDefinition rpc = (RpcDefinition) context.getSchemaNode();
final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
jsonWriter.name("output");
jsonWriter.beginObject();
writeChildren(nnWriter, (ContainerNode) data);
jsonWriter.endObject();
} else {
final List<QName> qnames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
final SchemaPath path = SchemaPath.of(Absolute.of(qnames)).getParent();
if (data instanceof MapEntryNode) {
data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build();
}
nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
nnWriter.write(data);
}
nnWriter.flush();
}
Aggregations