Search in sources :

Example 1 with ApiPolicyList

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList in project carbon-apimgt by wso2.

the class PolicyRetriever method getApiPolicy.

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

Example 2 with ApiPolicyList

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList in project carbon-apimgt by wso2.

the class SubscriptionDataLoaderImpl method getAPIPolicy.

@Override
public ApiPolicy getAPIPolicy(String policyName, String tenantDomain) throws DataLoadingException {
    String endPoint = APIConstants.SubscriptionValidationResources.API_POLICIES + "?policyName=" + policyName;
    ApiPolicy apiPolicy = new ApiPolicy();
    String responseString;
    if (log.isDebugEnabled()) {
        log.debug("getAPIPolicy for " + policyName + " for tenant " + tenantDomain);
    }
    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()) {
        APIPolicyList list = new Gson().fromJson(responseString, APIPolicyList.class);
        if (list.getList() != null && !list.getList().isEmpty()) {
            apiPolicy = list.getList().get(0);
        }
    }
    return apiPolicy;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) APIPolicyList(org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) Gson(com.google.gson.Gson) IOException(java.io.IOException)

Example 3 with ApiPolicyList

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList in project carbon-apimgt by wso2.

the class SubscriptionDataStoreImpl method initializeLoadingTasks.

public void initializeLoadingTasks() {
    Runnable apiTask = new PopulateTask<>(apiMap, () -> {
        try {
            log.debug("Calling loadAllApis. ");
            List<API> apiList = new SubscriptionDataLoaderImpl().loadAllApis(tenantDomain);
            apiByUUIDMap.clear();
            for (API api : apiList) {
                apiByUUIDMap.put(api.getUuid(), api);
                String key = api.getApiName().concat(":").concat(api.getApiVersion());
                apiNameVersionMap.put(key, api);
            }
            apisInitialized = true;
            return apiList;
        } catch (APIManagementException e) {
            log.error("Exception while loading APIs " + e);
        }
        return null;
    });
    executorService.schedule(apiTask, 0, TimeUnit.SECONDS);
    Runnable subscriptionLoadingTask = new PopulateTask<>(subscriptionMap, () -> {
        try {
            log.debug("Calling loadAllSubscriptions.");
            return new SubscriptionDataLoaderImpl().loadAllSubscriptions(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Subscriptions " + e);
        }
        return null;
    });
    executorService.schedule(subscriptionLoadingTask, 0, TimeUnit.SECONDS);
    Runnable applicationLoadingTask = new PopulateTask<>(applicationMap, () -> {
        try {
            log.debug("Calling loadAllApplications.");
            return new SubscriptionDataLoaderImpl().loadAllApplications(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Applications " + e);
        }
        return null;
    });
    executorService.schedule(applicationLoadingTask, 0, TimeUnit.SECONDS);
    Runnable keyMappingsTask = new PopulateTask<>(applicationKeyMappingMap, () -> {
        try {
            log.debug("Calling loadAllKeyMappings.");
            return new SubscriptionDataLoaderImpl().loadAllKeyMappings(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading ApplicationKeyMapping " + e);
        }
        return null;
    });
    executorService.schedule(keyMappingsTask, 0, TimeUnit.SECONDS);
    Runnable apiPolicyLoadingTask = new PopulateTask<>(apiPolicyMap, () -> {
        try {
            log.debug("Calling loadAllSubscriptionPolicies.");
            List<ApiPolicy> apiPolicyList = new SubscriptionDataLoaderImpl().loadAllAPIPolicies(tenantDomain);
            apiPoliciesInitialized = true;
            return apiPolicyList;
        } catch (APIManagementException e) {
            log.error("Exception while loading api Policies " + e);
        }
        return null;
    });
    executorService.schedule(apiPolicyLoadingTask, 0, TimeUnit.SECONDS);
    Runnable subPolicyLoadingTask = new PopulateTask<>(subscriptionPolicyMap, () -> {
        try {
            log.debug("Calling loadAllSubscriptionPolicies.");
            return new SubscriptionDataLoaderImpl().loadAllSubscriptionPolicies(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Subscription Policies " + e);
        }
        return null;
    });
    executorService.schedule(subPolicyLoadingTask, 0, TimeUnit.SECONDS);
    Runnable appPolicyLoadingTask = new PopulateTask<>(appPolicyMap, () -> {
        try {
            log.debug("Calling loadAllAppPolicies.");
            return new SubscriptionDataLoaderImpl().loadAllAppPolicies(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Application Policies " + e);
        }
        return null;
    });
    executorService.schedule(appPolicyLoadingTask, 0, TimeUnit.SECONDS);
    Runnable scopesLoadingTask = new PopulateTask<>(scopesMap, () -> {
        try {
            log.debug("Calling loadAllScopes.");
            List<Scope> scopeList = new SubscriptionDataLoaderImpl().loadAllScopes(tenantDomain);
            scopesInitialized = true;
            return scopeList;
        } catch (APIManagementException e) {
            log.error("Exception while loading Scopes " + e);
        }
        return null;
    });
    executorService.schedule(scopesLoadingTask, 0, TimeUnit.SECONDS);
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.keymgt.model.entity.Scope) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

Example 4 with ApiPolicyList

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList 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 5 with ApiPolicyList

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList 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)

Aggregations

ApiPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList)3 ApiPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy)2 ApiPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy)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 SubscriptionPolicyList (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList)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 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)1 APIPolicyList (org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyList)1 Scope (org.wso2.carbon.apimgt.keymgt.model.entity.Scope)1 DataLoadingException (org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException)1