Search in sources :

Example 1 with ResourceNode

use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.

the class ClientContextHolder method buildContext.

/**
 * Build a parsable context from a Ballerina {@link ServiceNode}.
 *
 * @param service   {@code ServiceNode} for a valid ballerina source file
 * @param endpoints list of endpoints to be used as service endpoints for generated client
 * @return A parsable data model for provided ballerina {@code service}
 */
public static ClientContextHolder buildContext(ServiceNode service, List<EndpointNode> endpoints) throws CodeGeneratorException {
    ClientContextHolder context = new ClientContextHolder();
    context.name = service.getName().getValue();
    context.resources = new ArrayList<>();
    context.endpoints = new ArrayList<>();
    // Extract bound endpoint details
    for (EndpointNode ep : endpoints) {
        EndpointContextHolder epContext = EndpointContextHolder.buildContext(service, ep);
        if (epContext != null) {
            context.endpoints.add(EndpointContextHolder.buildContext(service, ep));
        }
    }
    // Extract ballerina resource nodes as parsable resources
    for (ResourceNode resource : service.getResources()) {
        ResourceContextHolder operation = ResourceContextHolder.buildContext(resource);
        context.resources.add(operation);
    }
    return context;
}
Also used : EndpointNode(org.ballerinalang.model.tree.EndpointNode) ResourceNode(org.ballerinalang.model.tree.ResourceNode)

Example 2 with ResourceNode

use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method parseHttpResourceConfig.

/**
 * Parses the 'http:resourceConfig' annotation attachment and build swagger operation.
 * @param resource The ballerina resource definition.
 * @param operationAdaptor The operation adaptor..
 */
private void parseHttpResourceConfig(ResourceNode resource, OperationAdaptor operationAdaptor) {
    Optional<? extends AnnotationAttachmentNode> responsesAnnotation = resource.getAnnotationAttachments().stream().filter(a -> null != this.httpAlias && this.httpAlias.equals(a.getPackageAlias().getValue()) && "resourceConfig".equals(a.getAnnotationName().getValue())).findFirst();
    if (responsesAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> configAttributes = this.listToMap(responsesAnnotation.get());
        if (configAttributes.containsKey("methods") && configAttributes.get("methods").getValueArray().size() > 0) {
            List<? extends AnnotationAttachmentAttributeValueNode> methodsValues = configAttributes.get("methods").getValueArray();
            // Since there is only one http method.
            operationAdaptor.setHttpOperation(this.getStringLiteralValue(methodsValues.get(0)));
        }
        if (configAttributes.containsKey("path")) {
            operationAdaptor.setPath(this.getStringLiteralValue(configAttributes.get("path")));
        }
        if (configAttributes.containsKey("produces") && configAttributes.get("produces").getValueArray().size() > 0) {
            List<String> produces = new LinkedList<>();
            List<? extends AnnotationAttachmentAttributeValueNode> producesValues = configAttributes.get("produces").getValueArray();
            for (AnnotationAttachmentAttributeValueNode producesValue : producesValues) {
                produces.add(this.getStringLiteralValue(producesValue));
            }
            operationAdaptor.getOperation().setProduces(produces);
        }
        if (configAttributes.containsKey("consumes") && configAttributes.get("consumes").getValueArray().size() > 0) {
            List<String> consumes = new LinkedList<>();
            List<? extends AnnotationAttachmentAttributeValueNode> consumesValues = configAttributes.get("consumes").getValueArray();
            for (AnnotationAttachmentAttributeValueNode consumesValue : consumesValues) {
                consumes.add(this.getStringLiteralValue(consumesValue));
            }
            operationAdaptor.getOperation().setConsumes(consumes);
        }
    }
}
Also used : Scheme(io.swagger.models.Scheme) Swagger(io.swagger.models.Swagger) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) HashMap(java.util.HashMap) ArrayProperty(io.swagger.models.properties.ArrayProperty) ResourceNode(org.ballerinalang.model.tree.ResourceNode) Model(io.swagger.models.Model) ArrayList(java.util.ArrayList) VariableNode(org.ballerinalang.model.tree.VariableNode) MediaType(javax.ws.rs.core.MediaType) Lists(com.google.common.collect.Lists) Path(io.swagger.models.Path) Locale(java.util.Locale) Map(java.util.Map) Operation(io.swagger.models.Operation) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Set(java.util.Set) Parameter(io.swagger.models.parameters.Parameter) HttpConstants(org.ballerinalang.net.http.HttpConstants) Collectors(java.util.stream.Collectors) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Response(io.swagger.models.Response) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) IntegerProperty(io.swagger.models.properties.IntegerProperty) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) BooleanProperty(io.swagger.models.properties.BooleanProperty) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) LinkedList(java.util.LinkedList)

