use of org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream in project netconf by opendaylight.
the class JsonNormalizedNodeBodyReader method readFrom.
private static NormalizedNodeContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost) throws IOException {
final Optional<InputStream> nonEmptyInputStreamOptional = RestUtil.isInputStreamEmpty(entityStream);
if (nonEmptyInputStreamOptional.isEmpty()) {
return new NormalizedNodeContext(path, null);
}
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
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();
}
final JsonParserStream jsonParser = JsonParserStream.create(writer, JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(path.getSchemaContext()), stack.toInference());
final JsonReader reader = new JsonReader(new InputStreamReader(nonEmptyInputStreamOptional.get(), 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 {
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());
return new NormalizedNodeContext(newIIContext, result);
}
use of org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream in project opennms-opendaylight-plugin by OpenNMS.
the class YangDecoder method normalizedNodeFromJsonString.
public NormalizedNode<?, ?> normalizedNodeFromJsonString(final String inputJson) {
final NormalizedNodeResult result = new NormalizedNodeResult();
final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
// note: context used to be generated by using loadModules from TestUtils in
// org.opendaylight.yangtools.yang.data.codec.gson
final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, this.context);
jsonParser.parse(new JsonReader(new StringReader(inputJson)));
final NormalizedNode<?, ?> transformedInput = result.getResult();
return transformedInput;
}
use of org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream in project cps by onap.
the class YangUtils method parseJsonData.
private static NormalizedNode<?, ?> parseJsonData(final String jsonData, final SchemaContext schemaContext, final Optional<DataSchemaNode> optionalParentSchemaNode) {
final var jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared((EffectiveModelContext) schemaContext);
final var normalizedNodeResult = new NormalizedNodeResult();
final var normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter.from(normalizedNodeResult);
try (final JsonParserStream jsonParserStream = optionalParentSchemaNode.isPresent() ? JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, optionalParentSchemaNode.get()) : JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory)) {
final var jsonReader = new JsonReader(new StringReader(jsonData));
jsonParserStream.parse(jsonReader);
} catch (final IOException | JsonSyntaxException exception) {
throw new DataValidationException("Failed to parse json data: " + jsonData, exception.getMessage(), exception);
} catch (final IllegalStateException illegalStateException) {
throw new DataValidationException("Failed to parse json data. Unsupported xpath or json data:" + jsonData, illegalStateException.getMessage(), illegalStateException);
}
return normalizedNodeResult.getResult();
}
use of org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream in project netconf by opendaylight.
the class LibraryModulesSchemas method readJson.
private static Optional<NormalizedNode> readJson(final InputStream in) {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final JsonParserStream jsonParser = JsonParserStream.create(writer, JSON_CODECS);
final JsonReader reader = new JsonReader(new InputStreamReader(in, Charset.defaultCharset()));
jsonParser.parse(reader);
return resultHolder.isFinished() ? Optional.of(resultHolder.getResult()) : Optional.empty();
}
use of org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream 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);
}
Aggregations