Search in sources :

Example 31 with BandwidthLimit

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

the class SubscriptionValidationDataUtil method fromBandwidthLimitToDTO.

/**
 * Converts a Bandwidth Limit model object into a Bandwidth Limit DTO object.
 *
 * @param bandwidthLimit Bandwidth Limit model object
 * @return Bandwidth Limit DTO object derived from model
 */
private static BandwidthLimitDTO fromBandwidthLimitToDTO(BandwidthLimit bandwidthLimit) {
    BandwidthLimitDTO dto = new BandwidthLimitDTO();
    dto.setTimeUnit(bandwidthLimit.getTimeUnit());
    dto.setUnitTime(bandwidthLimit.getUnitTime());
    dto.setDataAmount(bandwidthLimit.getDataAmount());
    dto.setDataUnit(bandwidthLimit.getDataUnit());
    return dto;
}
Also used : BandwidthLimitDTO(org.wso2.carbon.apimgt.internal.service.dto.BandwidthLimitDTO)

Example 32 with BandwidthLimit

use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit 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 33 with BandwidthLimit

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

the class ApiMgtDAO method updateAPIPolicy.

/**
 * Update a API level throttling policy to database.
 * <p>
 * If condition group already exists for the policy, that condition Group will be deleted and condition Group will
 * be inserted to the database with old POLICY_ID.
 * </p>
 *
 * @param policy policy object defining the throttle policy
 * @throws APIManagementException
 */
public APIPolicy updateAPIPolicy(APIPolicy policy) throws APIManagementException {
    String updateQuery;
    int policyId = 0;
    String selectQuery;
    if (policy != null) {
        if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
            selectQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICY_ID_SQL;
            updateQuery = SQLConstants.ThrottleSQLConstants.UPDATE_API_POLICY_SQL;
        } else if (!StringUtils.isBlank(policy.getUUID())) {
            selectQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICY_ID_BY_UUID_SQL;
            updateQuery = ThrottleSQLConstants.UPDATE_API_POLICY_BY_UUID_SQL;
        } else {
            String errorMsg = "Policy object doesn't contain mandatory parameters. At least UUID or Name,Tenant Id" + " should be provided. Name: " + policy.getPolicyName() + ", Tenant Id: " + policy.getTenantId() + ", UUID: " + policy.getUUID();
            log.error(errorMsg);
            throw new APIManagementException(errorMsg);
        }
    } else {
        String errorMsg = "Provided Policy to update is null";
        log.error(errorMsg);
        throw new APIManagementException(errorMsg);
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        connection.setAutoCommit(false);
        try (PreparedStatement selectStatement = connection.prepareStatement(selectQuery);
            PreparedStatement deleteStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.DELETE_CONDITION_GROUP_SQL);
            PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) {
            if (selectQuery.equals(SQLConstants.ThrottleSQLConstants.GET_API_POLICY_ID_SQL)) {
                selectStatement.setString(1, policy.getPolicyName());
                selectStatement.setInt(2, policy.getTenantId());
            } else {
                selectStatement.setString(1, policy.getUUID());
            }
            try (ResultSet resultSet = selectStatement.executeQuery()) {
                if (resultSet.next()) {
                    policyId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID);
                }
            }
            deleteStatement.setInt(1, policyId);
            deleteStatement.executeUpdate();
            if (!StringUtils.isEmpty(policy.getDisplayName())) {
                updateStatement.setString(1, policy.getDisplayName());
            } else {
                updateStatement.setString(1, policy.getPolicyName());
            }
            updateStatement.setString(2, policy.getDescription());
            updateStatement.setString(3, policy.getDefaultQuotaPolicy().getType());
            if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
                RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
                updateStatement.setLong(4, limit.getRequestCount());
                updateStatement.setString(5, null);
            } else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
                BandwidthLimit limit = (BandwidthLimit) policy.getDefaultQuotaPolicy().getLimit();
                updateStatement.setLong(4, limit.getDataAmount());
                updateStatement.setString(5, limit.getDataUnit());
            } else if (PolicyConstants.EVENT_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
                EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
                updateStatement.setLong(4, limit.getEventCount());
                updateStatement.setString(5, null);
            }
            updateStatement.setLong(6, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
            updateStatement.setString(7, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
            if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
                updateStatement.setString(8, policy.getPolicyName());
                updateStatement.setInt(9, policy.getTenantId());
            } else if (!StringUtils.isBlank(policy.getUUID())) {
                updateStatement.setString(8, policy.getUUID());
            }
            int updatedRawCount = updateStatement.executeUpdate();
            if (updatedRawCount > 0) {
                List<Pipeline> pipelines = policy.getPipelines();
                if (pipelines != null) {
                    for (Pipeline pipeline : pipelines) {
                        // add each pipeline data to AM_CONDITION_GROUP table
                        addPipeline(pipeline, policyId, connection);
                    }
                }
            }
            connection.commit();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                // rollback failed. exception will be thrown later for upper exception
                log.error("Failed to rollback the add Global Policy: " + policy.toString(), ex);
            }
            handleException("Failed to update API policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
        }
    } catch (SQLException e) {
        handleException("Failed to update API policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
    }
    return policy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline)

