use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.
the class ThrottlingPolicyTemplateBuilderTest method testGetThrottlePolicyForAPILevel.
@Test
public void testGetThrottlePolicyForAPILevel() throws Exception {
ApiPolicy policy = TestUtil.getPolicyAPILevel();
Map<String, String> policyStringArray = templateBuilder.getThrottlePolicyForAPILevel(policy);
Assert.assertEquals(1, policyStringArray.size());
String defaultPolicyString = templateBuilder.getThrottlePolicyForAPILevelDefault(policy);
Assert.assertNotNull(defaultPolicyString);
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.
the class PolicyUtil method deployAllPolicies.
/**
* Deploy all the throttle policies retrieved from the database in the Traffic Manager.
*/
public static void deployAllPolicies() {
// Undeploy all existing policies
undeployAllPolicies();
PolicyRetriever policyRetriever = new PolicyRetriever();
try {
// Deploy all the policies retrieved from the database
SubscriptionPolicyList subscriptionPolicies = policyRetriever.getAllSubscriptionPolicies();
for (SubscriptionPolicy subscriptionPolicy : subscriptionPolicies.getList()) {
if (!(APIConstants.UNLIMITED_TIER.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()))) {
deployPolicy(subscriptionPolicy, null);
}
}
ApplicationPolicyList applicationPolicies = policyRetriever.getAllApplicationPolicies();
for (ApplicationPolicy applicationPolicy : applicationPolicies.getList()) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(applicationPolicy.getName())) {
deployPolicy(applicationPolicy, null);
}
}
ApiPolicyList apiPolicies = policyRetriever.getAllApiPolicies();
for (ApiPolicy apiPolicy : apiPolicies.getList()) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(apiPolicy.getName())) {
deployPolicy(apiPolicy, null);
}
}
GlobalPolicyList globalPolicies = policyRetriever.getAllGlobalPolicies();
for (GlobalPolicy globalPolicy : globalPolicies.getList()) {
deployPolicy(globalPolicy, null);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving throttle policies", e);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy 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);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method populateApiPolicyList.
private List<APIPolicy> populateApiPolicyList(ResultSet resultSet) throws SQLException {
List<APIPolicy> apiPolicies = new ArrayList<>();
Map<Integer, APIPolicy> temp = new ConcurrentHashMap<>();
if (resultSet != null) {
while (resultSet.next()) {
int policyId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID);
APIPolicy apiPolicy = temp.get(policyId);
if (apiPolicy == null) {
apiPolicy = new APIPolicy();
apiPolicy.setId(policyId);
apiPolicy.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
apiPolicy.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_DEFAULT_QUOTA_POLICY_TYPE));
apiPolicy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
String tenantDomain = APIUtil.getTenantDomainFromTenantId(apiPolicy.getTenantId());
apiPolicy.setTenantDomain(tenantDomain);
apiPolicy.setApplicableLevel(resultSet.getString(ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
setCommonProperties(apiPolicy, resultSet);
apiPolicies.add(apiPolicy);
}
APIPolicyConditionGroup apiPolicyConditionGroup = new APIPolicyConditionGroup();
int conditionGroup = resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONDITION_ID);
apiPolicyConditionGroup.setConditionGroupId(conditionGroup);
apiPolicyConditionGroup.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
apiPolicyConditionGroup.setPolicyId(policyId);
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
ConditionGroupDTO conditionGroupDTO = null;
try {
conditionGroupDTO = apiMgtDAO.createConditionGroupDTO(conditionGroup);
} catch (APIManagementException e) {
log.error("Error while processing api policies for policyId : " + policyId, e);
}
ConditionDTO[] conditionDTOS = conditionGroupDTO.getConditions();
apiPolicyConditionGroup.setConditionDTOS(Arrays.asList(conditionDTOS));
setCommonProperties(apiPolicyConditionGroup, resultSet);
apiPolicy.addConditionGroup(apiPolicyConditionGroup);
temp.put(policyId, apiPolicy);
}
}
return apiPolicies;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.
the class APIUtilTierTest method generateApiPolicies.
private APIPolicy[] generateApiPolicies(String[] policyNames) {
List<APIPolicy> policyList = new ArrayList<APIPolicy>();
for (String policyName : policyNames) {
APIPolicy policy = new APIPolicy(policyName);
QuotaPolicy quotaPolicy = new QuotaPolicy();
RequestCountLimit countLimit = new RequestCountLimit();
countLimit.setRequestCount(123);
quotaPolicy.setLimit(countLimit);
policy.setDefaultQuotaPolicy(quotaPolicy);
policy.setDescription(policyName);
policyList.add(policy);
}
APIPolicy[] array = {};
return policyList.toArray(array);
}
Aggregations