Search in sources :

Example 16 with PolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent in project carbon-apimgt by wso2.

the class PolicyUtilTest method testAddPolicy_APPType.

@Test
public void testAddPolicy_APPType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
    ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
    ApplicationPolicy policy = TestUtil.getPolicyAppLevel();
    ApplicationPolicyEvent policyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType());
    PolicyUtil.deployPolicy(policy, policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(1)).deployExecutionPlan(Mockito.anyString());
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with PolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent in project carbon-apimgt by wso2.

the class PolicyUtilTest method testDeletePolicy_AppType.

@Test
public void testDeletePolicy_AppType() throws ExecutionPlanConfigurationException {
    ApplicationPolicy policy = TestUtil.getPolicyAppLevel();
    ApplicationPolicyEvent policyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType());
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_APP + "_" + policy.getName())).thenReturn("EXECUTION_PLAN");
    PolicyUtil.undeployPolicy(policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(1)).undeployActiveExecutionPlan(Mockito.anyString());
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with PolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.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);
        }
    }
}
Also used : ThrottlePolicyDeployerException(org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) Gson(com.google.gson.Gson) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) PolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)

Aggregations

Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)6 ExecutionPlanConfigurationException (org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException)6 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)5 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)5 ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)5 ApplicationPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy)5 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)5 SubscriptionPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy)5 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)4 ArrayList (java.util.ArrayList)3 PolicyEvent (org.wso2.carbon.apimgt.core.models.events.PolicyEvent)3 Gson (com.google.gson.Gson)2 PolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 EndpointCertificateDeployer (org.wso2.carbon.apimgt.gateway.EndpointCertificateDeployer)1