Search in sources :

Example 86 with ApiPolicy

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

the class ThrottlingPolicyTemplateBuilderTest method testGetThrottlePolicyForAPILevel.

@Test
public void testGetThrottlePolicyForAPILevel() throws Exception {
    ApiPolicy policy = TestUtil.getPolicyAPILevel();
    Map<String, String> policyStringArray = templateBuilder.getThrottlePolicyForAPILevel(policy);
    Assert.assertEquals(1, policyStringArray.size());
    String defaultPolicyString = templateBuilder.getThrottlePolicyForAPILevelDefault(policy);
    Assert.assertNotNull(defaultPolicyString);
}
Also used : ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 87 with ApiPolicy

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

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

the class SubscriptionValidationDAO method setCommonProperties.

private void setCommonProperties(Policy policy, ResultSet resultSet) throws SQLException {
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    String prefix = "";
    if (policy instanceof APIPolicy) {
        prefix = "DEFAULT_";
    }
    quotaPolicy.setType(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
    if (quotaPolicy.getType() != null) {
        if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            RequestCountLimit reqLimit = new RequestCountLimit();
            reqLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            reqLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            reqLimit.setRequestCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            quotaPolicy.setLimit(reqLimit);
        } else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            BandwidthLimit bandLimit = new BandwidthLimit();
            bandLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            bandLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            bandLimit.setDataAmount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            bandLimit.setDataUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
            quotaPolicy.setLimit(bandLimit);
        } else if (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
            EventCountLimit eventCountLimit = new EventCountLimit();
            eventCountLimit.setEventCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            eventCountLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            eventCountLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            quotaPolicy.setLimit(eventCountLimit);
        }
        policy.setQuotaPolicy(quotaPolicy);
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.subscription.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 89 with ApiPolicy

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

the class SubscriptionValidationDAO method populateApiPolicyList.

private List<APIPolicy> populateApiPolicyList(ResultSet resultSet) throws SQLException {
    List<APIPolicy> apiPolicies = new ArrayList<>();
    Map<Integer, APIPolicy> temp = new ConcurrentHashMap<>();
    if (resultSet != null) {
        while (resultSet.next()) {
            int policyId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID);
            APIPolicy apiPolicy = temp.get(policyId);
            if (apiPolicy == null) {
                apiPolicy = new APIPolicy();
                apiPolicy.setId(policyId);
                apiPolicy.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
                apiPolicy.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_DEFAULT_QUOTA_POLICY_TYPE));
                apiPolicy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
                String tenantDomain = APIUtil.getTenantDomainFromTenantId(apiPolicy.getTenantId());
                apiPolicy.setTenantDomain(tenantDomain);
                apiPolicy.setApplicableLevel(resultSet.getString(ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                setCommonProperties(apiPolicy, resultSet);
                apiPolicies.add(apiPolicy);
            }
            APIPolicyConditionGroup apiPolicyConditionGroup = new APIPolicyConditionGroup();
            int conditionGroup = resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONDITION_ID);
            apiPolicyConditionGroup.setConditionGroupId(conditionGroup);
            apiPolicyConditionGroup.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
            apiPolicyConditionGroup.setPolicyId(policyId);
            ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
            ConditionGroupDTO conditionGroupDTO = null;
            try {
                conditionGroupDTO = apiMgtDAO.createConditionGroupDTO(conditionGroup);
            } catch (APIManagementException e) {
                log.error("Error while processing api policies for policyId : " + policyId, e);
            }
            ConditionDTO[] conditionDTOS = conditionGroupDTO.getConditions();
            apiPolicyConditionGroup.setConditionDTOS(Arrays.asList(conditionDTOS));
            setCommonProperties(apiPolicyConditionGroup, resultSet);
            apiPolicy.addConditionGroup(apiPolicyConditionGroup);
            temp.put(policyId, apiPolicy);
        }
    }
    return apiPolicies;
}
Also used : APIPolicyConditionGroup(org.wso2.carbon.apimgt.api.model.subscription.APIPolicyConditionGroup) ArrayList(java.util.ArrayList) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) APIPolicy(org.wso2.carbon.apimgt.api.model.subscription.APIPolicy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 90 with ApiPolicy

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

the class APIUtilTierTest method generateApiPolicies.

private APIPolicy[] generateApiPolicies(String[] policyNames) {
    List<APIPolicy> policyList = new ArrayList<APIPolicy>();
    for (String policyName : policyNames) {
        APIPolicy policy = new APIPolicy(policyName);
        QuotaPolicy quotaPolicy = new QuotaPolicy();
        RequestCountLimit countLimit = new RequestCountLimit();
        countLimit.setRequestCount(123);
        quotaPolicy.setLimit(countLimit);
        policy.setDefaultQuotaPolicy(quotaPolicy);
        policy.setDescription(policyName);
        policyList.add(policy);
    }
    APIPolicy[] array = {};
    return policyList.toArray(array);
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) ArrayList(java.util.ArrayList) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) Matchers.anyString(org.mockito.Matchers.anyString)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15