use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtilTestCase method fromBandwidthThrottleLimitDtoToQuotaPolicyTest.
@Test(description = "Convert Bandwidth Throttle Limit DTO to Quota Policy")
public void fromBandwidthThrottleLimitDtoToQuotaPolicyTest() throws Exception {
ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
throttleLimitDTO.setType(PolicyConstants.BANDWIDTH_LIMIT_TYPE);
BandwidthLimitDTO bandwidthLimitDTO = new BandwidthLimitDTO();
bandwidthLimitDTO.setDataAmount(10);
bandwidthLimitDTO.setDataUnit(KB);
throttleLimitDTO.setBandwidthLimit(bandwidthLimitDTO);
throttleLimitDTO.setTimeUnit("min");
throttleLimitDTO.setUnitTime(1);
QuotaPolicy policy = CommonThrottleMappingUtil.fromDTOToQuotaPolicy(throttleLimitDTO);
Assert.assertNotNull(policy);
assertEquals(policy.getType(), PolicyConstants.BANDWIDTH_TYPE);
BandwidthLimit bandwidthLimit = (BandwidthLimit) policy.getLimit();
assertEquals(bandwidthLimit.getDataAmount(), 10);
assertEquals(bandwidthLimit.getDataUnit(), KB);
assertEquals(bandwidthLimit.getTimeUnit(), "min");
assertEquals(bandwidthLimit.getUnitTime(), 1);
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class TierMappingUtilTestCase method testFromTierListToDTO.
@Test
public void testFromTierListToDTO() {
Policy policy1 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold");
Policy policy2 = SampleTestObjectCreator.createSubscriptionPolicyWithBndwidthLimit("Silver");
List<Policy> policyList = new ArrayList<>();
policyList.add(policy1);
policyList.add(policy2);
TierListDTO tierListDTO = TierMappingUtil.fromTierListToDTO(policyList, "subscription", 10, 0);
assertEquals(tierListDTO.getCount(), (Integer) policyList.size());
assertEquals(tierListDTO.getList().get(0).getName(), policy1.getPolicyName());
assertEquals(tierListDTO.getList().get(0).getDescription(), policy1.getDescription());
assertEquals(tierListDTO.getList().get(0).getTierLevel().name(), "SUBSCRIPTION");
assertEquals(tierListDTO.getList().get(0).getUnitTime().longValue(), policy1.getDefaultQuotaPolicy().getLimit().getUnitTime());
assertEquals(tierListDTO.getList().get(0).getRequestCount().longValue(), ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount());
assertEquals(tierListDTO.getList().get(1).getName(), policy2.getPolicyName());
assertEquals(tierListDTO.getList().get(1).getDescription(), policy2.getDescription());
assertEquals(tierListDTO.getList().get(1).getTierLevel().name(), "SUBSCRIPTION");
assertEquals(tierListDTO.getList().get(1).getUnitTime().longValue(), policy2.getDefaultQuotaPolicy().getLimit().getUnitTime());
assertEquals(tierListDTO.getList().get(1).getRequestCount().longValue(), ((BandwidthLimit) policy2.getDefaultQuotaPolicy().getLimit()).getDataAmount());
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateApplicationPolicy.
/**
* Updates Application level policy.
* <p>policy name and tenant id should be specified in <code>policy</code></p>
*
* @param policy updated policy object
* @throws APIManagementException
*/
public void updateApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
Connection connection = null;
PreparedStatement updateStatement = null;
boolean hasCustomAttrib = false;
String updateQuery;
if (policy.getTenantId() == -1 || StringUtils.isEmpty(policy.getPolicyName())) {
String errorMsg = "Policy object doesn't contain mandatory parameters. Name: " + policy.getPolicyName() + ", Tenant Id: " + policy.getTenantId();
log.error(errorMsg);
throw new APIManagementException(errorMsg);
}
try {
if (policy.getCustomAttributes() != null) {
hasCustomAttrib = true;
}
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(false);
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_WITH_CUSTOM_ATTRIBUTES_SQL;
}
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_BY_UUID_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_WITH_CUSTOM_ATTRIBUTES_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);
}
updateStatement = connection.prepareStatement(updateQuery);
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());
}
updateStatement.setLong(6, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
updateStatement.setString(7, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
if (hasCustomAttrib) {
updateStatement.setBlob(8, new ByteArrayInputStream(policy.getCustomAttributes()));
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(9, policy.getPolicyName());
updateStatement.setInt(10, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(9, policy.getUUID());
}
} else {
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());
}
}
updateStatement.executeUpdate();
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
// Rollback failed. Exception will be thrown later for upper exception
log.error("Failed to rollback the update Application Policy: " + policy.toString(), ex);
}
}
handleException("Failed to update application policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
} finally {
APIMgtDBUtil.closeAllConnections(updateStatement, connection, null);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil 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
*/
public static BandwidthLimitDTO fromBandwidthLimitToDTO(BandwidthLimit bandwidthLimit) {
// done
BandwidthLimitDTO dto = new BandwidthLimitDTO();
dto.setTimeUnit(bandwidthLimit.getTimeUnit());
dto.setUnitTime(bandwidthLimit.getUnitTime());
dto.setDataAmount(bandwidthLimit.getDataAmount());
dto.setDataUnit(bandwidthLimit.getDataUnit());
return dto;
}
use of org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit in project carbon-apimgt by wso2.
the class ApiMgtDAO 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 APIManagementException
*/
private ArrayList<Pipeline> getPipelines(int policyId) throws APIManagementException {
Connection connection = null;
PreparedStatement pipelinesStatement = null;
ResultSet resultSet = null;
ArrayList<Pipeline> pipelines = new ArrayList<Pipeline>();
try {
connection = APIMgtDBUtil.getConnection();
pipelinesStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.GET_PIPELINES_SQL);
int unitTime = 0;
int quota = 0;
int pipelineId = -1;
String timeUnit = null;
String quotaUnit = null;
String description;
pipelinesStatement.setInt(1, policyId);
resultSet = pipelinesStatement.executeQuery();
while (resultSet.next()) {
Pipeline pipeline = new Pipeline();
ArrayList<Condition> conditions = null;
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
timeUnit = resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT);
quotaUnit = resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_UNIT);
unitTime = resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME);
quota = resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA);
pipelineId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONDITION_ID);
description = resultSet.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION);
if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setUnitTime(unitTime);
requestCountLimit.setTimeUnit(timeUnit);
requestCountLimit.setRequestCount(quota);
quotaPolicy.setLimit(requestCountLimit);
} else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
BandwidthLimit bandwidthLimit = new BandwidthLimit();
bandwidthLimit.setUnitTime(unitTime);
bandwidthLimit.setTimeUnit(timeUnit);
bandwidthLimit.setDataUnit(quotaUnit);
bandwidthLimit.setDataAmount(quota);
quotaPolicy.setLimit(bandwidthLimit);
}
conditions = getConditions(pipelineId);
pipeline.setConditions(conditions);
pipeline.setQuotaPolicy(quotaPolicy);
pipeline.setId(pipelineId);
pipeline.setDescription(description);
pipelines.add(pipeline);
}
} catch (SQLException e) {
handleException("Failed to get pipelines for policyId: " + policyId, e);
} finally {
APIMgtDBUtil.closeAllConnections(pipelinesStatement, connection, resultSet);
}
return pipelines;
}
Aggregations