Search in sources :

Example 6 with APIPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent 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());
}
Also used : APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ArrayList(java.util.ArrayList) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with APIPolicyEvent

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

the class PolicyUtilTest method testDeletePolicy_APIType.

@Test
public void testDeletePolicy_APIType() throws ExecutionPlanConfigurationException {
    ApiPolicy policy = TestUtil.getPolicyAPILevel();
    List<Integer> deletedConditionGroupIds = new ArrayList<>();
    deletedConditionGroupIds.add(5);
    deletedConditionGroupIds.add(6);
    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");
    APIPolicyEvent policyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), null, deletedConditionGroupIds);
    PolicyUtil.undeployPolicy(policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(3)).undeployActiveExecutionPlan(Mockito.anyString());
}
Also used : APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with APIPolicyEvent

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

the class PolicyUtilTest method testAddPolicy_APIType.

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

Example 9 with APIPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent 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)

Example 10 with APIPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent 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");
    }
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ArrayList(java.util.ArrayList) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)

Aggregations

APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)10 ArrayList (java.util.ArrayList)7 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)6 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)5 ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)5 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)4 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)4 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)4 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)4 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)4 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)3 ExecutionPlanConfigurationException (org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException)3 Gson (com.google.gson.Gson)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 UnsupportedPolicyTypeException (org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException)2 PolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent)2 SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)2 ApplicationPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy)2