Example 3 with ResourceNode

use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getStreamingServiceDefinition.

private static Service getStreamingServiceDefinition(ServiceNode serviceNode, ServiceConfiguration serviceConfig, File.Builder fileBuilder) throws GrpcServerException {
    // Protobuf service definition builder.
    Service.Builder serviceBuilder = Service.newBuilder(serviceNode.getName().getValue());
    Message requestMessage = null;
    Message responseMessage = null;
    for (ResourceNode resourceNode : serviceNode.getResources()) {
        if (ON_MESSAGE_RESOURCE.equals(resourceNode.getName().getValue())) {
            requestMessage = getRequestMessage(resourceNode);
            Message respMsg = getResponseMessage(resourceNode);
            if (respMsg != null && !(MessageKind.EMPTY.equals(respMsg.getMessageKind()))) {
                responseMessage = getResponseMessage(resourceNode);
                break;
            }
        }
        if (ON_COMPLETE_RESOURCE.equals(resourceNode.getName().getValue())) {
            Message respMsg = getResponseMessage(resourceNode);
            if (respMsg != null && !(MessageKind.EMPTY.equals(respMsg.getMessageKind()))) {
                responseMessage = respMsg;
            }
        }
    }
    if (requestMessage == null) {
        throw new GrpcServerException("Cannot find request message definition for streaming service: " + serviceNode.getName().getValue());
    }
    if (responseMessage == null) {
        responseMessage = generateMessageDefinition(new BNullType());
    /*throw new GrpcServerException("Cannot find response message definition for streaming service: " +
                    serviceNode.getName().getValue());*/
    }
    if (requestMessage.getMessageKind() == MessageKind.USER_DEFINED) {
        if (!fileBuilder.getRegisteredMessages().contains(requestMessage.getDescriptorProto())) {
            fileBuilder.setMessage(requestMessage);
        }
    }
    if (requestMessage.getDependency() != null) {
        if (!fileBuilder.getRegisteredDependencies().contains(requestMessage.getDependency())) {
            fileBuilder.setDependeny(requestMessage.getDependency());
        }
    }
    if (responseMessage.getDependency() != null) {
        if (!fileBuilder.getRegisteredDependencies().contains(responseMessage.getDependency())) {
            fileBuilder.setDependeny(responseMessage.getDependency());
        }
    }
    Method resourceMethod = Method.newBuilder(serviceConfig.getRpcEndpoint()).setClientStreaming(serviceConfig.isClientStreaming()).setServerStreaming(serviceConfig.isServerStreaming()).setInputType(requestMessage.getMessageType()).setOutputType(responseMessage.getMessageType()).build();
    serviceBuilder.addMethod(resourceMethod);
    return serviceBuilder.build();
}
Also used : Message(org.ballerinalang.net.grpc.proto.definition.Message) EmptyMessage(org.ballerinalang.net.grpc.proto.definition.EmptyMessage) WrapperMessage(org.ballerinalang.net.grpc.proto.definition.WrapperMessage) UserDefinedMessage(org.ballerinalang.net.grpc.proto.definition.UserDefinedMessage) Service(org.ballerinalang.net.grpc.proto.definition.Service) Method(org.ballerinalang.net.grpc.proto.definition.Method) BNullType(org.wso2.ballerinalang.compiler.semantics.model.types.BNullType) ResourceNode(org.ballerinalang.model.tree.ResourceNode) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 4 with ResourceNode

use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getUnaryServiceDefinition.

