Search in sources :

Example 1 with ServiceNode

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

the class SwaggerConverterUtils method generateSwaggerDefinitions.

/**
 * This method will generate ballerina string from swagger definition. Since ballerina service definition is super
 * set of swagger definition we will take both swagger and ballerina definition and merge swagger changes to
 * ballerina definition selectively to prevent data loss
 *
 * @param ballerinaSource ballerina definition to be process as ballerina definition
 * @param serviceName service name
 * @return String representation of converted ballerina source
 * @throws IOException when error occur while processing input swagger and ballerina definitions.
 */
public static String generateSwaggerDefinitions(String ballerinaSource, String serviceName) throws IOException {
    // Get the ballerina model using the ballerina source code.
    BFile balFile = new BFile();
    balFile.setContent(ballerinaSource);
    BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
    String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
    String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
    SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
    String swaggerSource = StringUtils.EMPTY;
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangService) {
            ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
            // Generate swagger string for the mentioned service name.
            if (StringUtils.isNotBlank(serviceName)) {
                if (serviceDefinition.getName().getValue().equals(serviceName)) {
                    Swagger swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                    swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
                    break;
                }
            } else {
                // If no service name mentioned, then generate swagger definition for the first service.
                Swagger swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
                break;
            }
        }
    }
    return swaggerSource;
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) Swagger(io.swagger.models.Swagger) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 2 with ServiceNode

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

the class SwaggerConverterUtils method generateOAS3Definitions.

/**
 * This method will generate open API 3.X specification for given ballerina service. Since we will need to
 * support both swagger 2.0 and OAS 3.0 it was implemented to convert to swagger by default and convert it
 * to OAS on demand.
 *
 * @param ballerinaSource ballerina source to be converted to swagger/OAS definition
 * @param serviceName specific service name within ballerina source that need to map OAS
 * @return Generated OAS3 string output.
 * @throws IOException When error occurs while converting, parsing input source.
 */
public static String generateOAS3Definitions(String ballerinaSource, String serviceName) throws IOException {
    // Get the ballerina model using the ballerina source code.
    BFile balFile = new BFile();
    balFile.setContent(ballerinaSource);
    // Create empty swagger object.
    Swagger swaggerDefinition = new Swagger();
    BLangCompilationUnit topCompilationUnit = SwaggerConverterUtils.getTopLevelNodeFromBallerinaFile(balFile);
    String httpAlias = getAlias(topCompilationUnit, Constants.BALLERINA_HTTP_PACKAGE_NAME);
    String swaggerAlias = getAlias(topCompilationUnit, Constants.SWAGGER_PACKAGE_NAME);
    SwaggerServiceMapper swaggerServiceMapper = new SwaggerServiceMapper(httpAlias, swaggerAlias);
    String swaggerSource = StringUtils.EMPTY;
    for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
        if (topLevelNode instanceof BLangService) {
            ServiceNode serviceDefinition = (ServiceNode) topLevelNode;
            // Generate swagger string for the mentioned service name.
            if (StringUtils.isNotBlank(serviceName)) {
                if (serviceDefinition.getName().getValue().equals(serviceName)) {
                    swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                    break;
                }
            } else {
                // If no service name mentioned, then generate swagger definition for the first service.
                swaggerDefinition = swaggerServiceMapper.convertServiceToSwagger(serviceDefinition);
                break;
            }
        }
    }
    swaggerSource = swaggerServiceMapper.generateSwaggerString(swaggerDefinition);
    SwaggerConverter converter = new SwaggerConverter();
    return Yaml.pretty(converter.readContents(swaggerSource, null, null).getOpenAPI());
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) Swagger(io.swagger.models.Swagger) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) SwaggerConverter(io.swagger.v3.parser.converter.SwaggerConverter) BFile(org.ballerinalang.composer.service.ballerina.parser.service.model.BFile) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 3 with ServiceNode

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

the class BLangPackageBuilder method addServiceBody.

public void addServiceBody(Set<Whitespace> ws) {
    ServiceNode serviceNode = serviceNodeStack.peek();
    serviceNode.addWS(ws);
    blockNodeStack.pop().getStatements().forEach(varDef -> serviceNode.addVariable((VariableDefinitionNode) varDef));
}
Also used : VariableDefinitionNode(org.ballerinalang.model.tree.statements.VariableDefinitionNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode)

Example 4 with ServiceNode

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

