Search in sources :

Example 1 with ThrottlePolicyMapper

use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper in project product-microgateway by wso2.

the class ThrottlePolicyGenerator method generateGenericPolicies.

/**
 * Generate generic policies source.
 *
 * @param policies list of application policies
 * @return list of {@code GenSrcFile}
 * @throws IOException when file operations fail
 */
private List<GenSrcFile> generateGenericPolicies(List<ThrottlePolicyMapper> policies, GeneratorConstants.PolicyType type) throws IOException {
    ThrottlePolicy policyContext;
    if (policies == null) {
        return null;
    }
    List<GenSrcFile> sourceFiles = new ArrayList<>();
    for (ThrottlePolicyMapper policy : policies) {
        policyContext = new ThrottlePolicy().buildContext(policy, type);
        sourceFiles.add(generatePolicy(policyContext));
    }
    return sourceFiles;
}
Also used : GenSrcFile(org.wso2.apimgt.gateway.cli.model.template.GenSrcFile) ThrottlePolicyMapper(org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper) ArrayList(java.util.ArrayList) ThrottlePolicy(org.wso2.apimgt.gateway.cli.model.template.policy.ThrottlePolicy)

Example 2 with ThrottlePolicyMapper

use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper in project product-microgateway by wso2.

the class ThrottlePolicyGenerator method generate.

/**
 * Generate ballerina and stream source for a given app and subs policies.
 *
 * @param outPath     Destination file path to save generated source files. If not provided
 *                    {@code definitionPath} will be used as the default destination path
 * @param projectName Project name
 * @throws IOException when file operations fail
 */
public void generate(String outPath, String projectName) throws IOException {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    String policyFileLocation = CmdUtils.getProjectDirectoryPath(projectName) + File.separator + CliConstants.GW_DIST_POLICIES_FILE;
    ThrottlePolicyListMapper throttlePolicyListMapper = mapper.readValue(new File(policyFileLocation), ThrottlePolicyListMapper.class);
    // read application throttle policies and subscription throttle policies
    List<ThrottlePolicyMapper> applicationPolicies = throttlePolicyListMapper.getApplicationPolicies();
    List<ThrottlePolicyMapper> subscriptionPolicies = throttlePolicyListMapper.getSubscriptionPolicies();
    List<ThrottlePolicyMapper> resourcePolicies = throttlePolicyListMapper.getResourcePolicies();
    if (applicationPolicies == null && subscriptionPolicies == null && resourcePolicies == null) {
        return;
    }
    checkDuplicatePolicyNames(applicationPolicies, subscriptionPolicies, resourcePolicies);
    List<GenSrcFile> genFiles = new ArrayList<>();
    GenSrcFile initGenFile = generateInitBal(applicationPolicies, subscriptionPolicies, resourcePolicies);
    genFiles.add(initGenFile);
    CodegenUtils.writeGeneratedSources(genFiles, Paths.get(outPath), true);
}
Also used : GenSrcFile(org.wso2.apimgt.gateway.cli.model.template.GenSrcFile) ThrottlePolicyListMapper(org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyListMapper) ThrottlePolicyMapper(org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper) ArrayList(java.util.ArrayList) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) GenSrcFile(org.wso2.apimgt.gateway.cli.model.template.GenSrcFile) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ThrottlePolicyMapper

use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper in project product-microgateway by wso2.

the class ThrottlePolicyInitializer method buildPolicyContext.

public ThrottlePolicyInitializer buildPolicyContext(List<ThrottlePolicyMapper> policies, GeneratorConstants.PolicyType type) {
    for (ThrottlePolicyMapper policyDTO : policies) {
        String escapedPolicyName = CodegenUtils.trim(policyDTO.getName());
        String policyName = policyDTO.getName();
        switch(type) {
            case RESOURCE:
                policyInitNames.add(GeneratorConstants.RESOURCE_INIT_FUNC_PREFIX + escapedPolicyName + GeneratorConstants.INIT_FUNC_SUFFIX);
                policyDTO.setName(GeneratorConstants.RESOURCE_LEVEL_PREFIX + policyName);
                break;
            case APPLICATION:
                policyInitNames.add(GeneratorConstants.APPLICATION_INIT_FUNC_PREFIX + escapedPolicyName + GeneratorConstants.INIT_FUNC_SUFFIX);
                policyDTO.setName(GeneratorConstants.APP_LEVEL_PREFIX + policyName);
                break;
            case SUBSCRIPTION:
                policyInitNames.add(GeneratorConstants.SUBSCRIPTION_INIT_FUNC_PREFIX + escapedPolicyName + GeneratorConstants.INIT_FUNC_SUFFIX);
                policyDTO.setName(GeneratorConstants.SUB_LEVEL_PREFIX + policyName);
                break;
        }
        policyNames.add(escapedPolicyName);
        policyList.add(policyDTO);
    }
    return this;
}
Also used : ThrottlePolicyMapper(org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper)

Aggregations

ThrottlePolicyMapper (org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyMapper)3 ArrayList (java.util.ArrayList)2 GenSrcFile (org.wso2.apimgt.gateway.cli.model.template.GenSrcFile)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 File (java.io.File)1 ThrottlePolicyListMapper (org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottlePolicyListMapper)1 ThrottlePolicy (org.wso2.apimgt.gateway.cli.model.template.policy.ThrottlePolicy)1