Example 34 with BandwidthLimit

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

the class ApiMgtDAO 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 + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
    if (resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE).equalsIgnoreCase(PolicyConstants.REQUEST_COUNT_TYPE)) {
        RequestCountLimit reqLimit = new RequestCountLimit();
        reqLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
        reqLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
        reqLimit.setRequestCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
        quotaPolicy.setLimit(reqLimit);
    } else if (resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE).equalsIgnoreCase(PolicyConstants.BANDWIDTH_TYPE)) {
        BandwidthLimit bandLimit = new BandwidthLimit();
        bandLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
        bandLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
        bandLimit.setDataAmount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
        bandLimit.setDataUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
        quotaPolicy.setLimit(bandLimit);
    } else if (resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE).equalsIgnoreCase(PolicyConstants.EVENT_COUNT_TYPE)) {
        EventCountLimit eventCountLimit = new EventCountLimit();
        eventCountLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
        eventCountLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
        eventCountLimit.setEventCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
        quotaPolicy.setLimit(eventCountLimit);
    }
    policy.setUUID(resultSet.getString(ThrottlePolicyConstants.COLUMN_UUID));
    policy.setDescription(resultSet.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION));
    policy.setDisplayName(resultSet.getString(ThrottlePolicyConstants.COLUMN_DISPLAY_NAME));
    policy.setPolicyId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
    policy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
    policy.setTenantDomain(IdentityTenantUtil.getTenantDomain(policy.getTenantId()));
    policy.setDefaultQuotaPolicy(quotaPolicy);
    policy.setDeployed(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_DEPLOYED));
}
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) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 35 with BandwidthLimit

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

the class ApiMgtDAO method setCommonParametersForPolicy.

/**
 * Populates common attribute data of the <code>policy</code> to <code>policyStatement</code>
 *
 * @param policyStatement prepared statement initialized of policy operation
 * @param policy          <code>Policy</code> object with data
 * @throws SQLException
 */
private void setCommonParametersForPolicy(PreparedStatement policyStatement, Policy policy) throws SQLException {
    policyStatement.setString(1, policy.getPolicyName());
    if (!StringUtils.isEmpty(policy.getDisplayName())) {
        policyStatement.setString(2, policy.getDisplayName());
    } else {
        policyStatement.setString(2, policy.getPolicyName());
    }
    policyStatement.setInt(3, policy.getTenantId());
    policyStatement.setString(4, policy.getDescription());
    policyStatement.setString(5, policy.getDefaultQuotaPolicy().getType());
    // TODO use requestCount in same format in all places
    if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
        RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
        policyStatement.setLong(6, limit.getRequestCount());
        policyStatement.setString(7, null);
    } else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
        BandwidthLimit limit = (BandwidthLimit) policy.getDefaultQuotaPolicy().getLimit();
        policyStatement.setLong(6, limit.getDataAmount());
        policyStatement.setString(7, limit.getDataUnit());
    } else if (PolicyConstants.EVENT_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
        EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
        policyStatement.setLong(6, limit.getEventCount());
        policyStatement.setString(7, null);
    }
    policyStatement.setLong(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
    policyStatement.setString(9, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
    // policyStatement.setBoolean(9, APIUtil.isContentAwarePolicy(policy));
    policyStatement.setBoolean(10, policy.isDeployed());
    if (!StringUtils.isBlank(policy.getUUID())) {
        policyStatement.setString(11, policy.getUUID());
    } else {
        policyStatement.setString(11, UUID.randomUUID().toString());
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Aggregations

BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)19 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)18 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)15 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)13 EventCountLimit (org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)10 ArrayList (java.util.ArrayList)7 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)7 PreparedStatement (java.sql.PreparedStatement)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)4 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)4 Test (org.testng.annotations.Test)3 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)3 Condition (org.wso2.carbon.apimgt.api.model.policy.Condition)3 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)3