private static Service getUnaryServiceDefinition(ServiceNode serviceNode, File.Builder fileBuilder) throws GrpcServerException {
    // Protobuf service definition builder.
    Service.Builder serviceBuilder = Service.newBuilder(serviceNode.getName().getValue());
    for (ResourceNode resourceNode : serviceNode.getResources()) {
        Message requestMessage = getRequestMessage(resourceNode);
        if (requestMessage.getMessageKind() == MessageKind.USER_DEFINED) {
            if (!fileBuilder.getRegisteredMessages().contains(requestMessage.getDescriptorProto())) {
                fileBuilder.setMessage(requestMessage);
            }
        }
        if (requestMessage.getDependency() != null) {
            if (!fileBuilder.getRegisteredDependencies().contains(requestMessage.getDependency())) {
                fileBuilder.setDependeny(requestMessage.getDependency());
            }
        }
        Message responseMessage = getResponseMessage(resourceNode);
        if (responseMessage == null) {
            throw new GrpcServerException("Connection send expression not found in resource body");
        }
        if (responseMessage.getMessageKind() == MessageKind.USER_DEFINED) {
            if (!fileBuilder.getRegisteredMessages().contains(responseMessage.getDescriptorProto())) {
                fileBuilder.setMessage(responseMessage);
            }
        }
        if (responseMessage.getDependency() != null) {
            if (!fileBuilder.getRegisteredDependencies().contains(responseMessage.getDependency())) {
                fileBuilder.setDependeny(responseMessage.getDependency());
            }
        }
        boolean serverStreaming = isServerStreaming(resourceNode);
        Method resourceMethod = Method.newBuilder(resourceNode.getName().getValue()).setClientStreaming(false).setServerStreaming(serverStreaming).setInputType(requestMessage.getMessageType()).setOutputType(responseMessage.getMessageType()).build();
        serviceBuilder.addMethod(resourceMethod);
    }
    return serviceBuilder.build();
}
Also used : Message(org.ballerinalang.net.grpc.proto.definition.Message) EmptyMessage(org.ballerinalang.net.grpc.proto.definition.EmptyMessage) WrapperMessage(org.ballerinalang.net.grpc.proto.definition.WrapperMessage) UserDefinedMessage(org.ballerinalang.net.grpc.proto.definition.UserDefinedMessage) Service(org.ballerinalang.net.grpc.proto.definition.Service) Method(org.ballerinalang.net.grpc.proto.definition.Method) ResourceNode(org.ballerinalang.model.tree.ResourceNode) GrpcServerException(org.ballerinalang.net.grpc.exception.GrpcServerException)

Example 5 with ResourceNode

use of org.ballerinalang.model.tree.ResourceNode in project ballerina by ballerina-lang.

the class BLangPackageBuilder method startResourceDef.

public void startResourceDef() {
    ResourceNode resourceNode = TreeBuilder.createResourceNode();
    invokableNodeStack.push(resourceNode);
    startEndpointDeclarationScope(((BLangResource) resourceNode).endpoints);
}
Also used : ResourceNode(org.ballerinalang.model.tree.ResourceNode)

Aggregations

ResourceNode (org.ballerinalang.model.tree.ResourceNode)9 Lists (com.google.common.collect.Lists)5 ExternalDocs (io.swagger.models.ExternalDocs)5 Model (io.swagger.models.Model)5 ModelImpl (io.swagger.models.ModelImpl)5 Operation (io.swagger.models.Operation)5 Path (io.swagger.models.Path)5 RefModel (io.swagger.models.RefModel)5 Response (io.swagger.models.Response)5 Scheme (io.swagger.models.Scheme)5 Swagger (io.swagger.models.Swagger)5 BodyParameter (io.swagger.models.parameters.BodyParameter)5 Parameter (io.swagger.models.parameters.Parameter)5 PathParameter (io.swagger.models.parameters.PathParameter)5 ArrayProperty (io.swagger.models.properties.ArrayProperty)5 BooleanProperty (io.swagger.models.properties.BooleanProperty)5 IntegerProperty (io.swagger.models.properties.IntegerProperty)5 Property (io.swagger.models.properties.Property)5 StringProperty (io.swagger.models.properties.StringProperty)5 ArrayList (java.util.ArrayList)5