Search in sources :

Example 36 with RequestCountLimit

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

the class TestUtil method getPolicyGlobalLevel.

/**
 * Generate a dummy Global Policy object
 *
 * @return an Global Policy
 */
public static GlobalPolicy getPolicyGlobalLevel() {
    GlobalPolicy globalPolicy = new GlobalPolicy();
    globalPolicy.setId(1);
    globalPolicy.setTenantId(-1234);
    globalPolicy.setTenantDomain("carbon.super");
    globalPolicy.setName("policy1");
    globalPolicy.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);
    globalPolicy.setDefaultLimit(defaultLimit);
    globalPolicy.setSiddhiQuery("FROM   RequestStream SELECT   userId,   " + "(userId == 'admin@carbon.super') AS isEligible,   " + "str :concat('admin@carbon.super', '') as throttleKey INSERT INTO   " + "EligibilityStream; FROM   EligibilityStream [isEligible==true] " + "#throttler:timeBatch(1 min) SELECT throttleKey, (count(userId) >= 15) " + "as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS " + "into ResultStream;");
    globalPolicy.setKeyTemplate("$userId:$apiContext:$apiVersion");
    return globalPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) QuotaPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy)

Example 37 with RequestCountLimit

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

the class TestUtil method getPolicyAppLevel.

/**
 * Generate a dummy Application Policy object
 *
 * @return an Application Policy
 */
public static ApplicationPolicy getPolicyAppLevel() {
    ApplicationPolicy applicationPolicy = new ApplicationPolicy();
    applicationPolicy.setId(1);
    applicationPolicy.setTenantId(-1234);
    applicationPolicy.setTenantDomain("carbon.super");
    applicationPolicy.setName("policy1");
    applicationPolicy.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);
    applicationPolicy.setDefaultLimit(defaultLimit);
    return applicationPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) QuotaPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy)

Example 38 with RequestCountLimit

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

the class PolicyDAOImpl method setCommonPolicyDetails.

/**
 * Populated common attributes of policy type objects to <code>policy</code>
 * from <code>resultSet</code>
 *
 * @param policy    initiallized {@link Policy} object to populate
 * @param resultSet {@link ResultSet} with data to populate <code>policy</code>
 * @throws SQLException
 */
private void setCommonPolicyDetails(Policy policy, ResultSet resultSet) throws SQLException {
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    String prefix = "";
    if (policy instanceof APIPolicy) {
        prefix = "DEFAULT_";
    }
    quotaPolicy.setType(resultSet.getString(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
    if (resultSet.getString(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE).equalsIgnoreCase(PolicyConstants.REQUEST_COUNT_TYPE)) {
        RequestCountLimit reqLimit = new RequestCountLimit(resultSet.getString(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_TIME_UNIT), resultSet.getInt(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_UNIT_TIME), resultSet.getInt(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA));
        quotaPolicy.setLimit(reqLimit);
    } else if (resultSet.getString(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE).equalsIgnoreCase(PolicyConstants.BANDWIDTH_TYPE)) {
        BandwidthLimit bandLimit = new BandwidthLimit(resultSet.getString(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_TIME_UNIT), resultSet.getInt(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_UNIT_TIME), resultSet.getInt(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA), resultSet.getString(prefix + APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
        quotaPolicy.setLimit(bandLimit);
    }
    policy.setUuid(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID));
    policy.setDescription(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_DESCRIPTION));
    policy.setDisplayName(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_DISPLAY_NAME));
    policy.setDefaultQuotaPolicy(quotaPolicy);
    policy.setDeployed(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_DEPLOYED));
}
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) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 39 with RequestCountLimit

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

the class PolicyDAOImpl method getPipelines.

/**
 * Retrieves list of pipelines for the policy with policy Id: <code>policyId</code>
 *
 * @param policyId policy id of the pipelines
 * @return list of pipelines
 * @throws SQLException
 */
private ArrayList<Pipeline> getPipelines(String policyId, Connection connection) throws SQLException {
    ArrayList<Pipeline> pipelines = new ArrayList<>();
    final String sqlQuery = "SELECT CONDITION_GROUP_ID,QUOTA_TYPE,QUOTA,QUOTA_UNIT,UNIT_TIME,TIME_UNIT," + "DESCRIPTION FROM AM_CONDITION_GROUP WHERE UUID =?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        int unitTime;
        int quota;
        int pipelineId;
        String timeUnit;
        String quotaUnit;
        String description;
        preparedStatement.setString(1, policyId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Pipeline pipeline = new Pipeline();
                ArrayList<Condition> conditions;
                QuotaPolicy quotaPolicy = new QuotaPolicy();
                quotaPolicy.setType(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
                timeUnit = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_TIME_UNIT);
                quotaUnit = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_UNIT);
                unitTime = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UNIT_TIME);
                quota = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA);
                pipelineId = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_ID);
                description = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_DESCRIPTION);
                if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
                    RequestCountLimit requestCountLimit = new RequestCountLimit(timeUnit, unitTime, quota);
                    quotaPolicy.setLimit(requestCountLimit);
                } else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
                    BandwidthLimit bandwidthLimit = new BandwidthLimit(timeUnit, unitTime, quota, quotaUnit);
                    quotaPolicy.setLimit(bandwidthLimit);
                }
                conditions = getConditions(pipelineId, connection);
                pipeline.setConditions(conditions);
                pipeline.setQuotaPolicy(quotaPolicy);
                pipeline.setId(pipelineId);
                pipeline.setDescription(description);
                pipelines.add(pipeline);
            }
        }
    }
    return pipelines;
}
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) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 40 with RequestCountLimit

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

the class TierMappingUtil method fromTierToDTO.

/**
 * Converts a Tier object into TierDTO
 *
 * @param tier      Tier object
 * @param tierLevel tier level (api/application or resource)
 * @return TierDTO corresponds to Tier object
 */
public static TierDTO fromTierToDTO(Policy tier, String tierLevel) {
    TierDTO dto = new TierDTO();
    dto.setName(tier.getPolicyName());
    dto.setDescription(tier.getDescription());
    dto.setTierLevel(TierDTO.TierLevelEnum.valueOf(StringUtils.upperCase(tierLevel)));
    dto.setUnitTime((long) tier.getDefaultQuotaPolicy().getLimit().getUnitTime());
    Limit limit = tier.getDefaultQuotaPolicy().getLimit();
    if (limit instanceof RequestCountLimit) {
        dto.setRequestCount((long) ((RequestCountLimit) limit).getRequestCount());
    } else if (limit instanceof BandwidthLimit) {
        dto.setRequestCount((long) ((BandwidthLimit) limit).getDataAmount());
    }
    // // TODO: 08/12/16 More fields to map
    return dto;
}
Also used : TierDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierDTO) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

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