use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit 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;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class APIProviderImplTest method getPolicySubscriptionLevelperUser.
private SubscriptionPolicy getPolicySubscriptionLevelperUser() {
SubscriptionPolicy policy = new SubscriptionPolicy("gold");
policy.setDescription("Description");
RequestCountLimit defaultLimit = new RequestCountLimit();
defaultLimit.setTimeUnit("min");
defaultLimit.setUnitTime(5);
defaultLimit.setRequestCount(200);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setLimit(defaultLimit);
defaultQuotaPolicy.setType("RequestCount");
policy.setDefaultQuotaPolicy(defaultQuotaPolicy);
return policy;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit 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));
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit 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;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit 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;
}
Aggregations