use of org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent in project carbon-apimgt by wso2.
the class PolicyUtilTest method testUpdatePolicy_GlobalType.
@Test
public void testUpdatePolicy_GlobalType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenReturn("EXECUTION_PLAN");
GlobalPolicy policy = TestUtil.getPolicyGlobalLevel();
GlobalPolicyEvent policyEvent = new GlobalPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName());
PolicyUtil.deployPolicy(policy, policyEvent);
Mockito.verify(eventProcessorService, Mockito.times(1)).editActiveExecutionPlan(Mockito.anyString(), Mockito.anyString());
}
use of org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent 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.impl.notifier.events.GlobalPolicyEvent in project carbon-apimgt by wso2.
the class APIProviderImpl method updatePolicy.
public void updatePolicy(Policy policy) throws APIManagementException {
String oldKeyTemplate = null;
String newKeyTemplate = null;
if (policy instanceof APIPolicy) {
APIPolicy apiPolicy = (APIPolicy) policy;
apiPolicy.setUserLevel(PolicyConstants.ACROSS_ALL);
// TODO this has done due to update policy method not deleting the second level entries when delete on cascade
// TODO Need to fix appropriately
List<Pipeline> pipelineList = apiPolicy.getPipelines();
if (pipelineList != null && pipelineList.size() != 0) {
Iterator<Pipeline> pipelineIterator = pipelineList.iterator();
while (pipelineIterator.hasNext()) {
Pipeline pipeline = pipelineIterator.next();
if (!pipeline.isEnabled()) {
pipelineIterator.remove();
} else {
if (pipeline.getConditions() != null && pipeline.getConditions().size() != 0) {
Iterator<Condition> conditionIterator = pipeline.getConditions().iterator();
while (conditionIterator.hasNext()) {
Condition condition = conditionIterator.next();
if (JavaUtils.isFalseExplicitly(condition.getConditionEnabled())) {
conditionIterator.remove();
}
}
} else {
pipelineIterator.remove();
}
}
}
}
APIPolicy existingPolicy = apiMgtDAO.getAPIPolicy(policy.getPolicyName(), policy.getTenantId());
apiPolicy = apiMgtDAO.updateAPIPolicy(apiPolicy);
// TODO rename level to resource or appropriate name
APIManagerConfiguration config = getAPIManagerConfiguration();
if (log.isDebugEnabled()) {
log.debug("Calling invalidation cache for API Policy for tenant ");
}
String policyContext = APIConstants.POLICY_CACHE_CONTEXT + "/t/" + apiPolicy.getTenantDomain() + "/";
invalidateResourceCache(policyContext, null, Collections.EMPTY_SET);
List<Integer> addedConditionGroupIds = new ArrayList<>();
List<Integer> deletedConditionGroupIds = new ArrayList<>();
for (Pipeline pipeline : existingPolicy.getPipelines()) {
deletedConditionGroupIds.add(pipeline.getId());
}
for (Pipeline pipeline : apiPolicy.getPipelines()) {
addedConditionGroupIds.add(pipeline.getId());
}
APIPolicyEvent apiPolicyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), tenantId, apiPolicy.getTenantDomain(), apiPolicy.getPolicyId(), apiPolicy.getPolicyName(), apiPolicy.getDefaultQuotaPolicy().getType(), addedConditionGroupIds, deletedConditionGroupIds);
APIUtil.sendNotification(apiPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (policy instanceof ApplicationPolicy) {
ApplicationPolicy appPolicy = (ApplicationPolicy) policy;
apiMgtDAO.updateApplicationPolicy(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_UPDATE.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;
apiMgtDAO.updateSubscriptionPolicy(subPolicy);
String monetizationPlan = subPolicy.getMonetizationPlan();
Map<String, String> monetizationPlanProperties = subPolicy.getMonetizationPlanProperties();
// call the monetization extension point to create plans (if any)
if (StringUtils.isNotBlank(monetizationPlan) && MapUtils.isNotEmpty(monetizationPlanProperties)) {
updateMonetizationPlan(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_UPDATE.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;
// getting key templates before updating database
GlobalPolicy oldGlobalPolicy = apiMgtDAO.getGlobalPolicy(policy.getPolicyName());
oldKeyTemplate = oldGlobalPolicy.getKeyTemplate();
newKeyTemplate = globalPolicy.getKeyTemplate();
apiMgtDAO.updateGlobalPolicy(globalPolicy);
GlobalPolicy retrievedPolicy = apiMgtDAO.getGlobalPolicy(globalPolicy.getPolicyName());
GlobalPolicyEvent globalPolicyEvent = new GlobalPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.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);
}
// publishing keytemplate after update
if (oldKeyTemplate != null && newKeyTemplate != null) {
publishKeyTemplateEvent(oldKeyTemplate, "remove");
publishKeyTemplateEvent(newKeyTemplate, "add");
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent in project carbon-apimgt by wso2.
the class APIProviderImpl method deletePolicy.
/**
* @param username username to recognize the tenant
* @param policyLevel policy level
* @param policyName name of the policy to be deleted
* @throws APIManagementException
*/
public void deletePolicy(String username, String policyLevel, String policyName) throws APIManagementException {
int tenantID = APIUtil.getTenantId(username);
if (PolicyConstants.POLICY_LEVEL_API.equals(policyLevel)) {
// need to load whole policy object to get the pipelines
APIPolicy policy = apiMgtDAO.getAPIPolicy(policyName, APIUtil.getTenantId(username));
List<Integer> deletedConditionGroupIds = new ArrayList<>();
for (Pipeline pipeline : policy.getPipelines()) {
deletedConditionGroupIds.add(pipeline.getId());
}
APIPolicyEvent apiPolicyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), tenantId, policy.getTenantDomain(), policy.getPolicyId(), policy.getPolicyName(), policy.getDefaultQuotaPolicy().getType(), null, deletedConditionGroupIds);
APIUtil.sendNotification(apiPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (PolicyConstants.POLICY_LEVEL_APP.equals(policyLevel)) {
ApplicationPolicy appPolicy = apiMgtDAO.getApplicationPolicy(policyName, tenantID);
ApplicationPolicyEvent applicationPolicyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), tenantId, appPolicy.getTenantDomain(), appPolicy.getPolicyId(), appPolicy.getPolicyName(), appPolicy.getDefaultQuotaPolicy().getType());
APIUtil.sendNotification(applicationPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (PolicyConstants.POLICY_LEVEL_SUB.equals(policyLevel)) {
SubscriptionPolicy subscriptionPolicy = apiMgtDAO.getSubscriptionPolicy(policyName, tenantID);
// call the monetization extension point to delete plans if any
deleteMonetizationPlan(subscriptionPolicy);
SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), tenantId, subscriptionPolicy.getTenantDomain(), subscriptionPolicy.getPolicyId(), subscriptionPolicy.getPolicyName(), subscriptionPolicy.getDefaultQuotaPolicy().getType(), subscriptionPolicy.getRateLimitCount(), subscriptionPolicy.getRateLimitTimeUnit(), subscriptionPolicy.isStopOnQuotaReach(), subscriptionPolicy.getGraphQLMaxDepth(), subscriptionPolicy.getGraphQLMaxComplexity(), subscriptionPolicy.getSubscriberCount());
APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
} else if (PolicyConstants.POLICY_LEVEL_GLOBAL.equals(policyLevel)) {
GlobalPolicy globalPolicy = apiMgtDAO.getGlobalPolicy(policyName);
GlobalPolicyEvent globalPolicyEvent = new GlobalPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), tenantId, globalPolicy.getTenantDomain(), globalPolicy.getPolicyId(), globalPolicy.getPolicyName());
APIUtil.sendNotification(globalPolicyEvent, APIConstants.NotifierType.POLICY.name());
}
GlobalPolicy globalPolicy = null;
if (PolicyConstants.POLICY_LEVEL_GLOBAL.equals(policyLevel)) {
globalPolicy = apiMgtDAO.getGlobalPolicy(policyName);
}
// remove from database
apiMgtDAO.removeThrottlePolicy(policyLevel, policyName, tenantID);
if (globalPolicy != null) {
publishKeyTemplateEvent(globalPolicy.getKeyTemplate(), "remove");
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent in project carbon-apimgt by wso2.
the class ThrottlePolicyJMSMessageListener method handleNotificationMessage.
private void handleNotificationMessage(String eventType, String encodedEvent) {
byte[] eventDecoded = Base64.decodeBase64(encodedEvent);
String eventJson = new String(eventDecoded, StandardCharsets.UTF_8);
if (APIConstants.EventType.POLICY_CREATE.toString().equals(eventType) || APIConstants.EventType.POLICY_UPDATE.toString().equals(eventType) || APIConstants.EventType.POLICY_DELETE.toString().equals(eventType)) {
boolean updatePolicy = APIConstants.EventType.POLICY_CREATE.toString().equals(eventType) || APIConstants.EventType.POLICY_UPDATE.toString().equals(eventType);
boolean deletePolicy = APIConstants.EventType.POLICY_DELETE.toString().equals(eventType);
Runnable task = null;
PolicyEvent event = new Gson().fromJson(eventJson, PolicyEvent.class);
if (event.getPolicyType() == APIConstants.PolicyType.SUBSCRIPTION) {
// handle subscription policies
SubscriptionPolicyEvent policyEvent = new Gson().fromJson(eventJson, SubscriptionPolicyEvent.class);
if (!(APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policyEvent.getPolicyName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED.equalsIgnoreCase(policyEvent.getPolicyName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED.equalsIgnoreCase(policyEvent.getPolicyName()))) {
task = () -> {
try {
if (updatePolicy) {
SubscriptionPolicy subscriptionPolicy = policyRetriever.getSubscriptionPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
PolicyUtil.deployPolicy(subscriptionPolicy, policyEvent);
} else if (deletePolicy) {
PolicyUtil.undeployPolicy(policyEvent);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving subscription policy metadata from the database", e);
}
};
}
} else if (event.getPolicyType() == APIConstants.PolicyType.APPLICATION) {
// handle application policies
ApplicationPolicyEvent policyEvent = new Gson().fromJson(eventJson, ApplicationPolicyEvent.class);
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policyEvent.getPolicyName())) {
task = () -> {
try {
if (updatePolicy) {
ApplicationPolicy applicationPolicy = policyRetriever.getApplicationPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
PolicyUtil.deployPolicy(applicationPolicy, policyEvent);
} else if (deletePolicy) {
PolicyUtil.undeployPolicy(policyEvent);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving application policy metadata from the database", e);
}
};
}
} else if (event.getPolicyType() == APIConstants.PolicyType.API) {
// handle API policies
APIPolicyEvent policyEvent = new Gson().fromJson(eventJson, APIPolicyEvent.class);
if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policyEvent.getPolicyName())) {
task = () -> {
try {
if (updatePolicy) {
ApiPolicy apiPolicy = policyRetriever.getApiPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
PolicyUtil.deployPolicy(apiPolicy, policyEvent);
} else if (deletePolicy) {
PolicyUtil.undeployPolicy(policyEvent);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving API policy metadata from the database", e);
}
};
}
} else if (event.getPolicyType() == APIConstants.PolicyType.GLOBAL) {
// handle global policies
GlobalPolicyEvent policyEvent = new Gson().fromJson(eventJson, GlobalPolicyEvent.class);
task = () -> {
try {
if (updatePolicy) {
GlobalPolicy globalPolicy = policyRetriever.getGlobalPolicy(policyEvent.getPolicyName(), policyEvent.getTenantDomain());
PolicyUtil.deployPolicy(globalPolicy, policyEvent);
} else if (deletePolicy) {
PolicyUtil.undeployPolicy(policyEvent);
}
} catch (ThrottlePolicyDeployerException e) {
log.error("Error in retrieving Global policy metadata from the database", e);
}
};
}
if (task != null) {
policyRetrievalScheduler.schedule(task, 1, TimeUnit.MILLISECONDS);
}
}
}
Aggregations