Search in sources :

Example 61 with SubscriptionPolicy

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

the class SubscriptionPoliciesApiServiceImpl method subscriptionPoliciesGet.

@Override
public Response subscriptionPoliciesGet(String xWSO2Tenant, String policyName, MessageContext messageContext) {
    SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    if (StringUtils.isNotEmpty(xWSO2Tenant)) {
        if (APIConstants.CHAR_ASTERIX.equals(xWSO2Tenant)) {
            return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies())).build();
        } else if (StringUtils.isNotEmpty(policyName)) {
            List<SubscriptionPolicy> model = new ArrayList<>();
            SubscriptionPolicy subscriptionPolicy = subscriptionValidationDAO.getSubscriptionPolicyByNameForTenant(policyName, xWSO2Tenant);
            if (subscriptionPolicy != null) {
                model.add(subscriptionPolicy);
            }
            return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(model)).build();
        } else {
            return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies(xWSO2Tenant))).build();
        }
    } else {
        if (StringUtils.isNotEmpty(policyName)) {
            return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), "X-WSo2-Tenant header is missing.").build();
        }
    }
    return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies())).build();
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy) List(java.util.List) ArrayList(java.util.ArrayList) SubscriptionValidationDAO(org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)

Example 62 with SubscriptionPolicy

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

the class AbstractKeyValidationHandler method validate.

private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String apiTenantDomain, int tenantId, SubscriptionDataStore datastore, API api, ApplicationKeyMapping key, Application app, Subscription sub, String keyManager) {
    String subscriptionStatus = sub.getSubscriptionState();
    String type = key.getKeyType();
    if (APIConstants.SubscriptionStatus.BLOCKED.equals(subscriptionStatus)) {
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
        infoDTO.setAuthorized(false);
        return infoDTO;
    } else if (APIConstants.SubscriptionStatus.ON_HOLD.equals(subscriptionStatus) || APIConstants.SubscriptionStatus.REJECTED.equals(subscriptionStatus)) {
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.SUBSCRIPTION_INACTIVE);
        infoDTO.setAuthorized(false);
        return infoDTO;
    } else if (APIConstants.SubscriptionStatus.PROD_ONLY_BLOCKED.equals(subscriptionStatus) && !APIConstants.API_KEY_TYPE_SANDBOX.equals(type)) {
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
        infoDTO.setType(type);
        infoDTO.setAuthorized(false);
        return infoDTO;
    }
    infoDTO.setTier(sub.getPolicyId());
    infoDTO.setSubscriber(app.getSubName());
    infoDTO.setApplicationId(app.getId().toString());
    infoDTO.setApiName(api.getApiName());
    infoDTO.setApiVersion(api.getApiVersion());
    infoDTO.setApiPublisher(api.getApiProvider());
    infoDTO.setApplicationName(app.getName());
    infoDTO.setApplicationTier(app.getPolicy());
    infoDTO.setApplicationUUID(app.getUUID());
    infoDTO.setAppAttributes(app.getAttributes());
    infoDTO.setType(type);
    // Advanced Level Throttling Related Properties
    String apiTier = api.getApiTier();
    String subscriberTenant = MultitenantUtils.getTenantDomain(app.getSubName());
    ApplicationPolicy appPolicy = datastore.getApplicationPolicyByName(app.getPolicy(), APIUtil.getTenantIdFromTenantDomain(app.getOrganization()));
    if (appPolicy == null) {
        try {
            appPolicy = new SubscriptionDataLoaderImpl().getApplicationPolicy(app.getPolicy(), app.getOrganization());
            datastore.addOrUpdateApplicationPolicy(appPolicy);
        } catch (DataLoadingException e) {
            log.error("Error while loading ApplicationPolicy");
        }
    }
    SubscriptionPolicy subPolicy = datastore.getSubscriptionPolicyByName(sub.getPolicyId(), tenantId);
    if (subPolicy == null) {
        try {
            subPolicy = new SubscriptionDataLoaderImpl().getSubscriptionPolicy(sub.getPolicyId(), apiTenantDomain);
            datastore.addOrUpdateSubscriptionPolicy(subPolicy);
        } catch (DataLoadingException e) {
            log.error("Error while loading SubscriptionPolicy");
        }
    }
    ApiPolicy apiPolicy = datastore.getApiPolicyByName(api.getApiTier(), tenantId);
    boolean isContentAware = false;
    if (appPolicy.isContentAware() || subPolicy.isContentAware() || (apiPolicy != null && apiPolicy.isContentAware())) {
        isContentAware = true;
    }
    infoDTO.setContentAware(isContentAware);
    // TODO this must implement as a part of throttling implementation.
    int spikeArrest = 0;
    String apiLevelThrottlingKey = "api_level_throttling_key";
    if (subPolicy.getRateLimitCount() > 0) {
        spikeArrest = subPolicy.getRateLimitCount();
    }
    String spikeArrestUnit = null;
    if (subPolicy.getRateLimitTimeUnit() != null) {
        spikeArrestUnit = subPolicy.getRateLimitTimeUnit();
    }
    boolean stopOnQuotaReach = subPolicy.isStopOnQuotaReach();
    int graphQLMaxDepth = 0;
    if (subPolicy.getGraphQLMaxDepth() > 0) {
        graphQLMaxDepth = subPolicy.getGraphQLMaxDepth();
    }
    int graphQLMaxComplexity = 0;
    if (subPolicy.getGraphQLMaxComplexity() > 0) {
        graphQLMaxComplexity = subPolicy.getGraphQLMaxComplexity();
    }
    List<String> list = new ArrayList<String>();
    list.add(apiLevelThrottlingKey);
    infoDTO.setSpikeArrestLimit(spikeArrest);
    infoDTO.setSpikeArrestUnit(spikeArrestUnit);
    infoDTO.setStopOnQuotaReach(stopOnQuotaReach);
    infoDTO.setSubscriberTenantDomain(subscriberTenant);
    infoDTO.setGraphQLMaxDepth(graphQLMaxDepth);
    infoDTO.setGraphQLMaxComplexity(graphQLMaxComplexity);
    if (apiTier != null && apiTier.trim().length() > 0) {
        infoDTO.setApiTier(apiTier);
    }
    // We also need to set throttling data list associated with given API. This need to have
    // policy id and
    // condition id list for all throttling tiers associated with this API.
    infoDTO.setThrottlingDataList(list);
    infoDTO.setAuthorized(true);
    return infoDTO;
}
Also used : SubscriptionDataLoaderImpl(org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl) DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) SubscriptionPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) ArrayList(java.util.ArrayList)

