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;
}
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);
}
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;
}
Aggregations