Search in sources :

Example 1 with APIPolicyConditionGroup

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method setCommonProperties.

private void setCommonProperties(APIPolicyConditionGroup apiPolicyConditionGroup, ResultSet resultSet) throws SQLException {
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
    if (quotaPolicy.getType() != null) {
        if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            RequestCountLimit reqLimit = new RequestCountLimit();
            reqLimit.setUnitTime(resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            reqLimit.setTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            reqLimit.setRequestCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA));
            quotaPolicy.setLimit(reqLimit);
        } else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            BandwidthLimit bandLimit = new BandwidthLimit();
            bandLimit.setUnitTime(resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            bandLimit.setTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            bandLimit.setDataAmount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA));
            bandLimit.setDataUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
            quotaPolicy.setLimit(bandLimit);
        }
        apiPolicyConditionGroup.setQuotaPolicy(quotaPolicy);
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 2 with APIPolicyConditionGroup

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup in project carbon-apimgt by wso2.

the class SubscriptionValidationDataUtil method fromApiPolicyToApiPolicyListDTO.

public static ApiPolicyListDTO fromApiPolicyToApiPolicyListDTO(List<APIPolicy> model) {
    ApiPolicyListDTO apiPolicyListDTO = new ApiPolicyListDTO();
    if (model != null) {
        for (APIPolicy apiPolicyModel : model) {
            ApiPolicyDTO policyDTO = new ApiPolicyDTO();
            policyDTO.setName(apiPolicyModel.getName());
            policyDTO.setQuotaType(apiPolicyModel.getQuotaType());
            policyDTO.setTenantId(apiPolicyModel.getTenantId());
            policyDTO.setTenantDomain(apiPolicyModel.getTenantDomain());
            policyDTO.setApplicableLevel(apiPolicyModel.getApplicableLevel());
            policyDTO.setDefaultLimit(getThrottleLimitDTO(apiPolicyModel));
            apiPolicyListDTO.getList().add(policyDTO);
            List<APIPolicyConditionGroup> retrievedGroups = apiPolicyModel.getConditionGroups();
            List<ApiPolicyConditionGroupDTO> condGroups = new ArrayList<ApiPolicyConditionGroupDTO>();
            for (APIPolicyConditionGroup retGroup : retrievedGroups) {
                ApiPolicyConditionGroupDTO group = new ApiPolicyConditionGroupDTO();
                group.setConditionGroupId(retGroup.getConditionGroupId());
                group.setQuotaType(retGroup.getQuotaType());
                group.setDefaultLimit(getThrottleLimitDTO(retGroup));
                group.setPolicyId(retGroup.getPolicyId());
                List<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO> condition = new ArrayList<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO>();
                List<ConditionDTO> retrievedConditions = retGroup.getConditionDTOS();
                for (ConditionDTO retrievedCondition : retrievedConditions) {
                    org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO conditionDTO = new org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO();
                    conditionDTO.setConditionType(retrievedCondition.getConditionType());
                    conditionDTO.setIsInverted(retrievedCondition.isInverted());
                    conditionDTO.setName(retrievedCondition.getConditionName());
                    conditionDTO.setValue(retrievedCondition.getConditionValue());
                    condition.add(conditionDTO);
                }
                group.setCondition(condition);
                condGroups.add(group);
            }
            policyDTO.setConditionGroups(condGroups);
        }
        apiPolicyListDTO.setCount(model.size());
    } else {
        apiPolicyListDTO.setCount(0);
    }
    return apiPolicyListDTO;
}
Also used : APIPolicyConditionGroup(org.wso2.carbon.apimgt.api.model.subscription.APIPolicyConditionGroup) ArrayList(java.util.ArrayList) ApiPolicyConditionGroupDTO(org.wso2.carbon.apimgt.internal.service.dto.ApiPolicyConditionGroupDTO) ApiPolicyDTO(org.wso2.carbon.apimgt.internal.service.dto.ApiPolicyDTO) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) APIPolicy(org.wso2.carbon.apimgt.api.model.subscription.APIPolicy) ApiPolicyListDTO(org.wso2.carbon.apimgt.internal.service.dto.ApiPolicyListDTO)

Example 3 with APIPolicyConditionGroup

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevelDefault.

/**
 * Generate default policy for API level throttling
 *
 * @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
 *               used as else condition
 * @return the generated execution plan for the policy
 * @throws APITemplateException if failed to generate policy
 */
