use of org.opendaylight.yangtools.yang.model.api.OperationDefinition in project netconf by opendaylight.
the class JsonNormalizedNodeBodyReader method readFrom.
public static NormalizedNodePayload readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost) {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final EffectiveStatementInference parentSchema;
final SchemaInferenceStack stack;
if (path.getSchemaNode() instanceof RpcEffectiveStatement) {
stack = SchemaInferenceStack.of(path.getSchemaContext(), Absolute.of(path.getSchemaNode().getQName()));
} else {
stack = SchemaInferenceStack.of(path.getSchemaContext());
path.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
}
if (!isPost) {
stack.exit();
}
parentSchema = stack.toInference();
final JsonParserStream jsonParser = JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()), parentSchema);
final JsonReader reader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
jsonParser.parse(reader);
NormalizedNode result = resultHolder.getResult();
final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
InstanceIdentifierContext<? extends SchemaNode> newIIContext;
while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
final Object childNode = ((DataContainerNode) result).body().iterator().next();
if (isPost) {
iiToDataList.add(result.getIdentifier());
}
result = (NormalizedNode) childNode;
}
if (isPost) {
if (result instanceof MapEntryNode) {
iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getIdentifier().getNodeType()));
iiToDataList.add(result.getIdentifier());
} else {
final List<? extends @NonNull EffectiveStatement<?, ?>> parentPath = parentSchema.statementPath();
if (parentPath.isEmpty() || !(parentPath.get(parentPath.size() - 1) instanceof OperationDefinition)) {
iiToDataList.add(result.getIdentifier());
}
}
} else {
if (result instanceof MapNode) {
result = Iterables.getOnlyElement(((MapNode) result).body());
}
}
final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(path.getInstanceIdentifier().getPathArguments(), iiToDataList));
newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(), path.getSchemaContext());
// FIXME: can result really be null?
return NormalizedNodePayload.ofNullable(newIIContext, result);
}
use of org.opendaylight.yangtools.yang.model.api.OperationDefinition in project netconf by opendaylight.
the class XmlNormalizedNodeBodyReader method parse.
private NormalizedNodePayload parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
DataSchemaNode schemaNode;
final boolean isOperation;
if (schemaNodeContext instanceof OperationDefinition) {
schemaNode = ((OperationDefinition) schemaNodeContext).getInput();
isOperation = true;
} else if (schemaNodeContext instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) schemaNodeContext;
isOperation = false;
} else {
throw new IllegalStateException("Unknown SchemaNode " + schemaNodeContext);
}
final String docRootElm = doc.getDocumentElement().getLocalName();
final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
if (isPost() && !isOperation) {
final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm, docRootNamespace);
if (foundSchemaNodes.isEmpty()) {
throw new IllegalStateException(String.format("Child \"%s\" was not found in parent schema node \"%s\"", docRootElm, schemaNode.getQName()));
}
while (!foundSchemaNodes.isEmpty()) {
final Object child = foundSchemaNodes.pop();
if (child instanceof AugmentationSchemaNode) {
final AugmentationSchemaNode augmentSchemaNode = (AugmentationSchemaNode) child;
iiToDataList.add(DataSchemaContextNode.augmentationIdentifierFrom(augmentSchemaNode));
} else if (child instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) child;
iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(schemaNode.getQName()));
}
}
// PUT
} else if (!isOperation) {
final QName scQName = schemaNode.getQName();
checkState(docRootElm.equals(scQName.getLocalName()) && docRootNamespace.equals(scQName.getNamespace().toString()), "Not correct message root element \"%s\", should be \"%s\"", docRootElm, scQName);
}
NormalizedNode parsed;
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
if (schemaNode instanceof ContainerLike || schemaNode instanceof ListSchemaNode || schemaNode instanceof LeafSchemaNode) {
final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(pathContext.getSchemaContext(), schemaNode.getPath()).toInference());
xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
parsed = resultHolder.getResult();
// Therefore we now have to extract the MapEntryNode from the parsed MapNode.
if (parsed instanceof MapNode) {
final MapNode mapNode = (MapNode) parsed;
// extracting the MapEntryNode
parsed = mapNode.body().iterator().next();
}
if (schemaNode instanceof ListSchemaNode && isPost()) {
iiToDataList.add(parsed.getIdentifier());
}
} else {
LOG.warn("Unknown schema node extension {} was not parsed", schemaNode.getClass());
parsed = null;
}
final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(pathContext.getInstanceIdentifier().getPathArguments(), iiToDataList));
final InstanceIdentifierContext<? extends SchemaNode> outIIContext = new InstanceIdentifierContext<>(fullIIToData, pathContext.getSchemaNode(), pathContext.getMountPoint(), pathContext.getSchemaContext());
// FIXME: can result really be null?
return NormalizedNodePayload.ofNullable(outIIContext, parsed);
}
Aggregations