use of org.wso2.carbon.apimgt.core.models.events.PolicyEvent 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.core.models.events.PolicyEvent in project carbon-apimgt by wso2.
the class PolicyUtilTest method testDeletePolicy_SubType.
@Test
public void testDeletePolicy_SubType() throws ExecutionPlanConfigurationException {
SubscriptionPolicy policy = TestUtil.getPolicySubLevel();
SubscriptionPolicyEvent policyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), policy.getRateLimitCount(), policy.getRateLimitTimeUnit(), policy.isStopOnQuotaReach(), policy.getGraphQLMaxDepth(), policy.getGraphQLMaxComplexity(), policy.getSubscriberCount());
Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_SUB + "_" + policy.getName())).thenReturn("EXECUTION_PLAN");
PolicyUtil.undeployPolicy(policyEvent);
Mockito.verify(eventProcessorService, Mockito.times(1)).undeployActiveExecutionPlan(Mockito.anyString());
}
use of org.wso2.carbon.apimgt.core.models.events.PolicyEvent in project carbon-apimgt by wso2.
the class PolicyUtilTest method testUpdatePolicy_APIType.
@Test
public void testUpdatePolicy_APIType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
ApiPolicy policy = TestUtil.getPolicyAPILevel();
List<Integer> deletedConditionGroupIds = new ArrayList<>();
deletedConditionGroupIds.add(5);
deletedConditionGroupIds.add(6);
APIPolicyEvent policyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), null, deletedConditionGroupIds);
ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_1")).thenThrow(executionPlanConfigurationException);
Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_5")).thenReturn("EXECUTION_PLAN");
Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_6")).thenReturn("EXECUTION_PLAN");
Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_default")).thenReturn("EXECUTION_PLAN");
PolicyUtil.deployPolicy(policy, policyEvent);
Mockito.verify(eventProcessorService, Mockito.times(2)).undeployActiveExecutionPlan(Mockito.anyString());
Mockito.verify(eventProcessorService, Mockito.times(1)).editActiveExecutionPlan(Mockito.anyString(), Mockito.anyString());
Mockito.verify(eventProcessorService, Mockito.times(1)).deployExecutionPlan(Mockito.anyString());
}
use of org.wso2.carbon.apimgt.core.models.events.PolicyEvent in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method updatePolicy.
@Override
public void updatePolicy(PolicyValidationData policyValidationData) throws GatewayException {
if (policyValidationData != null) {
PolicyEvent policyEvent = new PolicyEvent(APIMgtConstants.GatewayEventTypes.POLICY_UPDATE);
policyEvent.setId(policyValidationData.getId());
policyEvent.setName(policyValidationData.getName());
policyEvent.setStopOnQuotaReach(policyValidationData.isStopOnQuotaReach());
publishToThrottleTopic(policyEvent);
if (log.isDebugEnabled()) {
log.debug("Policy : " + policyValidationData.getName() + " update event has been successfully " + "published " + "to broker");
}
}
}
use of org.wso2.carbon.apimgt.core.models.events.PolicyEvent 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