public String getThrottlePolicyForAPILevelDefault(ApiPolicy policy) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating default policy for API Level :" + policy.toString());
    }
    Set<String> conditionsSet = new HashSet<>();
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        velocityengine.init();
        Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
        StringWriter writer;
        VelocityContext context;
        List<APIPolicyConditionGroup> conditionGroups = policy.getConditionGroups();
        JSONArray policyConditionJson = new JSONArray();
        if (conditionGroups != null) {
            for (APIPolicyConditionGroup conditionGroup : conditionGroups) {
                if (conditionGroup.getDefaultLimit() == null) {
                    continue;
                }
                policyConditionJson.add(getPolicyConditionJson(conditionGroup.getCondition()));
                String conditionString = getPolicyConditionForDefault(conditionGroup.getCondition());
                if (!StringUtils.isEmpty(conditionString)) {
                    conditionsSet.add(conditionString);
                }
            }
        }
        context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        context.put("quotaPolicy", policy.getDefaultLimit());
        context.put("evaluatedConditions", new String(Base64.encodeBase64(policyConditionJson.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
        String conditionSetString = getConditionForDefault(conditionsSet);
        if (!StringUtils.isEmpty(conditionSetString)) {
            context.put("condition", " AND " + conditionSetString);
        } else {
            context.put("condition", "");
        }
        writer = new StringWriter();
        template.merge(context, writer);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
        return writer.toString();
    } catch (VelocityException e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) APIPolicyConditionGroup(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) JSONArray(org.json.simple.JSONArray) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) HashSet(java.util.HashSet) Template(org.apache.velocity.Template)

Example 4 with APIPolicyConditionGroup

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevel.

/**
 * Generate policy for API level throttling
 *
 * @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
 *               used as else condition
 * @return a Map containing a set of policies for each condition group
 * @throws APITemplateException if failed to generate policy
 */
public Map<String, String> getThrottlePolicyForAPILevel(ApiPolicy policy) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating policy for API Level :" + policy.toString());
    }
    Map<String, String> policyArray = new HashMap<>();
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        velocityengine.init();
        Template template = velocityengine.getTemplate(getTemplatePathForAPI());
        StringWriter writer;
        VelocityContext context;
        if (policy.getConditionGroups() != null) {
            for (APIPolicyConditionGroup conditionGroup : policy.getConditionGroups()) {
                if (conditionGroup.getDefaultLimit() == null) {
                    continue;
                }
                context = new VelocityContext();
                setConstantContext(context);
                context.put("policy", policy);
                context.put("quotaPolicy", conditionGroup.getDefaultLimit());
                context.put("pipeline", "condition_" + conditionGroup.getConditionGroupId());
                String conditionString = getPolicyCondition(conditionGroup.getCondition());
                JSONArray conditions = new JSONArray();
                conditions.add(getPolicyConditionJson(conditionGroup.getCondition()));
                context.put("condition", " AND " + conditionString);
                context.put("evaluatedConditions", new String(Base64.encodeBase64(conditions.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
                writer = new StringWriter();
                template.merge(context, writer);
                if (log.isDebugEnabled()) {
                    log.debug("Policy : " + writer.toString());
                }
                String policyName = policy.getTenantDomain() + APIConstants.DELEM_UNDERSCORE + PolicyConstants.POLICY_LEVEL_RESOURCE + APIConstants.DELEM_UNDERSCORE + policy.getName() + APIConstants.THROTTLE_POLICY_CONDITION + conditionGroup.getConditionGroupId();
                policyArray.put(policyName, writer.toString());
            }
        }
    } catch (VelocityException e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
    return policyArray;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) APIPolicyConditionGroup(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) JSONArray(org.json.simple.JSONArray) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 5 with APIPolicyConditionGroup

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method populateApiPolicyList.

private List<APIPolicy> populateApiPolicyList(ResultSet resultSet) throws SQLException {
    List<APIPolicy> apiPolicies = new ArrayList<>();
    Map<Integer, APIPolicy> temp = new ConcurrentHashMap<>();
    if (resultSet != null) {
        while (resultSet.next()) {
            int policyId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID);
            APIPolicy apiPolicy = temp.get(policyId);
            if (apiPolicy == null) {
                apiPolicy = new APIPolicy();
                apiPolicy.setId(policyId);
                apiPolicy.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
                apiPolicy.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_DEFAULT_QUOTA_POLICY_TYPE));
                apiPolicy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
                String tenantDomain = APIUtil.getTenantDomainFromTenantId(apiPolicy.getTenantId());
                apiPolicy.setTenantDomain(tenantDomain);
                apiPolicy.setApplicableLevel(resultSet.getString(ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                setCommonProperties(apiPolicy, resultSet);
                apiPolicies.add(apiPolicy);
            }
            APIPolicyConditionGroup apiPolicyConditionGroup = new APIPolicyConditionGroup();
            int conditionGroup = resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONDITION_ID);
            apiPolicyConditionGroup.setConditionGroupId(conditionGroup);
            apiPolicyConditionGroup.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
            apiPolicyConditionGroup.setPolicyId(policyId);
            ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
            ConditionGroupDTO conditionGroupDTO = null;
            try {
                conditionGroupDTO = apiMgtDAO.createConditionGroupDTO(conditionGroup);
            } catch (APIManagementException e) {
                log.error("Error while processing api policies for policyId : " + policyId, e);
            }
            ConditionDTO[] conditionDTOS = conditionGroupDTO.getConditions();
            apiPolicyConditionGroup.setConditionDTOS(Arrays.asList(conditionDTOS));
            setCommonProperties(apiPolicyConditionGroup, resultSet);
            apiPolicy.addConditionGroup(apiPolicyConditionGroup);
            temp.put(policyId, apiPolicy);
        }
    }
    return apiPolicies;
}
Also used : APIPolicyConditionGroup(org.wso2.carbon.apimgt.api.model.subscription.APIPolicyConditionGroup) ArrayList(java.util.ArrayList) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) APIPolicy(org.wso2.carbon.apimgt.api.model.subscription.APIPolicy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ArrayList (java.util.ArrayList)4 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)3 APIPolicyConditionGroup (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup)3 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 Template (org.apache.velocity.Template)2 VelocityContext (org.apache.velocity.VelocityContext)2 VelocityEngine (org.apache.velocity.app.VelocityEngine)2 VelocityException (org.apache.velocity.exception.VelocityException)2 JSONArray (org.json.simple.JSONArray)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)2 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)2 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)2 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)2 APIPolicy (org.wso2.carbon.apimgt.api.model.subscription.APIPolicy)2 APIPolicyConditionGroup (org.wso2.carbon.apimgt.api.model.subscription.APIPolicyConditionGroup)2 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)2 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1