Search in sources :

Example 6 with EventCountLimit

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

the class CommonThrottleMappingUtil method fromDTOToEventCountLimit.

private static EventCountLimit fromDTOToEventCountLimit(EventCountLimitDTO dto) {
    EventCountLimit eventCountLimit = new EventCountLimit();
    eventCountLimit.setTimeUnit(dto.getTimeUnit());
    eventCountLimit.setUnitTime(dto.getUnitTime());
    eventCountLimit.setEventCount(dto.getEventCount());
    return eventCountLimit;
}
Also used : EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)

Example 7 with EventCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.EventCountLimit 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)

Example 8 with EventCountLimit

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

the class SubscriptionValidationDAO method setCommonProperties.

private void setCommonProperties(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 (quotaPolicy.getType() != null) {
        if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            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 (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            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 (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
            EventCountLimit eventCountLimit = new EventCountLimit();
            eventCountLimit.setEventCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            eventCountLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            eventCountLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            quotaPolicy.setLimit(eventCountLimit);
        }
        policy.setQuotaPolicy(quotaPolicy);
    }
}
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.subscription.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 9 with EventCountLimit

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

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

the class ThrottlingPolicyMappingUtil method fromSubscriptionToDTO.

public static SubscriptionPolicyDTO fromSubscriptionToDTO(SubscriptionPolicy subscriptionPolicy, int position) {
    SubscriptionPolicyDTO dto = new SubscriptionPolicyDTO();
    dto.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
    dto.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
    dto.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
    ThrottleLimitDTO limitDTO = new ThrottleLimitDTO();
    limitDTO.setType(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT);
    EventCountLimit eventCountLimit = (EventCountLimit) subscriptionPolicy.getDefaultQuotaPolicy().getLimit();
    EventCountLimitDTO eventCountLimitDTO = new EventCountLimitDTO();
    eventCountLimitDTO.setEventCount(eventCountLimit.getEventCount());
    eventCountLimitDTO.setTimeUnit(eventCountLimit.getTimeUnit());
    eventCountLimitDTO.setUnitTime(eventCountLimit.getUnitTime());
    limitDTO.setEventCount(eventCountLimitDTO);
    dto.setDefaultLimit(limitDTO);
    dto.setSubscriberCount(subscriptionPolicy.getSubscriberCount());
    dto.setDisplayName(subscriptionPolicy.getDisplayName());
    dto.setDescription(subscriptionPolicy.getDescription());
    dto.setIsDeployed(subscriptionPolicy.isDeployed());
    dto.setPolicyName(subscriptionPolicy.getPolicyName());
    dto.setBillingPlan(subscriptionPolicy.getBillingPlan());
    dto.setPolicyId(position);
    dto.setUuid(subscriptionPolicy.getUUID());
    dto.setIsDeployed(subscriptionPolicy.isDeployed());
    dto.setTenantDomain(subscriptionPolicy.getTenantDomain());
    dto.setTenantId(subscriptionPolicy.getTenantId());
    return dto;
}
Also used : EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) EventCountLimitDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.EventCountLimitDTO) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO) SubscriptionPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionPolicyDTO)

Aggregations

EventCountLimit (org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)14 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)12 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)10 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)9 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)4 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)4 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)4 Endpoint (org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleLimitTypeException)2 Tier (org.wso2.carbon.apimgt.api.model.Tier)2 Limit (org.wso2.carbon.apimgt.api.model.policy.Limit)2 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)2 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)2 ThrottleLimitDTO (org.wso2.carbon.apimgt.internal.service.dto.ThrottleLimitDTO)2 ThrottleLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleLimitDTO)2