use of org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement in project yangtools by opendaylight.
the class YT1291Test method testRpcIndexing.
@Test
public void testRpcIndexing() {
final SchemaTreeEffectiveStatement<?> foo = Iterables.getOnlyElement(context.getModuleStatements().values()).findSchemaTreeNode(FOO).orElseThrow();
assertThat(foo, instanceOf(RpcEffectiveStatement.class));
final RpcEffectiveStatement rpc = (RpcEffectiveStatement) foo;
assertThat(rpc.findDataTreeNode(INPUT).orElseThrow(), instanceOf(InputEffectiveStatement.class));
assertThat(rpc.findDataTreeNode(OUTPUT).orElseThrow(), instanceOf(OutputEffectiveStatement.class));
}
use of org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement 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.model.api.stmt.RpcEffectiveStatement in project yangtools by opendaylight.
the class NormalizedNodeStreamWriterStack method ofOperation.
/**
* Create a new writer with the specified context and rooted in the specified schema path.
*
* @param context Associated {@link EffectiveModelContext}
* @param operation Operation schema path
* @param qname Input/Output container QName
* @return A new {@link NormalizedNodeStreamWriter}
* @throws NullPointerException if any argument is null
* @throws IllegalArgumentException if {@code operation} does not point to an actual operation or if {@code qname}
* does not identify a valid root underneath it.
*/
@NonNull
public static NormalizedNodeStreamWriterStack ofOperation(final EffectiveModelContext context, final Absolute operation, final QName qname) {
final SchemaInferenceStack stack = SchemaInferenceStack.of(context, operation);
final EffectiveStatement<?, ?> current = stack.currentStatement();
checkArgument(current instanceof RpcEffectiveStatement || current instanceof ActionEffectiveStatement, "Path %s resolved into non-operation %s", operation, current);
stack.enterSchemaTree(qname);
return new NormalizedNodeStreamWriterStack(stack);
}
use of org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement 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.stmt.RpcEffectiveStatement in project mdsal by opendaylight.
the class RpcServiceGenerator method createTypeImpl.
@Override
GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
builder.addImplementsType(BindingTypes.RPC_SERVICE);
for (RpcGenerator rpcGen : rpcs) {
final RpcEffectiveStatement rpc = rpcGen.statement();
final QName qname = rpc.argument();
// FIXME: this may still conflict in theory
final MethodSignatureBuilder method = builder.addMethod(BindingMapping.getRpcMethodName(qname));
method.addParameter(getChild(rpcGen, InputEffectiveStatement.class).getGeneratedType(builderFactory), "input");
method.setReturnType(Types.listenableFutureTypeFor(BindingTypes.rpcResult(getChild(rpcGen, OutputEffectiveStatement.class).getGeneratedType(builderFactory))));
// FIXME: this should not be part of runtime types
method.addAnnotation(CHECK_RETURN_VALUE_ANNOTATION);
final String rpcName = qname.getLocalName();
method.setComment(new TypeMemberComment("Invoke {@code " + rpcName + "} RPC.", rpc.findFirstEffectiveSubstatementArgument(DescriptionEffectiveStatement.class).orElse(null), "@param input of {@code " + rpcName + "}\n" + "@return output of {@code " + rpcName + '}'));
}
return builder.build();
}
Aggregations