use of org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter in project netconf by opendaylight.
the class JsonNormalizedNodeBodyWriter method writeNormalizedNode.
private static void writeNormalizedNode(final JsonWriter jsonWriter, final SchemaPath path, final InstanceIdentifierContext<SchemaNode> context, final NormalizedNode data, final DepthParam depth, final List<Set<QName>> fields) throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
if (context.getSchemaNode() instanceof RpcDefinition) {
/*
* RpcDefinition is not supported as initial codec in JSONStreamWriter,
* so we need to emit initial output declaration..
*/
final RpcDefinition rpc = (RpcDefinition) context.getSchemaNode();
final SchemaPath rpcPath = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
nnWriter = createNormalizedNodeWriter(context, rpcPath, jsonWriter, depth, fields);
final Module module = context.getSchemaContext().findModule(data.getIdentifier().getNodeType().getModule()).get();
jsonWriter.name(module.getName() + ":output");
jsonWriter.beginObject();
writeChildren(nnWriter, (ContainerNode) data);
jsonWriter.endObject();
} else if (context.getSchemaNode() instanceof ActionDefinition) {
/*
* ActionDefinition is not supported as initial codec in JSONStreamWriter,
* so we need to emit initial output declaration..
*/
final ActionDefinition actDef = (ActionDefinition) context.getSchemaNode();
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());
qNames.add(actDef.getQName());
qNames.add(actDef.getOutput().getQName());
final SchemaPath actPath = SchemaPath.of(Absolute.of(qNames));
nnWriter = createNormalizedNodeWriter(context, actPath, jsonWriter, depth, fields);
final Module module = context.getSchemaContext().findModule(data.getIdentifier().getNodeType().getModule()).get();
jsonWriter.name(module.getName() + ":output");
jsonWriter.beginObject();
writeChildren(nnWriter, (ContainerNode) data);
jsonWriter.endObject();
} else {
if (SchemaPath.ROOT.equals(path)) {
nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth, fields);
} else {
nnWriter = createNormalizedNodeWriter(context, path.getParent(), jsonWriter, depth, fields);
}
if (data instanceof MapEntryNode) {
// Restconf allows returning one list item. We need to wrap it
// in map node in order to serialize it properly
nnWriter.write(ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build());
} else {
nnWriter.write(data);
}
}
nnWriter.flush();
}
use of org.opendaylight.restconf.nb.rfc8040.jersey.providers.api.RestconfNormalizedNodeWriter in project netconf by opendaylight.
the class XmlNormalizedNodeBodyWriter method writeNormalizedNode.
private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final InstanceIdentifierContext<?> pathContext, final NormalizedNode data, final DepthParam depth, final List<Set<QName>> fields) throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
if (pathContext.getSchemaNode() instanceof RpcDefinition) {
/*
* RpcDefinition is not supported as initial codec in XMLStreamWriter,
* so we need to emit initial output declaration..
*/
final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
final SchemaPath rpcPath = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, rpcPath, depth, fields);
writeElements(xmlWriter, nnWriter, (ContainerNode) data);
} else if (pathContext.getSchemaNode() instanceof ActionDefinition) {
/*
* ActionDefinition is not supported as initial codec in XMLStreamWriter,
* so we need to emit initial output declaration..
*/
final ActionDefinition actDef = (ActionDefinition) pathContext.getSchemaNode();
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());
qNames.add(actDef.getQName());
qNames.add(actDef.getOutput().getQName());
final SchemaPath actPath = SchemaPath.of(Absolute.of(qNames));
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, actPath, depth, fields);
writeElements(xmlWriter, nnWriter, (ContainerNode) data);
} else {
final boolean isRoot = pathContext.getInstanceIdentifier().isEmpty();
if (isRoot) {
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth, fields);
} 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));
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path.getParent(), depth, fields);
}
if (data instanceof MapEntryNode) {
// Restconf allows returning one list item. We need to wrap it
// in map node in order to serialize it properly
nnWriter.write(ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).addChild((MapEntryNode) data).build());
} else if (isRoot) {
if (data instanceof ContainerNode && ((ContainerNode) data).isEmpty()) {
writeEmptyDataNode(xmlWriter, data);
} else {
writeAndWrapInDataNode(xmlWriter, nnWriter, data);
}
} else {
nnWriter.write(data);
}
}
nnWriter.flush();
}
Aggregations