Search in sources :

Example 6 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromQuotaPolicyToDTO.

/**
 * Converts a Quota Policy object into a Throttle Limit DTO object
 *
 * @param quotaPolicy Quota Policy object
 * @return Throttle Limit DTO object derived from the Quota Policy object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static ThrottleLimitDTO fromQuotaPolicyToDTO(QuotaPolicy quotaPolicy) throws UnsupportedThrottleLimitTypeException {
    Limit limit = quotaPolicy.getLimit();
    String throttleLimitType = quotaPolicy.getType();
    if (PolicyConstants.REQUEST_COUNT_TYPE.equals(throttleLimitType)) {
        if (limit instanceof RequestCountLimit) {
            RequestCountLimit requestCountLimit = (RequestCountLimit) limit;
            return fromRequestCountLimitToDTO(requestCountLimit);
        } else {
            String msg = "Throttle limit type " + throttleLimitType + " is not of type RequestCountLimit";
            log.error(msg);
            throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
        }
    } else if (PolicyConstants.BANDWIDTH_TYPE.equals(throttleLimitType)) {
        if (limit instanceof BandwidthLimit) {
            BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
            return fromBandwidthLimitToDTO(bandwidthLimit);
        } else {
            String msg = "Throttle limit type " + throttleLimitType + " is not of type BandwidthLimit";
            log.error(msg);
            throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
        }
    } else {
        String msg = "Throttle limit type " + throttleLimitType + " is not supported";
        log.error(msg);
        throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
    }
}
Also used : UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 7 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultPipelines.

/**
 * create default pipeline for api policy
 *
 * @return list of Pipeline objects is returned
 */
public static List<Pipeline> createDefaultPipelines() {
    // Pipeline 1
    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
    ipCondition.setStartingIP("192.168.12.3");
    ipCondition.setEndingIP("192.168.88.19");
    IPCondition ipConditionSpecific = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
    ipConditionSpecific.setSpecificIP("123.42.14.56");
    // adding above conditions to condition list of pipeline 1
    // contains conditions for each pipeline
    List<Condition> conditionsList = new ArrayList<>();
    conditionsList.add(ipCondition);
    conditionsList.add(ipConditionSpecific);
    // set quota policy with bandwidth limit
    BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
    QuotaPolicy quotaPolicy1 = new QuotaPolicy();
    quotaPolicy1.setType(PolicyConstants.BANDWIDTH_TYPE);
    quotaPolicy1.setLimit(bandwidthLimit);
    Pipeline pipeline1 = new Pipeline();
    pipeline1.setId(0);
    pipeline1.setConditions(conditionsList);
    pipeline1.setQuotaPolicy(quotaPolicy1);
    // End of pipeline 1 -> Beginning of pipeline 2
    HeaderCondition headerCondition = new HeaderCondition();
    headerCondition.setHeader("Browser");
    headerCondition.setValue("Chrome");
    JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
    jwtClaimsCondition.setClaimUrl("/path/path2");
    jwtClaimsCondition.setAttribute("attributed");
    QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
    queryParameterCondition.setParameter("Location");
    queryParameterCondition.setValue("Colombo");
    // adding conditions to condition list of pipeline2
    conditionsList = new ArrayList<>();
    conditionsList.add(headerCondition);
    conditionsList.add(jwtClaimsCondition);
    conditionsList.add(queryParameterCondition);
    // pipeline 2 with request count as quota policy
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
    QuotaPolicy quotaPolicy2 = new QuotaPolicy();
    quotaPolicy2.setType(REQUEST_COUNT_TYPE);
    quotaPolicy2.setLimit(requestCountLimit);
    Pipeline pipeline2 = new Pipeline();
    pipeline2.setId(1);
    pipeline2.setConditions(conditionsList);
    pipeline2.setQuotaPolicy(quotaPolicy2);
    // adding pipelines
    // contains all the default pipelines
    List<Pipeline> pipelineList = new ArrayList<>();
    pipelineList.add(pipeline1);
    pipelineList.add(pipeline2);
    return pipelineList;
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ArrayList(java.util.ArrayList) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 8 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultAPIPolicy.

/**
 * create default api policy
 *
 * @return APIPolicy object is returned
 */
protected static APIPolicy createDefaultAPIPolicy() {
    APIPolicy apiPolicy = new APIPolicy(SAMPLE_API_POLICY);
    apiPolicy.setUuid(UUID.randomUUID().toString());
    apiPolicy.setDisplayName(SAMPLE_API_POLICY);
    apiPolicy.setDescription(SAMPLE_API_POLICY_DESCRIPTION);
    apiPolicy.setUserLevel(APIMgtConstants.ThrottlePolicyConstants.API_LEVEL);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(REQUEST_COUNT_TYPE);
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1000, 10000);
    defaultQuotaPolicy.setLimit(requestCountLimit);
    apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    apiPolicy.setPipelines(createDefaultPipelines());
    return apiPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 9 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultSubscriptionPolicy.

/**
 * Create a subscription policy.
 *
 * @return SubscriptionPolicy object
 */
protected static SubscriptionPolicy createDefaultSubscriptionPolicy() {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(SAMPLE_SUBSCRIPTION_POLICY);
    subscriptionPolicy.setUuid(UUID.randomUUID().toString());
    subscriptionPolicy.setDisplayName(SAMPLE_SUBSCRIPTION_POLICY);
    subscriptionPolicy.setDescription(SAMPLE_SUBSCRIPTION_POLICY_DESCRIPTION);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(REQUEST_COUNT_TYPE);
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 10000, 1000);
    defaultQuotaPolicy.setLimit(requestCountLimit);
    subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    return subscriptionPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)

Example 10 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createSubscriptionPolicyWithRequestLimit.

public static SubscriptionPolicy createSubscriptionPolicyWithRequestLimit(String name) {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(name);
    subscriptionPolicy.setDescription("testDescription");
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit("s", 60, 10);
    quotaPolicy.setLimit(requestCountLimit);
    subscriptionPolicy.setDefaultQuotaPolicy(quotaPolicy);
    return subscriptionPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)

Aggregations

RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)33 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)30 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)25 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)22 ArrayList (java.util.ArrayList)16 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)16 Test (org.testng.annotations.Test)14 EventCountLimit (org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)12 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)12 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)11 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)9 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)9 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)8 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)8 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)7 PreparedStatement (java.sql.PreparedStatement)6 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)6 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)5