Search in sources :

Example 1 with ExecutionPlanConfiguration

use of org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration in project carbon-apimgt by wso2.

the class PolicyUtilTest method testDeployAllPolicies.

@Test
public void testDeployAllPolicies() throws ExecutionPlanConfigurationException, ThrottlePolicyDeployerException, ExecutionPlanDependencyValidationException {
    ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
    Map<String, ExecutionPlanConfiguration> executionPlanConfigurationMap = new HashMap<>();
    ExecutionPlanConfiguration executionPlanConfiguration = Mockito.mock(ExecutionPlanConfiguration.class);
    executionPlanConfigurationMap.put("sample_policy", executionPlanConfiguration);
    Mockito.when(eventProcessorService.getAllActiveExecutionConfigurations()).thenReturn(executionPlanConfigurationMap);
    SubscriptionPolicyList subscriptionPolicyList = new SubscriptionPolicyList();
    SubscriptionPolicy subscriptionPolicy = TestUtil.getPolicySubLevel();
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    subscriptionPolicies.add(subscriptionPolicy);
    subscriptionPolicyList.setList(subscriptionPolicies);
    Mockito.when(policyRetriever.getAllSubscriptionPolicies()).thenReturn(subscriptionPolicyList);
    ApplicationPolicyList applicationPolicyList = new ApplicationPolicyList();
    ApplicationPolicy applicationPolicy = TestUtil.getPolicyAppLevel();
    List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
    applicationPolicies.add(applicationPolicy);
    applicationPolicyList.setList(applicationPolicies);
    Mockito.when(policyRetriever.getAllApplicationPolicies()).thenReturn(applicationPolicyList);
    ApiPolicyList apiPolicyList = new ApiPolicyList();
    ApiPolicy apiPolicy = TestUtil.getPolicyAPILevel();
    List<ApiPolicy> apiPolicies = new ArrayList<>();
    apiPolicies.add(apiPolicy);
    apiPolicyList.setList(apiPolicies);
    Mockito.when(policyRetriever.getAllApiPolicies()).thenReturn(apiPolicyList);
    GlobalPolicyList globalPolicyList = new GlobalPolicyList();
    GlobalPolicy globalPolicy = TestUtil.getPolicyGlobalLevel();
    List<GlobalPolicy> globalPolicies = new ArrayList<>();
    globalPolicies.add(globalPolicy);
    globalPolicyList.setList(globalPolicies);
    Mockito.when(policyRetriever.getAllGlobalPolicies()).thenReturn(globalPolicyList);
    PolicyUtil.deployAllPolicies();
    Mockito.verify(eventProcessorService, Mockito.times(5)).deployExecutionPlan(Mockito.anyString());
}
Also used : ApplicationPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList) HashMap(java.util.HashMap) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) ArrayList(java.util.ArrayList) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) ExecutionPlanConfiguration(org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) SubscriptionPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList) ApiPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList) GlobalPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with ExecutionPlanConfiguration

use of org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration in project carbon-apimgt by wso2.

the class PolicyUtil method undeployAllPolicies.

/**
 * Undeploy all the throttle policies in the Traffic Manager except the excluded ones.
 */
private static void undeployAllPolicies() {
    APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIMConfiguration();
    EventProcessorService eventProcessorService = ServiceReferenceHolder.getInstance().getEventProcessorService();
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(APIConstants.SUPER_TENANT_DOMAIN, true);
        Map<String, ExecutionPlanConfiguration> executionPlanConfigurationMap = eventProcessorService.getAllActiveExecutionConfigurations();
        // Undeploy all the policies except the skip ones provided
        for (Map.Entry<String, ExecutionPlanConfiguration> pair : executionPlanConfigurationMap.entrySet()) {
            String policyPlanName = pair.getKey();
            boolean skiped = false;
            for (String skipPolicyName : apiManagerConfiguration.getThrottleProperties().getSkipRedeployingPolicies()) {
                if (skipPolicyName.equalsIgnoreCase(policyPlanName)) {
                    skiped = true;
                    break;
                }
            }
            if (!skiped) {
                eventProcessorService.undeployActiveExecutionPlan(policyPlanName);
            }
        }
    } catch (ExecutionPlanConfigurationException e) {
        log.error("Error in removing existing throttle policies", e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ExecutionPlanConfiguration(org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) EventProcessorService(org.wso2.carbon.event.processor.core.EventProcessorService)

Aggregations

HashMap (java.util.HashMap)2 ExecutionPlanConfiguration (org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration)2 ExecutionPlanConfigurationException (org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)1 ApiPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList)1 ApplicationPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy)1 ApplicationPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList)1 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)1 GlobalPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList)1 SubscriptionPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy)1 SubscriptionPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList)1 EventProcessorService (org.wso2.carbon.event.processor.core.EventProcessorService)1