Search in sources :

Example 1 with SubscriptionPolicyList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList 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 SubscriptionPolicyList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.

the class PolicyUtil method deployAllPolicies.

/**
 * Deploy all the throttle policies retrieved from the database in the Traffic Manager.
 */
public static void deployAllPolicies() {
    // Undeploy all existing policies
    undeployAllPolicies();
    PolicyRetriever policyRetriever = new PolicyRetriever();
    try {
        // Deploy all the policies retrieved from the database
        SubscriptionPolicyList subscriptionPolicies = policyRetriever.getAllSubscriptionPolicies();
        for (SubscriptionPolicy subscriptionPolicy : subscriptionPolicies.getList()) {
            if (!(APIConstants.UNLIMITED_TIER.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()) || APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED.equalsIgnoreCase(subscriptionPolicy.getName()))) {
                deployPolicy(subscriptionPolicy, null);
            }
        }
        ApplicationPolicyList applicationPolicies = policyRetriever.getAllApplicationPolicies();
        for (ApplicationPolicy applicationPolicy : applicationPolicies.getList()) {
            if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(applicationPolicy.getName())) {
                deployPolicy(applicationPolicy, null);
            }
        }
        ApiPolicyList apiPolicies = policyRetriever.getAllApiPolicies();
        for (ApiPolicy apiPolicy : apiPolicies.getList()) {
            if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(apiPolicy.getName())) {
                deployPolicy(apiPolicy, null);
            }
        }
        GlobalPolicyList globalPolicies = policyRetriever.getAllGlobalPolicies();
        for (GlobalPolicy globalPolicy : globalPolicies.getList()) {
            deployPolicy(globalPolicy, null);
        }
    } catch (ThrottlePolicyDeployerException e) {
        log.error("Error in retrieving throttle policies", e);
    }
}
Also used : ThrottlePolicyDeployerException(org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException) ApplicationPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) SubscriptionPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ApiPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList) GlobalPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList) PolicyRetriever(org.wso2.carbon.apimgt.throttle.policy.deployer.PolicyRetriever)

Example 3 with SubscriptionPolicyList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.

the class PolicyRetriever method getSubscriptionPolicy.

/**
 * Get a subscription policy given the name.
 *
 * @param policyName   policy name
 * @param tenantDomain tenant domain
 * @return subscription policy
 * @throws ThrottlePolicyDeployerException if failure occurs
 */
public SubscriptionPolicy getSubscriptionPolicy(String policyName, String tenantDomain) throws ThrottlePolicyDeployerException {
    String path = APIConstants.SubscriptionValidationResources.SUBSCRIPTION_POLICIES + "?policyName=" + policyName;
    SubscriptionPolicyList subscriptionPolicyList = getPolicies(path, tenantDomain, SubscriptionPolicyList.class);
    if (subscriptionPolicyList.getList() != null && !subscriptionPolicyList.getList().isEmpty()) {
        return subscriptionPolicyList.getList().get(0);
    }
    return null;
}
Also used : SubscriptionPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList)

Example 4 with SubscriptionPolicyList

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method getSubscriptionPolicy.

@Override
public SubscriptionPolicy getSubscriptionPolicy(String policyName, String tenantDomain) throws DataLoadingException {
    String endPoint = APIConstants.SubscriptionValidationResources.SUBSCRIPTION_POLICIES + "?policyName=" + policyName;
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy();
    if (log.isDebugEnabled()) {
        log.debug("getSubscriptionPolicy for " + policyName + " for tenant " + tenantDomain);
    }
    String responseString;
    try {
        responseString = invokeService(endPoint, tenantDomain);
    } catch (IOException e) {
        String msg = "Error while executing the http client " + endPoint;
        log.error(msg, e);
        throw new DataLoadingException(msg, e);
    }
    if (responseString != null && !responseString.isEmpty()) {
        SubscriptionPolicyList list = new Gson().fromJson(responseString, SubscriptionPolicyList.class);
        if (list.getList() != null && !list.getList().isEmpty()) {
            subscriptionPolicy = list.getList().get(0);
        }
    }
    return subscriptionPolicy;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) SubscriptionPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy) SubscriptionPolicyList(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Aggregations

SubscriptionPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList)3 ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)2 ApiPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList)2 ApplicationPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy)2 ApplicationPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList)2 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)2 GlobalPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList)2 SubscriptionPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy)2 Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 SubscriptionPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy)1 SubscriptionPolicyList (org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicyList)1 DataLoadingException (org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException)1 PolicyRetriever (org.wso2.carbon.apimgt.throttle.policy.deployer.PolicyRetriever)1 ThrottlePolicyDeployerException (org.wso2.carbon.apimgt.throttle.policy.deployer.exception.ThrottlePolicyDeployerException)1 ExecutionPlanConfiguration (org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration)1