Search in sources :

Example 1 with RpcEffectiveStatement

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));
}
Also used : RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) OutputEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.OutputEffectiveStatement) InputEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.InputEffectiveStatement) Test(org.junit.Test)

Example 2 with RpcEffectiveStatement

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);
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) Iterables(com.google.common.collect.Iterables) Provider(javax.ws.rs.ext.Provider) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) Draft02(org.opendaylight.netconf.sal.rest.api.Draft02) LoggerFactory(org.slf4j.LoggerFactory) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) ResultAlreadySetException(org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException) Consumes(javax.ws.rs.Consumes) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) RestconfService(org.opendaylight.netconf.sal.rest.api.RestconfService) RestUtil(org.opendaylight.restconf.common.util.RestUtil) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) Logger(org.slf4j.Logger) SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) List(java.util.List) Type(java.lang.reflect.Type) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) ControllerContext(org.opendaylight.netconf.sal.restconf.impl.ControllerContext) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) ArrayList(java.util.ArrayList) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) JsonReader(com.google.gson.stream.JsonReader) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode)

Example 3 with RpcEffectiveStatement

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);
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) ActionEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement) NonNull(org.eclipse.jdt.annotation.NonNull) Objects.requireNonNull(java.util.Objects.requireNonNull)

Example 4 with RpcEffectiveStatement

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);
}
Also used : SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) OperationDefinition(org.opendaylight.yangtools.yang.model.api.OperationDefinition) JSONCodecFactorySupplier(org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier) Iterables(com.google.common.collect.Iterables) Provider(javax.ws.rs.ext.Provider) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) InstanceIdentifierContext(org.opendaylight.restconf.common.context.InstanceIdentifierContext) EffectiveStatementInference(org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference) LoggerFactory(org.slf4j.LoggerFactory) DOMMountPointService(org.opendaylight.mdsal.dom.api.DOMMountPointService) NormalizedNodePayload(org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) ErrorType(org.opendaylight.yangtools.yang.common.ErrorType) Absolute(org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute) MediaType(javax.ws.rs.core.MediaType) ResultAlreadySetException(org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException) Consumes(javax.ws.rs.Consumes) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) SchemaContextHandler(org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) EffectiveStatement(org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) Logger(org.slf4j.Logger) SchemaInferenceStack(org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack) ErrorTag(org.opendaylight.yangtools.yang.common.ErrorTag) Throwables(com.google.common.base.Throwables) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) MediaTypes(org.opendaylight.restconf.nb.rfc8040.MediaTypes) SchemaNode(org.opendaylight.yangtools.yang.model.api.SchemaNode) List(java.util.List) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) WebApplicationException(javax.ws.rs.WebApplicationException) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) NonNull(org.eclipse.jdt.annotation.NonNull) RestconfDocumentedException(org.opendaylight.restconf.common.errors.RestconfDocumentedException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) InputStream(java.io.InputStream) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) JsonParserStream(org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) EffectiveStatementInference(org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference) ArrayList(java.util.ArrayList) DataContainerNode(org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode) MapNode(org.opendaylight.yangtools.yang.data.api.schema.MapNode) NonNull(org.eclipse.jdt.annotation.NonNull) NormalizedNodeResult(org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult) JsonReader(com.google.gson.stream.JsonReader) ChoiceNode(org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) OperationDefinition(org.opendaylight.yangtools.yang.model.api.OperationDefinition) InputStreamReader(java.io.InputStreamReader) ImmutableNormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter) NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) MapEntryNode(org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) AugmentationNode(org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode)

Example 5 with RpcEffectiveStatement

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();
}
Also used : DescriptionEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement) GeneratedTypeBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder) RpcEffectiveStatement(org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement) TypeMemberComment(org.opendaylight.mdsal.binding.model.api.TypeMemberComment) QName(org.opendaylight.yangtools.yang.common.QName) MethodSignatureBuilder(org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder)

Aggregations

RpcEffectiveStatement (org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement)5 Throwables (com.google.common.base.Throwables)2 Iterables (com.google.common.collect.Iterables)2 JsonReader (com.google.gson.stream.JsonReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Consumes (javax.ws.rs.Consumes)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 MediaType (javax.ws.rs.core.MediaType)2 Provider (javax.ws.rs.ext.Provider)2 NonNull (org.eclipse.jdt.annotation.NonNull)2 InstanceIdentifierContext (org.opendaylight.restconf.common.context.InstanceIdentifierContext)2 RestconfDocumentedException (org.opendaylight.restconf.common.errors.RestconfDocumentedException)2 ErrorTag (org.opendaylight.yangtools.yang.common.ErrorTag)2 ErrorType (org.opendaylight.yangtools.yang.common.ErrorType)2 YangInstanceIdentifier (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier)2 AugmentationNode (org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode)2