Example 63 with SubscriptionPolicy

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

the class KeyManagerDataServiceImpl method getSubscriptionPolicyFromSubscriptionPolicyEvent.

private SubscriptionPolicy getSubscriptionPolicyFromSubscriptionPolicyEvent(SubscriptionPolicyEvent event) {
    SubscriptionPolicy policy = new SubscriptionPolicy();
    policy.setId(event.getPolicyId());
    policy.setQuotaType(event.getQuotaType());
    policy.setRateLimitCount(event.getRateLimitCount());
    policy.setRateLimitTimeUnit(event.getRateLimitTimeUnit());
    policy.setStopOnQuotaReach(event.isStopOnQuotaReach());
    policy.setTenantId(event.getTenantId());
    policy.setTierName(event.getPolicyName());
    policy.setGraphQLMaxComplexity(event.getGraphQLMaxComplexity());
    policy.setGraphQLMaxDepth(event.getGraphQLMaxDepth());
    if (log.isDebugEnabled()) {
        log.debug("Event: " + event.toString());
        log.debug("Converted : " + policy.toString());
    }
    return policy;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy)

Example 64 with SubscriptionPolicy

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

the class SubscriptionValidationDAO method populateSubscriptionPolicyList.

private List<SubscriptionPolicy> populateSubscriptionPolicyList(ResultSet resultSet) throws SQLException {
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    if (resultSet != null) {
        while (resultSet.next()) {
            SubscriptionPolicy subscriptionPolicyDTO = new SubscriptionPolicy();
            subscriptionPolicyDTO.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
            subscriptionPolicyDTO.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_POLICY_NAME));
            subscriptionPolicyDTO.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
            subscriptionPolicyDTO.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
            String tenantDomain = APIUtil.getTenantDomainFromTenantId(subscriptionPolicyDTO.getTenantId());
            subscriptionPolicyDTO.setTenantDomain(tenantDomain);
            subscriptionPolicyDTO.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
            subscriptionPolicyDTO.setRateLimitTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
            subscriptionPolicyDTO.setStopOnQuotaReach(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
            subscriptionPolicyDTO.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
            subscriptionPolicyDTO.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
            setCommonProperties(subscriptionPolicyDTO, resultSet);
            subscriptionPolicies.add(subscriptionPolicyDTO);
        }
    }
    return subscriptionPolicies;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy) ArrayList(java.util.ArrayList)

Example 65 with SubscriptionPolicy

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

the class SubscriptionValidationDAO method getSubscriptionPolicyByNameForTenant.

/*
     * @param policyName : name of the subscription level throttling policy
     * @return {@link SubscriptionPolicy}
     * */
public SubscriptionPolicy getSubscriptionPolicyByNameForTenant(String policyName, String tenantDomain) {
    if (StringUtils.isNotEmpty(policyName) && StringUtils.isNotEmpty(tenantDomain)) {
        try (Connection conn = APIMgtDBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_SUBSCRIPTION_POLICY_SQL)) {
            int tenantId = 0;
            try {
                tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
            } catch (UserStoreException e) {
                log.error("Error in loading ApplicationPolicy for tenantDomain : " + tenantDomain, e);
            }
            ps.setString(1, policyName);
            ps.setInt(2, tenantId);
            try (ResultSet resultSet = ps.executeQuery()) {
                if (resultSet.next()) {
                    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy();
                    subscriptionPolicy.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
                    subscriptionPolicy.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_POLICY_NAME));
                    subscriptionPolicy.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
                    subscriptionPolicy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
                    subscriptionPolicy.setTenantDomain(APIUtil.getTenantDomainFromTenantId(tenantId));
                    subscriptionPolicy.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
                    subscriptionPolicy.setRateLimitTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
                    subscriptionPolicy.setStopOnQuotaReach(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
                    subscriptionPolicy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
                    subscriptionPolicy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
                    setCommonProperties(subscriptionPolicy, resultSet);
                    return subscriptionPolicy;
                }
            }
        } catch (SQLException e) {
            log.error("Error in retrieving Subscription policy by id : " + policyName + " for " + tenantDomain, e);
        }
    }
    return null;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy) SQLException(java.sql.SQLException) Connection(java.sql.Connection) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

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