use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.
the class APIUtilTierTest method mockPolicyRetrieval.
private void mockPolicyRetrieval(ApiMgtDAO apiMgtDAO) throws Exception {
QuotaPolicy quotaPolicy = Mockito.mock(QuotaPolicy.class);
ApplicationPolicy applicationPolicy = Mockito.mock(ApplicationPolicy.class);
SubscriptionPolicy subscriptionPolicy = Mockito.mock(SubscriptionPolicy.class);
APIPolicy apiPolicy = Mockito.mock(APIPolicy.class);
Mockito.when(applicationPolicy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(subscriptionPolicy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(apiPolicy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(apiMgtDAO.getApplicationPolicy(anyString(), anyInt())).thenReturn(applicationPolicy);
Mockito.when(apiMgtDAO.getSubscriptionPolicy(anyString(), anyInt())).thenReturn(subscriptionPolicy);
Mockito.when(apiMgtDAO.getAPIPolicy(anyString(), anyInt())).thenReturn(apiPolicy);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
Map<String, List<Notifier>> notifierMap = Mockito.mock(Map.class);
List<Notifier> notifierList = new ArrayList<>();
Mockito.when(notifierMap.get(any())).thenReturn(notifierList);
Mockito.when(serviceReferenceHolder.getNotifiersMap()).thenReturn(notifierMap);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
Map<String, Long> defaultLimits = new HashMap<>();
Mockito.when(throttleProperties.getDefaultThrottleTierLimits()).thenReturn(defaultLimits);
Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.
the class APIUtilTierTest method generateAppPolicies.
private ApplicationPolicy[] generateAppPolicies(String[] policyNames) {
List<ApplicationPolicy> policyList = new ArrayList<ApplicationPolicy>();
for (String policyName : policyNames) {
ApplicationPolicy policy = new ApplicationPolicy(policyName);
QuotaPolicy quotaPolicy = new QuotaPolicy();
RequestCountLimit countLimit = new RequestCountLimit();
countLimit.setRequestCount(123);
quotaPolicy.setLimit(countLimit);
policy.setDefaultQuotaPolicy(quotaPolicy);
policy.setDescription(policyName);
policyList.add(policy);
}
ApplicationPolicy[] array = {};
return policyList.toArray(array);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDataUtil method getThrottleLimitDTO.
/**
* Converts a quota policy object of a policy into a Throttle Limit DTO object.
*
* @param policy policy model object
* @return Throttle Limit DTO
*/
private static ThrottleLimitDTO getThrottleLimitDTO(Policy policy) {
QuotaPolicy quotaPolicy = policy.getQuotaPolicy();
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;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevelDefault.
/**
* Generate default policy for API level throttling
*
* @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
* used as else condition
* @return the generated execution plan for the policy
* @throws APITemplateException if failed to generate policy
*/
public String getThrottlePolicyForAPILevelDefault(ApiPolicy policy) throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating default policy for API Level :" + policy.toString());
}
Set<String> conditionsSet = new HashSet<>();
try {
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
velocityengine.init();
Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
StringWriter writer;
VelocityContext context;
List<APIPolicyConditionGroup> conditionGroups = policy.getConditionGroups();
JSONArray policyConditionJson = new JSONArray();
if (conditionGroups != null) {
for (APIPolicyConditionGroup conditionGroup : conditionGroups) {
if (conditionGroup.getDefaultLimit() == null) {
continue;
}
policyConditionJson.add(getPolicyConditionJson(conditionGroup.getCondition()));
String conditionString = getPolicyConditionForDefault(conditionGroup.getCondition());
if (!StringUtils.isEmpty(conditionString)) {
conditionsSet.add(conditionString);
}
}
}
context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
context.put("quotaPolicy", policy.getDefaultLimit());
context.put("evaluatedConditions", new String(Base64.encodeBase64(policyConditionJson.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
String conditionSetString = getConditionForDefault(conditionsSet);
if (!StringUtils.isEmpty(conditionSetString)) {
context.put("condition", " AND " + conditionSetString);
} else {
context.put("condition", "");
}
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
return writer.toString();
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevel.
/**
* Generate policy for API level throttling
*
* @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
* used as else condition
* @return a Map containing a set of policies for each condition group
* @throws APITemplateException if failed to generate policy
*/
public Map<String, String> getThrottlePolicyForAPILevel(ApiPolicy policy) throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating policy for API Level :" + policy.toString());
}
Map<String, String> policyArray = new HashMap<>();
try {
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
velocityengine.init();
Template template = velocityengine.getTemplate(getTemplatePathForAPI());
StringWriter writer;
VelocityContext context;
if (policy.getConditionGroups() != null) {
for (APIPolicyConditionGroup conditionGroup : policy.getConditionGroups()) {
if (conditionGroup.getDefaultLimit() == null) {
continue;
}
context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
context.put("quotaPolicy", conditionGroup.getDefaultLimit());
context.put("pipeline", "condition_" + conditionGroup.getConditionGroupId());
String conditionString = getPolicyCondition(conditionGroup.getCondition());
JSONArray conditions = new JSONArray();
conditions.add(getPolicyConditionJson(conditionGroup.getCondition()));
context.put("condition", " AND " + conditionString);
context.put("evaluatedConditions", new String(Base64.encodeBase64(conditions.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
String policyName = policy.getTenantDomain() + APIConstants.DELEM_UNDERSCORE + PolicyConstants.POLICY_LEVEL_RESOURCE + APIConstants.DELEM_UNDERSCORE + policy.getName() + APIConstants.THROTTLE_POLICY_CONDITION + conditionGroup.getConditionGroupId();
policyArray.put(policyName, writer.toString());
}
}
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return policyArray;
}
Aggregations