use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIProviderImpl method addPolicy.
/**
* Deploy policy to global CEP and persist the policy object
*
* @param policy policy object
*/
public void addPolicy(Policy policy) throws APIManagementException {
if (policy instanceof APIPolicy) {
APIPolicy apiPolicy = (APIPolicy) policy;
// Check if there's a policy exists before adding the new policy
Policy existingPolicy = getAPIPolicy(userNameWithoutChange, apiPolicy.getPolicyName());
if (existingPolicy != null) {
handleException("Advanced Policy with name " + apiPolicy.getPolicyName() + " already exists");
}
apiPolicy.setUserLevel(PolicyConstants.ACROSS_ALL);
apiPolicy = apiMgtDAO.addAPIPolicy(apiPolicy);
List<Integer> addedConditionGroupIds = new ArrayList<>();
for (Pipeline pipeline : apiPolicy.getPipelines()) {
addedConditionGroupIds.add(pipeline.getId());
}
APIPolicyEvent apiPolicyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, apiPolicy.getTenantDomain(), apiPolicy.getPolicyId(), apiPolicy.getPolicyName(), apiPolicy.getDefaultQuotaPolicy().getType(), addedConditionGroupIds, null);
APIUtil.sendNotification(apiPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (policy instanceof ApplicationPolicy) {
ApplicationPolicy appPolicy = (ApplicationPolicy) policy;
// Check if there's a policy exists before adding the new policy
Policy existingPolicy = getApplicationPolicy(userNameWithoutChange, appPolicy.getPolicyName());
if (existingPolicy != null) {
handleException("Application Policy with name " + appPolicy.getPolicyName() + " already exists");
}
apiMgtDAO.addApplicationPolicy(appPolicy);
// policy id is not set. retrieving policy to get the id.
ApplicationPolicy retrievedPolicy = apiMgtDAO.getApplicationPolicy(appPolicy.getPolicyName(), tenantId);
ApplicationPolicyEvent applicationPolicyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, appPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), appPolicy.getPolicyName(), appPolicy.getDefaultQuotaPolicy().getType());
APIUtil.sendNotification(applicationPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (policy instanceof SubscriptionPolicy) {
SubscriptionPolicy subPolicy = (SubscriptionPolicy) policy;
// Check if there's a policy exists before adding the new policy
Policy existingPolicy = getSubscriptionPolicy(userNameWithoutChange, subPolicy.getPolicyName());
if (existingPolicy != null) {
handleException("Subscription Policy with name " + subPolicy.getPolicyName() + " already exists");
}
apiMgtDAO.addSubscriptionPolicy(subPolicy);
String monetizationPlan = subPolicy.getMonetizationPlan();
Map<String, String> monetizationPlanProperties = subPolicy.getMonetizationPlanProperties();
if (StringUtils.isNotBlank(monetizationPlan) && MapUtils.isNotEmpty(monetizationPlanProperties)) {
createMonetizationPlan(subPolicy);
}
// policy id is not set. retrieving policy to get the id.
SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(subPolicy.getPolicyName(), tenantId);
SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, subPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), subPolicy.getPolicyName(), subPolicy.getDefaultQuotaPolicy().getType(), subPolicy.getRateLimitCount(), subPolicy.getRateLimitTimeUnit(), subPolicy.isStopOnQuotaReach(), subPolicy.getGraphQLMaxDepth(), subPolicy.getGraphQLMaxComplexity(), subPolicy.getSubscriberCount());
APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (policy instanceof GlobalPolicy) {
GlobalPolicy globalPolicy = (GlobalPolicy) policy;
// checking if policy already exist
Policy existingPolicy = getGlobalPolicy(globalPolicy.getPolicyName());
if (existingPolicy != null) {
throw new APIManagementException("Policy name already exists");
}
apiMgtDAO.addGlobalPolicy(globalPolicy);
publishKeyTemplateEvent(globalPolicy.getKeyTemplate(), "add");
GlobalPolicy retrievedPolicy = apiMgtDAO.getGlobalPolicy(globalPolicy.getPolicyName());
GlobalPolicyEvent globalPolicyEvent = new GlobalPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, globalPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), globalPolicy.getPolicyName());
APIUtil.sendNotification(globalPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else {
String msg = "Policy type " + policy.getClass().getName() + " is not supported";
log.error(msg);
throw new UnsupportedPolicyTypeException(msg);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class AbstractKeyValidationHandler method validate.
private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String apiTenantDomain, int tenantId, SubscriptionDataStore datastore, API api, ApplicationKeyMapping key, Application app, Subscription sub, String keyManager) {
String subscriptionStatus = sub.getSubscriptionState();
String type = key.getKeyType();
if (APIConstants.SubscriptionStatus.BLOCKED.equals(subscriptionStatus)) {
infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
infoDTO.setAuthorized(false);
return infoDTO;
} else if (APIConstants.SubscriptionStatus.ON_HOLD.equals(subscriptionStatus) || APIConstants.SubscriptionStatus.REJECTED.equals(subscriptionStatus)) {
infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.SUBSCRIPTION_INACTIVE);
infoDTO.setAuthorized(false);
return infoDTO;
} else if (APIConstants.SubscriptionStatus.PROD_ONLY_BLOCKED.equals(subscriptionStatus) && !APIConstants.API_KEY_TYPE_SANDBOX.equals(type)) {
infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
infoDTO.setType(type);
infoDTO.setAuthorized(false);
return infoDTO;
}
infoDTO.setTier(sub.getPolicyId());
infoDTO.setSubscriber(app.getSubName());
infoDTO.setApplicationId(app.getId().toString());
infoDTO.setApiName(api.getApiName());
infoDTO.setApiVersion(api.getApiVersion());
infoDTO.setApiPublisher(api.getApiProvider());
infoDTO.setApplicationName(app.getName());
infoDTO.setApplicationTier(app.getPolicy());
infoDTO.setApplicationUUID(app.getUUID());
infoDTO.setAppAttributes(app.getAttributes());
infoDTO.setType(type);
// Advanced Level Throttling Related Properties
String apiTier = api.getApiTier();
String subscriberTenant = MultitenantUtils.getTenantDomain(app.getSubName());
ApplicationPolicy appPolicy = datastore.getApplicationPolicyByName(app.getPolicy(), APIUtil.getTenantIdFromTenantDomain(app.getOrganization()));
if (appPolicy == null) {
try {
appPolicy = new SubscriptionDataLoaderImpl().getApplicationPolicy(app.getPolicy(), app.getOrganization());
datastore.addOrUpdateApplicationPolicy(appPolicy);
} catch (DataLoadingException e) {
log.error("Error while loading ApplicationPolicy");
}
}
SubscriptionPolicy subPolicy = datastore.getSubscriptionPolicyByName(sub.getPolicyId(), tenantId);
if (subPolicy == null) {
try {
subPolicy = new SubscriptionDataLoaderImpl().getSubscriptionPolicy(sub.getPolicyId(), apiTenantDomain);
datastore.addOrUpdateSubscriptionPolicy(subPolicy);
} catch (DataLoadingException e) {
log.error("Error while loading SubscriptionPolicy");
}
}
ApiPolicy apiPolicy = datastore.getApiPolicyByName(api.getApiTier(), tenantId);
boolean isContentAware = false;
if (appPolicy.isContentAware() || subPolicy.isContentAware() || (apiPolicy != null && apiPolicy.isContentAware())) {
isContentAware = true;
}
infoDTO.setContentAware(isContentAware);
// TODO this must implement as a part of throttling implementation.
int spikeArrest = 0;
String apiLevelThrottlingKey = "api_level_throttling_key";
if (subPolicy.getRateLimitCount() > 0) {
spikeArrest = subPolicy.getRateLimitCount();
}
String spikeArrestUnit = null;
if (subPolicy.getRateLimitTimeUnit() != null) {
spikeArrestUnit = subPolicy.getRateLimitTimeUnit();
}
boolean stopOnQuotaReach = subPolicy.isStopOnQuotaReach();
int graphQLMaxDepth = 0;
if (subPolicy.getGraphQLMaxDepth() > 0) {
graphQLMaxDepth = subPolicy.getGraphQLMaxDepth();
}
int graphQLMaxComplexity = 0;
if (subPolicy.getGraphQLMaxComplexity() > 0) {
graphQLMaxComplexity = subPolicy.getGraphQLMaxComplexity();
}
List<String> list = new ArrayList<String>();
list.add(apiLevelThrottlingKey);
infoDTO.setSpikeArrestLimit(spikeArrest);
infoDTO.setSpikeArrestUnit(spikeArrestUnit);
infoDTO.setStopOnQuotaReach(stopOnQuotaReach);
infoDTO.setSubscriberTenantDomain(subscriberTenant);
infoDTO.setGraphQLMaxDepth(graphQLMaxDepth);
infoDTO.setGraphQLMaxComplexity(graphQLMaxComplexity);
if (apiTier != null && apiTier.trim().length() > 0) {
infoDTO.setApiTier(apiTier);
}
// We also need to set throttling data list associated with given API. This need to have
// policy id and
// condition id list for all throttling tiers associated with this API.
infoDTO.setThrottlingDataList(list);
infoDTO.setAuthorized(true);
return infoDTO;
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class KeyManagerDataServiceImpl method getSubscriptionPolicyFromSubscriptionPolicyEvent.
private SubscriptionPolicy getSubscriptionPolicyFromSubscriptionPolicyEvent(SubscriptionPolicyEvent event) {
SubscriptionPolicy policy = new SubscriptionPolicy();
policy.setId(event.getPolicyId());
policy.setQuotaType(event.getQuotaType());
policy.setRateLimitCount(event.getRateLimitCount());
policy.setRateLimitTimeUnit(event.getRateLimitTimeUnit());
policy.setStopOnQuotaReach(event.isStopOnQuotaReach());
policy.setTenantId(event.getTenantId());
policy.setTierName(event.getPolicyName());
policy.setGraphQLMaxComplexity(event.getGraphQLMaxComplexity());
policy.setGraphQLMaxDepth(event.getGraphQLMaxDepth());
if (log.isDebugEnabled()) {
log.debug("Event: " + event.toString());
log.debug("Converted : " + policy.toString());
}
return policy;
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIUtil method deployRetrievedSubscriptionPolicy.
private static void deployRetrievedSubscriptionPolicy(int tenantId, SubscriptionPolicy retrievedPolicy) {
SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, retrievedPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), retrievedPolicy.getPolicyName(), retrievedPolicy.getDefaultQuotaPolicy().getType(), retrievedPolicy.getRateLimitCount(), retrievedPolicy.getRateLimitTimeUnit(), retrievedPolicy.isStopOnQuotaReach(), retrievedPolicy.getGraphQLMaxDepth(), retrievedPolicy.getGraphQLMaxComplexity(), retrievedPolicy.getSubscriberCount());
APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIUtil method getPolicyByName.
public static Tier getPolicyByName(String policyLevel, String policyName, String organization) throws APIManagementException {
int tenantId = APIUtil.getInternalOrganizationId(organization);
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Policy policy;
if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
policy = apiMgtDAO.getSubscriptionPolicy(policyName, tenantId);
} else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
policy = apiMgtDAO.getAPIPolicy(policyName, tenantId);
} else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
policy = apiMgtDAO.getApplicationPolicy(policyName, tenantId);
} else {
throw new APIManagementException("No such a policy type : " + policyLevel);
}
if (policy != null) {
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policy.getPolicyName())) {
Tier tier = new Tier(policy.getPolicyName());
tier.setDescription(policy.getDescription());
tier.setDisplayName(policy.getDisplayName());
Limit limit = policy.getDefaultQuotaPolicy().getLimit();
tier.setTimeUnit(limit.getTimeUnit());
tier.setUnitTime(limit.getUnitTime());
tier.setQuotaPolicyType(policy.getDefaultQuotaPolicy().getType());
// If the policy is a subscription policy
if (policy instanceof SubscriptionPolicy) {
SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) policy;
tier.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
tier.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
setBillingPlanAndCustomAttributesToTier(subscriptionPolicy, tier);
if (StringUtils.equals(subscriptionPolicy.getBillingPlan(), APIConstants.COMMERCIAL_TIER_PLAN)) {
tier.setMonetizationAttributes(subscriptionPolicy.getMonetizationPlanProperties());
}
}
if (limit instanceof RequestCountLimit) {
RequestCountLimit countLimit = (RequestCountLimit) limit;
tier.setRequestsPerMin(countLimit.getRequestCount());
tier.setRequestCount(countLimit.getRequestCount());
} else if (limit instanceof BandwidthLimit) {
BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
tier.setRequestsPerMin(bandwidthLimit.getDataAmount());
tier.setRequestCount(bandwidthLimit.getDataAmount());
tier.setBandwidthDataUnit(bandwidthLimit.getDataUnit());
} else {
EventCountLimit eventCountLimit = (EventCountLimit) limit;
tier.setRequestCount(eventCountLimit.getEventCount());
tier.setRequestsPerMin(eventCountLimit.getEventCount());
}
if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
tier.setTierPlan(((SubscriptionPolicy) policy).getBillingPlan());
}
return tier;
} else {
if (APIUtil.isEnabledUnlimitedTier()) {
Tier tier = new Tier(policy.getPolicyName());
tier.setDescription(policy.getDescription());
tier.setDisplayName(policy.getDisplayName());
tier.setRequestsPerMin(Integer.MAX_VALUE);
tier.setRequestCount(Integer.MAX_VALUE);
if (isUnlimitedTierPaid(getTenantDomainFromTenantId(tenantId))) {
tier.setTierPlan(APIConstants.COMMERCIAL_TIER_PLAN);
} else {
tier.setTierPlan(APIConstants.BILLING_PLAN_FREE);
}
return tier;
}
}
}
return null;
}
Aggregations