Search in sources :

Example 56 with SubscriptionPolicy

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

the class APIUtil method deployRetrievedSubscriptionPolicy.

private static void deployRetrievedSubscriptionPolicy(int tenantId, SubscriptionPolicy retrievedPolicy) {
    SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, retrievedPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), retrievedPolicy.getPolicyName(), retrievedPolicy.getDefaultQuotaPolicy().getType(), retrievedPolicy.getRateLimitCount(), retrievedPolicy.getRateLimitTimeUnit(), retrievedPolicy.isStopOnQuotaReach(), retrievedPolicy.getGraphQLMaxDepth(), retrievedPolicy.getGraphQLMaxComplexity(), retrievedPolicy.getSubscriberCount());
    APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
}
Also used : SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)

Example 57 with SubscriptionPolicy

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

the class APIUtil method getPolicyByName.

public static Tier getPolicyByName(String policyLevel, String policyName, String organization) throws APIManagementException {
    int tenantId = APIUtil.getInternalOrganizationId(organization);
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    Policy policy;
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        policy = apiMgtDAO.getSubscriptionPolicy(policyName, tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
        policy = apiMgtDAO.getAPIPolicy(policyName, tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
        policy = apiMgtDAO.getApplicationPolicy(policyName, tenantId);
    } else {
        throw new APIManagementException("No such a policy type : " + policyLevel);
    }
    if (policy != null) {
        if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policy.getPolicyName())) {
            Tier tier = new Tier(policy.getPolicyName());
            tier.setDescription(policy.getDescription());
            tier.setDisplayName(policy.getDisplayName());
            Limit limit = policy.getDefaultQuotaPolicy().getLimit();
            tier.setTimeUnit(limit.getTimeUnit());
            tier.setUnitTime(limit.getUnitTime());
            tier.setQuotaPolicyType(policy.getDefaultQuotaPolicy().getType());
            // If the policy is a subscription policy
            if (policy instanceof SubscriptionPolicy) {
                SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) policy;
                tier.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
                tier.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
                setBillingPlanAndCustomAttributesToTier(subscriptionPolicy, tier);
                if (StringUtils.equals(subscriptionPolicy.getBillingPlan(), APIConstants.COMMERCIAL_TIER_PLAN)) {
                    tier.setMonetizationAttributes(subscriptionPolicy.getMonetizationPlanProperties());
                }
            }
            if (limit instanceof RequestCountLimit) {
                RequestCountLimit countLimit = (RequestCountLimit) limit;
                tier.setRequestsPerMin(countLimit.getRequestCount());
                tier.setRequestCount(countLimit.getRequestCount());
            } else if (limit instanceof BandwidthLimit) {
                BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
                tier.setRequestsPerMin(bandwidthLimit.getDataAmount());
                tier.setRequestCount(bandwidthLimit.getDataAmount());
                tier.setBandwidthDataUnit(bandwidthLimit.getDataUnit());
            } else {
                EventCountLimit eventCountLimit = (EventCountLimit) limit;
                tier.setRequestCount(eventCountLimit.getEventCount());
                tier.setRequestsPerMin(eventCountLimit.getEventCount());
            }
            if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
                tier.setTierPlan(((SubscriptionPolicy) policy).getBillingPlan());
            }
            return tier;
        } else {
            if (APIUtil.isEnabledUnlimitedTier()) {
                Tier tier = new Tier(policy.getPolicyName());
                tier.setDescription(policy.getDescription());
                tier.setDisplayName(policy.getDisplayName());
                tier.setRequestsPerMin(Integer.MAX_VALUE);
                tier.setRequestCount(Integer.MAX_VALUE);
                if (isUnlimitedTierPaid(getTenantDomainFromTenantId(tenantId))) {
                    tier.setTierPlan(APIConstants.COMMERCIAL_TIER_PLAN);
                } else {
                    tier.setTierPlan(APIConstants.BILLING_PLAN_FREE);
                }
                return tier;
            }
        }
    }
    return null;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) Limit(org.wso2.carbon.apimgt.api.model.policy.Limit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 58 with SubscriptionPolicy

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

the class APIUtil method getTiersFromPolicies.

public static Map<String, Tier> getTiersFromPolicies(String policyLevel, int tenantId) throws APIManagementException {
    Map<String, Tier> tierMap = new TreeMap<String, Tier>();
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    Policy[] policies;
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getSubscriptionPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getAPIPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getApplicationPolicies(tenantId);
    } else {
        throw new APIManagementException("No such a policy type : " + policyLevel);
    }
    for (Policy policy : policies) {
        if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policy.getPolicyName())) {
            Tier tier = new Tier(policy.getPolicyName());
            tier.setDescription(policy.getDescription());
            tier.setDisplayName(policy.getDisplayName());
            Limit limit = policy.getDefaultQuotaPolicy().getLimit();
            tier.setTimeUnit(limit.getTimeUnit());
            tier.setUnitTime(limit.getUnitTime());
            tier.setQuotaPolicyType(policy.getDefaultQuotaPolicy().getType());
            // If the policy is a subscription policy
            if (policy instanceof SubscriptionPolicy) {
                SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) policy;
                tier.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
                tier.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
                setBillingPlanAndCustomAttributesToTier(subscriptionPolicy, tier);
                if (StringUtils.equals(subscriptionPolicy.getBillingPlan(), APIConstants.COMMERCIAL_TIER_PLAN)) {
                    tier.setMonetizationAttributes(subscriptionPolicy.getMonetizationPlanProperties());
                }
            }
            if (limit instanceof RequestCountLimit) {
                RequestCountLimit countLimit = (RequestCountLimit) limit;
                tier.setRequestsPerMin(countLimit.getRequestCount());
                tier.setRequestCount(countLimit.getRequestCount());
            } else if (limit instanceof BandwidthLimit) {
                BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
                tier.setRequestsPerMin(bandwidthLimit.getDataAmount());
                tier.setRequestCount(bandwidthLimit.getDataAmount());
                tier.setBandwidthDataUnit(bandwidthLimit.getDataUnit());
            } else {
                EventCountLimit eventCountLimit = (EventCountLimit) limit;
                tier.setRequestCount(eventCountLimit.getEventCount());
                tier.setRequestsPerMin(eventCountLimit.getEventCount());
            }
            if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
                tier.setTierPlan(((SubscriptionPolicy) policy).getBillingPlan());
            }
            tierMap.put(policy.getPolicyName(), tier);
        } else {
            if (APIUtil.isEnabledUnlimitedTier()) {
                Tier tier = new Tier(policy.getPolicyName());
                tier.setDescription(policy.getDescription());
                tier.setDisplayName(policy.getDisplayName());
                tier.setRequestsPerMin(Integer.MAX_VALUE);
                tier.setRequestCount(Integer.MAX_VALUE);
                if (isUnlimitedTierPaid(getTenantDomainFromTenantId(tenantId))) {
                    tier.setTierPlan(APIConstants.COMMERCIAL_TIER_PLAN);
                } else {
                    tier.setTierPlan(APIConstants.BILLING_PLAN_FREE);
                }
                tierMap.put(policy.getPolicyName(), tier);
            }
        }
    }
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        tierMap.remove(APIConstants.UNAUTHENTICATED_TIER);
    }
    return tierMap;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) TreeMap(java.util.TreeMap) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Limit(org.wso2.carbon.apimgt.api.model.policy.Limit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 59 with SubscriptionPolicy

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

the class APIUtil method getAPI.

public static API getAPI(GovernanceArtifact artifact) throws APIManagementException {
    API api;
    try {
        String providerName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, apiVersion);
        api = new API(apiIdentifier);
        int apiId = ApiMgtDAO.getInstance().getAPIID(artifact.getId());
        if (apiId == -1) {
            return null;
        }
        // set uuid
        api.setUUID(artifact.getId());
        api.setRating(getAverageRating(apiId));
        api.setThumbnailUrl(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
        api.setStatus(getLcStateFromArtifact(artifact));
        api.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT));
        api.setVisibility(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY));
        api.setVisibleRoles(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES));
        api.setVisibleTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS));
        api.setTransports(artifact.getAttribute(APIConstants.API_OVERVIEW_TRANSPORTS));
        api.setInSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_INSEQUENCE));
        api.setOutSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_OUTSEQUENCE));
        api.setFaultSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_FAULTSEQUENCE));
        api.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
        api.setResponseCache(artifact.getAttribute(APIConstants.API_OVERVIEW_RESPONSE_CACHING));
        api.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
        api.setEnableStore(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ENABLE_STORE)));
        api.setTestKey(artifact.getAttribute(APIConstants.API_OVERVIEW_TESTKEY));
        int cacheTimeout = APIConstants.API_RESPONSE_CACHE_TIMEOUT;
        try {
            cacheTimeout = Integer.parseInt(artifact.getAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT));
        } catch (NumberFormatException e) {
        // ignore
        }
        api.setCacheTimeout(cacheTimeout);
        String apiLevelTier = ApiMgtDAO.getInstance().getAPILevelTier(apiId);
        api.setApiLevelPolicy(apiLevelTier);
        Set<Tier> availablePolicy = new HashSet<Tier>();
        String[] subscriptionPolicy = ApiMgtDAO.getInstance().getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, replaceEmailDomainBack(providerName));
        List<String> definedPolicyNames = Arrays.asList(subscriptionPolicy);
        String policies = artifact.getAttribute(APIConstants.API_OVERVIEW_TIER);
        if (policies != null && !"".equals(policies)) {
            String[] policyNames = policies.split("\\|\\|");
            for (String policyName : policyNames) {
                if (definedPolicyNames.contains(policyName) || APIConstants.UNLIMITED_TIER.equals(policyName)) {
                    Tier p = new Tier(policyName);
                    availablePolicy.add(p);
                } else {
                    log.warn("Unknown policy: " + policyName + " found on API: " + apiName);
                }
            }
        }
        api.addAvailableTiers(availablePolicy);
        String tenantDomainName = MultitenantUtils.getTenantDomain(replaceEmailDomainBack(providerName));
        api.setMonetizationCategory(getAPIMonetizationCategory(availablePolicy, tenantDomainName));
        api.setRedirectURL(artifact.getAttribute(APIConstants.API_OVERVIEW_REDIRECT_URL));
        api.setApiOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_OWNER));
        api.setAdvertiseOnly(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
        api.setEndpointConfig(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG));
        api.setSubscriptionAvailability(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY));
        api.setSubscriptionAvailableTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS));
        api.setAsDefaultVersion(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_IS_DEFAULT_VERSION)));
        api.setImplementation(artifact.getAttribute(APIConstants.PROTOTYPE_OVERVIEW_IMPLEMENTATION));
        api.setTechnicalOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER));
        api.setTechnicalOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER_EMAIL));
        api.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
        api.setBusinessOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER_EMAIL));
        ArrayList<URITemplate> urlPatternsList;
        urlPatternsList = ApiMgtDAO.getInstance().getAllURITemplates(api.getContext(), api.getId().getVersion());
        Set<URITemplate> uriTemplates = new HashSet<URITemplate>(urlPatternsList);
        for (URITemplate uriTemplate : uriTemplates) {
            uriTemplate.setResourceURI(api.getUrl());
            uriTemplate.setResourceSandboxURI(api.getSandboxUrl());
        }
        api.setUriTemplates(uriTemplates);
        String environments = artifact.getAttribute(APIConstants.API_OVERVIEW_ENVIRONMENTS);
        api.setEnvironments(extractEnvironmentsForAPI(environments));
        api.setCorsConfiguration(getCorsConfigurationFromArtifact(artifact));
        api.setAuthorizationHeader(artifact.getAttribute(APIConstants.API_OVERVIEW_AUTHORIZATION_HEADER));
        api.setApiSecurity(artifact.getAttribute(APIConstants.API_OVERVIEW_API_SECURITY));
        // non empty URLs to API object
        try {
            api.setEnvironmentList(extractEnvironmentListForAPI(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG)));
        } catch (ParseException e) {
            String msg = "Failed to parse endpoint config JSON of API: " + apiName + " " + apiVersion;
            log.error(msg, e);
            throw new APIManagementException(msg, e);
        } catch (ClassCastException e) {
            String msg = "Invalid endpoint config JSON found in API: " + apiName + " " + apiVersion;
            log.error(msg, e);
            throw new APIManagementException(msg, e);
        }
    } catch (GovernanceException e) {
        String msg = "Failed to get API from artifact ";
        throw new APIManagementException(msg, e);
    }
    return api;
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ParseException(org.json.simple.parser.ParseException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 60 with SubscriptionPolicy

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

the class APIUtil method addDefaultSuperTenantAdvancedThrottlePolicies.

public static void addDefaultSuperTenantAdvancedThrottlePolicies() throws APIManagementException {
    int tenantId = MultitenantConstants.SUPER_TENANT_ID;
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    /* Check if 'Unlimited' policy is available in AM_POLICY_APPLICATION table, to determine whether the default policies are loaded into the database at lease once.
           If yes, default policies won't be added to database again.
        */
    if (apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_APP, tenantId, APIConstants.DEFAULT_APP_POLICY_UNLIMITED)) {
        log.debug("Default Throttling Policies are not written into the database again, as they were added once at initial server startup");
        return;
    }
    long[] requestCount = new long[] { 50, 20, 10, Integer.MAX_VALUE };
    // Adding application level throttle policies
    String[] appPolicies = new String[] { APIConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, APIConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, APIConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, APIConstants.DEFAULT_APP_POLICY_UNLIMITED };
    String[] appPolicyDecs = new String[] { APIConstants.DEFAULT_APP_POLICY_LARGE_DESC, APIConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, APIConstants.DEFAULT_APP_POLICY_SMALL_DESC, APIConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
    String policyName;
    // Add application level throttle policies
    for (int i = 0; i < appPolicies.length; i++) {
        policyName = appPolicies[i];
        if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_APP, tenantId, policyName)) {
            ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
            applicationPolicy.setDisplayName(policyName);
            applicationPolicy.setDescription(appPolicyDecs[i]);
            applicationPolicy.setTenantId(tenantId);
            applicationPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit();
            requestCountLimit.setRequestCount(requestCount[i]);
            requestCountLimit.setUnitTime(1);
            requestCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            apiMgtDAO.addApplicationPolicy(applicationPolicy);
        }
    }
    // Adding Subscription level policies
    long[] requestCountSubPolicies = new long[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
    String[] subPolicies = new String[] { APIConstants.DEFAULT_SUB_POLICY_GOLD, APIConstants.DEFAULT_SUB_POLICY_SILVER, APIConstants.DEFAULT_SUB_POLICY_BRONZE, APIConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, APIConstants.DEFAULT_SUB_POLICY_UNLIMITED };
    String[] subPolicyDecs = new String[] { APIConstants.DEFAULT_SUB_POLICY_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_SILVER_DESC, APIConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, APIConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, APIConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
    for (int i = 0; i < subPolicies.length; i++) {
        policyName = subPolicies[i];
        if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) {
            SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
            subscriptionPolicy.setDisplayName(policyName);
            subscriptionPolicy.setDescription(subPolicyDecs[i]);
            subscriptionPolicy.setTenantId(tenantId);
            subscriptionPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit();
            requestCountLimit.setRequestCount(requestCountSubPolicies[i]);
            requestCountLimit.setUnitTime(1);
            requestCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            subscriptionPolicy.setStopOnQuotaReach(true);
            subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE);
            apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
        }
    }
    // Adding Event based subscription level policies for async policies (WS & SSE)
    long[] eventCountSubPolicyValues = new long[] { 50000, 25000, 5000, Integer.MAX_VALUE };
    String[] eventCountSubPolicyNames = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED };
    String[] eventCountSubPolicyDescriptions = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED_DESC };
    for (int i = 0; i < eventCountSubPolicyNames.length; i++) {
        policyName = eventCountSubPolicyNames[i];
        if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) {
            SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
            subscriptionPolicy.setDisplayName(policyName);
            subscriptionPolicy.setDescription(eventCountSubPolicyDescriptions[i]);
            subscriptionPolicy.setTenantId(tenantId);
            subscriptionPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            EventCountLimit eventCountLimit = new EventCountLimit();
            eventCountLimit.setEventCount(eventCountSubPolicyValues[i]);
            eventCountLimit.setUnitTime(1);
            eventCountLimit.setTimeUnit(APIConstants.TIME_UNIT_DAY);
            defaultQuotaPolicy.setType(PolicyConstants.EVENT_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(eventCountLimit);
            subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            subscriptionPolicy.setStopOnQuotaReach(true);
            subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE);
            apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
        }
    }
    // Adding Event based Webhooks API specific policies (WEBSUB)
    long[] eventCountWHSubPolicyValues = new long[] { 10000, 5000, 1000, Integer.MAX_VALUE };
    int[] subscriptionCountValues = new int[] { 1000, 500, 100, Integer.MAX_VALUE };
    String[] eventCountWHSubPolicyNames = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED };
    String[] eventCountWHSubPolicyDescriptions = new String[] { APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE_DESC, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED_DESC };
    for (int i = 0; i < eventCountWHSubPolicyNames.length; i++) {
        policyName = eventCountWHSubPolicyNames[i];
        if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) {
            SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
            subscriptionPolicy.setDisplayName(policyName);
            subscriptionPolicy.setDescription(eventCountWHSubPolicyDescriptions[i]);
            subscriptionPolicy.setTenantId(tenantId);
            subscriptionPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            EventCountLimit eventCountLimit = new EventCountLimit();
            eventCountLimit.setEventCount(eventCountWHSubPolicyValues[i]);
            eventCountLimit.setUnitTime(1);
            eventCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MONTH);
            defaultQuotaPolicy.setType(PolicyConstants.EVENT_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(eventCountLimit);
            subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            subscriptionPolicy.setStopOnQuotaReach(true);
            subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE);
            subscriptionPolicy.setSubscriberCount(subscriptionCountValues[i]);
            apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
        }
    }
    // Adding Resource level policies
    String[] apiPolicies = new String[] { APIConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_UNLIMITED };
    String[] apiPolicyDecs = new String[] { APIConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, APIConstants.DEFAULT_API_POLICY_PLUS_DESC, APIConstants.DEFAULT_API_POLICY_BASIC_DESC, APIConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
    long[] requestCountApiPolicies = new long[] { 50000, 20000, 10000, Integer.MAX_VALUE };
    for (int i = 0; i < apiPolicies.length; i++) {
        policyName = apiPolicies[i];
        if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_API, tenantId, policyName)) {
            APIPolicy apiPolicy = new APIPolicy(policyName);
            apiPolicy.setDisplayName(policyName);
            apiPolicy.setDescription(apiPolicyDecs[i]);
            apiPolicy.setTenantId(tenantId);
            apiPolicy.setUserLevel(APIConstants.API_POLICY_API_LEVEL);
            apiPolicy.setDeployed(true);
            QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
            RequestCountLimit requestCountLimit = new RequestCountLimit();
            requestCountLimit.setRequestCount(requestCountApiPolicies[i]);
            requestCountLimit.setUnitTime(1);
            requestCountLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE);
            defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
            defaultQuotaPolicy.setLimit(requestCountLimit);
            apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
            apiMgtDAO.addAPIPolicy(apiPolicy);
        }
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)42 Test (org.testng.annotations.Test)41 ArrayList (java.util.ArrayList)36 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 API (org.wso2.carbon.apimgt.core.models.API)30 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)30 Test (org.junit.Test)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)24 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 SQLException (java.sql.SQLException)15 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)15 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)15