use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class ThrottlerUtil method addDefaultAdvancedThrottlePolicies.
/**
* Deploy default throttle polices at startup
*
* @throws APIManagementException throws if any exception occured
*/
public static void addDefaultAdvancedThrottlePolicies() throws APIManagementException {
int[] requestCount = new int[] { 50, 20, 10, Integer.MAX_VALUE };
// Adding application level throttle policies
String[] appPolicies = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED };
String[] appPolicyDecs = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_LARGE_DESC, ThrottleConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, ThrottleConstants.DEFAULT_APP_POLICY_SMALL_DESC, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
String policyName;
// Add application level throttle policies
for (int i = 0; i < appPolicies.length; i++) {
policyName = appPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.application, policyName)) {
ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
applicationPolicy.setUuid(UUID.randomUUID().toString());
applicationPolicy.setDisplayName(policyName);
applicationPolicy.setDescription(appPolicyDecs[i]);
applicationPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCount[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
policyDAO.addApplicationPolicy(applicationPolicy);
}
}
// Adding Subscription level policies
int[] requestCountSubPolicies = new int[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
String[] subPolicies = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED };
String[] subPolicyDecs = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
for (int i = 0; i < subPolicies.length; i++) {
policyName = subPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.subscription, policyName)) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
subscriptionPolicy.setUuid(UUID.randomUUID().toString());
subscriptionPolicy.setDisplayName(policyName);
subscriptionPolicy.setDescription(subPolicyDecs[i]);
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountSubPolicies[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setBillingPlan(ThrottleConstants.BILLING_PLAN_FREE);
policyDAO.addSubscriptionPolicy(subscriptionPolicy);
}
}
// Adding Resource level policies
String[] apiPolicies = new String[] { ThrottleConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED };
String[] apiPolicyDecs = new String[] { ThrottleConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, ThrottleConstants.DEFAULT_API_POLICY_PLUS_DESC, ThrottleConstants.DEFAULT_API_POLICY_BASIC_DESC, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
int[] requestCountApiPolicies = new int[] { 50000, 20000, 10000, Integer.MAX_VALUE };
for (int i = 0; i < apiPolicies.length; i++) {
policyName = apiPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.api, policyName)) {
APIPolicy apiPolicy = new APIPolicy(policyName);
apiPolicy.setUuid(UUID.randomUUID().toString());
apiPolicy.setDisplayName(policyName);
apiPolicy.setDescription(apiPolicyDecs[i]);
apiPolicy.setUserLevel(ThrottleConstants.API_POLICY_API_LEVEL);
apiPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountApiPolicies[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
policyDAO.addApiPolicy(apiPolicy);
}
}
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultAPIPolicy.
/**
* create default api policy
*
* @return APIPolicy object is returned
*/
public static APIPolicy createDefaultAPIPolicy() {
APIPolicy apiPolicy = new APIPolicy(SAMPLE_API_POLICY);
apiPolicy.setUuid(UUID.randomUUID().toString());
apiPolicy.setDisplayName(SAMPLE_API_POLICY);
apiPolicy.setDescription(SAMPLE_API_POLICY_DESCRIPTION);
apiPolicy.setUserLevel(APIMgtConstants.ThrottlePolicyConstants.API_LEVEL);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1000, 10000);
defaultQuotaPolicy.setLimit(requestCountLimit);
apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
apiPolicy.setPipelines(createDefaultPipelines());
return apiPolicy;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultSubscriptionPolicy.
public static SubscriptionPolicy createDefaultSubscriptionPolicy() {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(SAMPLE_SUBSCRIPTION_POLICY);
subscriptionPolicy.setUuid(UUID.randomUUID().toString());
subscriptionPolicy.setDisplayName(SAMPLE_SUBSCRIPTION_POLICY);
subscriptionPolicy.setDescription(SAMPLE_SUBSCRIPTION_POLICY_DESCRIPTION);
subscriptionPolicy.setCustomAttributes(SAMPLE_CUSTOM_ATTRIBUTE.getBytes());
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 10000, 1000);
defaultQuotaPolicy.setLimit(requestCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setBillingPlan(BUSINESS_PLAN_FREE);
subscriptionPolicy.setRateLimitCount(100);
subscriptionPolicy.setRateLimitTimeUnit("s");
subscriptionPolicy.setStopOnQuotaReach(true);
return subscriptionPolicy;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultSiddhiAppforSubscriptionPolicy.
public static String createDefaultSiddhiAppforSubscriptionPolicy() {
SubscriptionPolicy policy = createDefaultSubscriptionPolicy();
RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
String siddhiApp = "@App:name('subscription_" + policy.getPolicyName() + "')\n" + "\n@App:description('ExecutionPlan for subscription_" + policy.getPolicyName() + "')\n" + "\n@source(type='inMemory', topic='apim', @map(type='passThrough'))\n" + "define stream RequestStream (messageID string, appKey string, appTier string," + " subscriptionKey string," + " apiKey string, apiTier string, subscriptionTier string, resourceKey string, resourceTier string," + " userId string, apiContext string, apiVersion string, appTenant string, apiTenant string, " + "appId string, apiName string, propertiesMap string);\n" + "\n@sink(type='jms', @map(type='text'),\n" + "factory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672', destination='TEST.FOO', connection.factory." + "type='topic',\n" + "connection.factory.jndi.name='TopicConnectionFactory')\n" + "define stream GlobalThrottleStream (throttleKey string, isThrottled bool" + ", expiryTimeStamp long);\n" + "\nFROM RequestStream\n" + "SELECT messageID, (subscriptionTier == '" + policy.getPolicyName() + "')" + " AS isEligible, subscriptionKey AS throttleKey, propertiesMap\n" + "INSERT INTO EligibilityStream;\n" + "\nFROM EligibilityStream[isEligible==true]#throttler:timeBatch(" + policy.getDefaultQuotaPolicy().getLimit().getUnitTime() + " " + policy.getDefaultQuotaPolicy().getLimit().getTimeUnit() + ", 0)\n" + "select throttleKey, (count(messageID) >= " + limit.getRequestCount() + ")" + " as isThrottled, expiryTimeStamp group by throttleKey\n" + "INSERT ALL EVENTS into ResultStream;\n" + "\nfrom ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)" + " select * " + "insert into GlobalThrottleStream;\n";
return siddhiApp;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createApplicationPolicyWithRequestLimit.
public static ApplicationPolicy createApplicationPolicyWithRequestLimit(String name) {
ApplicationPolicy applicationPolicy = new ApplicationPolicy(name);
applicationPolicy.setDescription("testDescription");
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType("requestCount");
RequestCountLimit requestCountLimit = new RequestCountLimit("s", 60, 10);
quotaPolicy.setLimit(requestCountLimit);
applicationPolicy.setDefaultQuotaPolicy(quotaPolicy);
applicationPolicy.setDisplayName("displayName");
return applicationPolicy;
}
Aggregations