Search in sources :

Example 6 with APIPolicyConditionGroup

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

the class SubscriptionValidationDataUtil method getThrottleLimitDTO.

/**
 * Converts a quota policy object of a condition group into a Throttle Limit DTO object.
 *
 * @param apiPolicyConditionGroup condition group model object
 * @return Throttle Limit DTO
 */
private static ThrottleLimitDTO getThrottleLimitDTO(APIPolicyConditionGroup apiPolicyConditionGroup) {
    QuotaPolicy quotaPolicy = apiPolicyConditionGroup.getQuotaPolicy();
    if (quotaPolicy != null) {
        ThrottleLimitDTO defaultLimit = new ThrottleLimitDTO();
        defaultLimit.setQuotaType(quotaPolicy.getType());
        if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
            RequestCountLimit requestCountLimit = (RequestCountLimit) quotaPolicy.getLimit();
            defaultLimit.setRequestCount(fromRequestCountLimitToDTO(requestCountLimit));
        } else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
            BandwidthLimit bandwidthLimit = (BandwidthLimit) quotaPolicy.getLimit();
            defaultLimit.setBandwidth(fromBandwidthLimitToDTO(bandwidthLimit));
        } else if (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
            EventCountLimit eventCountLimit = (EventCountLimit) quotaPolicy.getLimit();
            defaultLimit.setEventCount(fromEventCountLimitToDTO(eventCountLimit));
        }
        return defaultLimit;
    }
    return null;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) ThrottleLimitDTO(org.wso2.carbon.apimgt.internal.service.dto.ThrottleLimitDTO) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 7 with APIPolicyConditionGroup

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

the class TestUtil method getPolicyAPILevel.

/**
 * Generate a dummy API Policy object
 *
 * @return an API Policy
 */
public static ApiPolicy getPolicyAPILevel() {
    ApiPolicy apiPolicy = new ApiPolicy();
    apiPolicy.setId(1);
    apiPolicy.setTenantId(-1234);
    apiPolicy.setTenantDomain("carbon.super");
    apiPolicy.setName("policy1");
    apiPolicy.setQuotaType("requestCount");
    QuotaPolicy defaultLimit = new QuotaPolicy();
    defaultLimit.setQuotaType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit();
    requestCountLimit.setRequestCount(1);
    requestCountLimit.setTimeUnit("min");
    requestCountLimit.setUnitTime(1);
    defaultLimit.setRequestCount(requestCountLimit);
    apiPolicy.setDefaultLimit(defaultLimit);
    apiPolicy.setApplicableLevel("apiLevel");
    List<APIPolicyConditionGroup> conditionGroups = new ArrayList<>();
    APIPolicyConditionGroup conditionGroup1 = new APIPolicyConditionGroup();
    conditionGroup1.setPolicyId(1);
    conditionGroup1.setQuotaType("requestCount");
    conditionGroup1.setConditionGroupId(1);
    Set<Condition> conditions = new HashSet<>();
    Condition condition1 = new Condition();
    condition1.setConditionType("IPSpecific");
    condition1.setName("IPSpecific");
    condition1.setValue("127.0.0.1");
    condition1.setInverted(false);
    conditions.add(condition1);
    conditionGroup1.setCondition(conditions);
    QuotaPolicy defaultLimitCondGroup1 = new QuotaPolicy();
    defaultLimitCondGroup1.setQuotaType("requestCount");
    RequestCountLimit requestCountLimitCondGroup1 = new RequestCountLimit();
    requestCountLimitCondGroup1.setRequestCount(1);
    requestCountLimitCondGroup1.setTimeUnit("min");
    requestCountLimitCondGroup1.setUnitTime(1);
    defaultLimitCondGroup1.setRequestCount(requestCountLimitCondGroup1);
    conditionGroup1.setDefaultLimit(defaultLimitCondGroup1);
    conditionGroups.add(conditionGroup1);
    apiPolicy.setConditionGroups(conditionGroups);
    return apiPolicy;
}
Also used : Condition(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition) RequestCountLimit(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit) APIPolicyConditionGroup(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ArrayList(java.util.ArrayList) QuotaPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy) HashSet(java.util.HashSet)

Example 8 with APIPolicyConditionGroup

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

the class APIKeyValidationService method getTemplates.