the class BLangPackageBuilder method addServiceEndpointAttachments.

public void addServiceEndpointAttachments(int size, Set<Whitespace> ws) {
    ServiceNode serviceNode = serviceNodeStack.peek();
    serviceNode.addWS(ws);
    for (int i = 0; i < size; i++) {
        BLangNameReference nameReference = nameReferenceStack.pop();
        BLangSimpleVarRef varRef = (BLangSimpleVarRef) TreeBuilder.createSimpleVariableReferenceNode();
        varRef.pos = nameReference.pos;
        varRef.addWS(nameReference.ws);
        varRef.pkgAlias = (BLangIdentifier) nameReference.pkgAlias;
        varRef.variableName = (BLangIdentifier) nameReference.name;
        serviceNode.bindToEndpoint(varRef);
    }
}
Also used : ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BLangNameReference(org.wso2.ballerinalang.compiler.tree.BLangNameReference) BLangAnnotationAttachmentPoint(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 5 with ServiceNode

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

the class SwaggerServiceMapper method parseServiceInfoAnnotationAttachment.

/**
 * Parses the 'ServiceInfo' annotation and build the swagger definition for it.
 * @param service The ballerina service which has the 'ServiceInfo' annotation attachment.
 * @param swagger The swagger definition to be built up.
 */
private void parseServiceInfoAnnotationAttachment(ServiceNode service, Swagger swagger) {
    Optional<? extends AnnotationAttachmentNode> swaggerInfoAnnotation = service.getAnnotationAttachments().stream().filter(a -> null != swaggerAlias && this.swaggerAlias.equals(a.getPackageAlias().getValue()) && "ServiceInfo".equals(a.getAnnotationName().getValue())).findFirst();
    Info info = new Info().version("1.0.0").title(service.getName().getValue());
    if (swaggerInfoAnnotation.isPresent()) {
        Map<String, AnnotationAttachmentAttributeValueNode> attributes = this.listToMap(swaggerInfoAnnotation.get());
        if (attributes.containsKey("serviceVersion")) {
            info.version(this.getStringLiteralValue(attributes.get("serviceVersion")));
        }
        if (attributes.containsKey("title")) {
            info.title(this.getStringLiteralValue(attributes.get("title")));
        }
        if (attributes.containsKey("description")) {
            info.description(this.getStringLiteralValue(attributes.get("description")));
        }
        if (attributes.containsKey("termsOfService")) {
            info.termsOfService(this.getStringLiteralValue(attributes.get("termsOfService")));
        }
        this.createContactModel(attributes.get("contact"), info);
        this.createLicenseModel(attributes.get("license"), info);
        this.createExternalDocModel(attributes.get("externalDoc"), swagger);
        this.createTagModel(attributes.get("tags"), swagger);
        this.createOrganizationModel(attributes.get("organization"), info);
        this.createDevelopersModel(attributes.get("developers"), info);
    }
    swagger.setInfo(info);
}
Also used : Scheme(io.swagger.models.Scheme) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) Tag(io.swagger.models.Tag) License(io.swagger.models.License) Json(io.swagger.util.Json) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Lists(com.google.common.collect.Lists) Map(java.util.Map) Organization(org.ballerinalang.ballerina.swagger.convertor.service.model.Organization) LinkedList(java.util.LinkedList) Contact(io.swagger.models.Contact) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) Info(io.swagger.models.Info) Developer(org.ballerinalang.ballerina.swagger.convertor.service.model.Developer) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) List(java.util.List) AnnotationAttachmentAttributeNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeNode) ExternalDocs(io.swagger.models.ExternalDocs) LiteralNode(org.ballerinalang.model.tree.expressions.LiteralNode) Optional(java.util.Optional) In(io.swagger.models.auth.In) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) Info(io.swagger.models.Info)

Aggregations

ServiceNode (org.ballerinalang.model.tree.ServiceNode)7 Swagger (io.swagger.models.Swagger)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Lists (com.google.common.collect.Lists)3 Contact (io.swagger.models.Contact)3 ExternalDocs (io.swagger.models.ExternalDocs)3 Info (io.swagger.models.Info)3 License (io.swagger.models.License)3 Scheme (io.swagger.models.Scheme)3 Tag (io.swagger.models.Tag)3 ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)3 BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)3 In (io.swagger.models.auth.In)3 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)3 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)3 Json (io.swagger.util.Json)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3