private ArrayList<URITemplate> getTemplates(String context, String version) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(context);
    if (tenantDomain == null) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    ArrayList<URITemplate> templates = new ArrayList<URITemplate>();
    SubscriptionDataStore store = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    if (store == null) {
        return templates;
    }
    API api = store.getApiByContextAndVersion(context, version);
    if (api == null) {
        log.debug("SubscriptionDataStore didn't contains API metadata reading from rest api context: " + context + " And version " + version);
        api = new SubscriptionDataLoaderImpl().getApi(context, version);
        if (api != null) {
            store.addOrUpdateAPI(api);
            if (log.isDebugEnabled()) {
                log.debug("Update SubscriptionDataStore api for " + api.getCacheKey());
            }
        }
    }
    if (api == null || api.getApiId() == 0) {
        return templates;
    }
    List<URLMapping> mapping = api.getResources();
    if (mapping == null || mapping.isEmpty()) {
        return templates;
    }
    int apiTenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(api.getApiProvider()));
    if (log.isDebugEnabled()) {
        log.debug("Tenant domain: " + tenantDomain + " tenantId: " + apiTenantId);
    }
    ApiPolicy apiPolicy;
    URITemplate template;
    for (URLMapping urlMapping : mapping) {
        template = new URITemplate();
        template.setHTTPVerb(urlMapping.getHttpMethod());
        template.setAuthType(urlMapping.getAuthScheme());
        template.setUriTemplate(urlMapping.getUrlPattern());
        template.setThrottlingTier(urlMapping.getThrottlingPolicy());
        if (store.isApiPoliciesInitialized()) {
            log.debug("SubscriptionDataStore Initialized. Reading API Policies from SubscriptionDataStore");
            apiPolicy = store.getApiPolicyByName(urlMapping.getThrottlingPolicy(), apiTenantId);
            if (apiPolicy == null) {
                // could be null for situations where invoke before map is updated
                log.debug("API Policies not found in the SubscriptionDataStore. Retrieving from the Rest API");
                apiPolicy = new SubscriptionDataLoaderImpl().getAPIPolicy(urlMapping.getThrottlingPolicy(), tenantDomain);
                if (apiPolicy != null) {
                    if (apiPolicy.getName() != null) {
                        store.addOrUpdateApiPolicy(apiPolicy);
                        if (log.isDebugEnabled()) {
                            log.debug("Update SubscriptionDataStore API Policy for " + apiPolicy.getCacheKey());
                        }
                    } else {
                        throw new APIManagementException("Exception while loading api policy for " + urlMapping.getThrottlingPolicy() + " for domain " + tenantDomain);
                    }
                }
            }
        } else {
            log.debug("SubscriptionDataStore not Initialized. Reading API Policies from Rest API");
            apiPolicy = new SubscriptionDataLoaderImpl().getAPIPolicy(urlMapping.getThrottlingPolicy(), tenantDomain);
            if (apiPolicy != null) {
                if (apiPolicy.getName() != null) {
                    store.addOrUpdateApiPolicy(apiPolicy);
                    if (log.isDebugEnabled()) {
                        log.debug("Update SubscriptionDataStore API Policy for " + apiPolicy.getCacheKey());
                    }
                } else {
                    throw new APIManagementException("Exception while loading api policy for " + urlMapping.getThrottlingPolicy() + " for domain " + tenantDomain);
                }
            }
        }
        List<String> tiers = new ArrayList<String>();
        tiers.add(urlMapping.getThrottlingPolicy() + ">" + apiPolicy.isContentAware());
        template.setThrottlingTiers(tiers);
        template.setApplicableLevel(apiPolicy.getApplicableLevel());
        List<APIPolicyConditionGroup> conditions = apiPolicy.getConditionGroups();
        List<ConditionGroupDTO> conditionGroupsList = new ArrayList<ConditionGroupDTO>();
        for (APIPolicyConditionGroup cond : conditions) {
            Set<Condition> condSet = cond.getCondition();
            if (condSet.isEmpty()) {
                continue;
            }
            List<ConditionDTO> conditionDtoList = new ArrayList<ConditionDTO>();
            for (Condition condition : condSet) {
                ConditionDTO item = new ConditionDTO();
                item.setConditionName(condition.getName());
                item.setConditionType(condition.getConditionType());
                item.setConditionValue(condition.getValue());
                item.isInverted(condition.isInverted());
                conditionDtoList.add(item);
            }
            ConditionGroupDTO group = new ConditionGroupDTO();
            group.setConditionGroupId("_condition_" + cond.getConditionGroupId());
            group.setConditions(conditionDtoList.toArray(new ConditionDTO[] {}));
            conditionGroupsList.add(group);
        }
        ConditionGroupDTO defaultGroup = new ConditionGroupDTO();
        defaultGroup.setConditionGroupId(APIConstants.THROTTLE_POLICY_DEFAULT);
        conditionGroupsList.add(defaultGroup);
        template.getThrottlingConditions().add(APIConstants.THROTTLE_POLICY_DEFAULT);
        template.setConditionGroups(conditionGroupsList.toArray(new ConditionGroupDTO[] {}));
        templates.add(template);
    }
    return templates;
}
Also used : Condition(org.wso2.carbon.apimgt.keymgt.model.entity.Condition) APIPolicyConditionGroup(org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyConditionGroup) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) SubscriptionDataLoaderImpl(org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl) URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

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