Search in sources :

Example 1 with ThrottlingPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO in project carbon-apimgt by wso2.

the class ThrottlingPolicyMappingUtil method setTierPermissions.

/**
 * Fills the tier information on TierDTO
 *
 * @param throttlingPolicyDTO Object Containing throttling policy DTOs
 * @param throttlingPolicy    Throttling Policy object
 * @return ThrottlingPolicyDTO with permission info
 */
public static ThrottlingPolicyDTO setTierPermissions(ThrottlingPolicyDTO throttlingPolicyDTO, Tier throttlingPolicy) {
    ThrottlingPolicyPermissionInfoDTO tierPermission = new ThrottlingPolicyPermissionInfoDTO();
    // If no permission found for the tier, the default permission will be applied
    if (throttlingPolicy.getTierPermission() == null || throttlingPolicy.getTierPermission().getPermissionType() == null) {
        tierPermission.setType(ThrottlingPolicyPermissionInfoDTO.TypeEnum.valueOf("ALLOW"));
        List<String> roles = new ArrayList<>();
        roles.add("Internal/everyone");
        tierPermission.setRoles(roles);
    } else {
        String permissionType = throttlingPolicy.getTierPermission().getPermissionType();
        tierPermission.setType(ThrottlingPolicyPermissionInfoDTO.TypeEnum.valueOf(permissionType.toUpperCase()));
        tierPermission.setRoles(Arrays.asList(throttlingPolicy.getTierPermission().getRoles()));
    }
    throttlingPolicyDTO.setThrottlingPolicyPermissions(tierPermission);
    return throttlingPolicyDTO;
}
Also used : ArrayList(java.util.ArrayList) ThrottlingPolicyPermissionInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ThrottlingPolicyPermissionInfoDTO)

Example 2 with ThrottlingPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO in project carbon-apimgt by wso2.

the class ThrottlingPoliciesApiServiceImpl method getThrottlingPolicyByName.

/**
 * Returns the matched throttling policy to the given policy name
 *
 * @param policyName  name of the throttling policy
 * @param policyLevel throttling policy level (subscription or api)
 * @param ifNoneMatch If-None-Match header value
 * @return ThrottlingPolicyDTO matched to the given throttling policy name
 */
@Override
public Response getThrottlingPolicyByName(String policyName, String policyLevel, String ifNoneMatch, MessageContext messageContext) {
    try {
        String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        ThrottlingPolicyDTO.PolicyLevelEnum policyLevelEnum;
        Tier foundTier = null;
        if (StringUtils.isBlank(policyLevel)) {
            RestApiUtil.handleBadRequest("policyLevel cannot be empty", log);
        }
        // retrieves the tier based on the given tier-level
        if (ThrottlingPolicyDTO.PolicyLevelEnum.SUBSCRIPTION.toString().equals(policyLevel)) {
            foundTier = APIUtil.getPolicyByName(PolicyConstants.POLICY_LEVEL_SUB, policyName, tenantDomain);
            policyLevelEnum = ThrottlingPolicyDTO.PolicyLevelEnum.SUBSCRIPTION;
        } else if (ThrottlingPolicyDTO.PolicyLevelEnum.API.toString().equals(policyLevel)) {
            Map<String, Tier> resourceTiersMap = APIUtil.getTiers(APIConstants.TIER_RESOURCE_TYPE, tenantDomain);
            policyLevelEnum = ThrottlingPolicyDTO.PolicyLevelEnum.API;
            if (resourceTiersMap != null) {
                foundTier = RestApiUtil.findTier(resourceTiersMap.values(), policyName);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError("policyLevel should be one of " + Arrays.toString(ThrottlingPolicyDTO.PolicyLevelEnum.values()), log);
            return null;
        }
        // returns if the tier is found, otherwise send 404
        if (foundTier != null) {
            return Response.ok().entity(ThrottlingPolicyMappingUtil.fromTierToDTO(foundTier, policyLevelEnum.toString())).build();
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_THROTTLING_POLICY, policyName, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving throttling policies";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) ThrottlingPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO) Map(java.util.Map)

Example 3 with ThrottlingPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO in project carbon-apimgt by wso2.

the class ThrottlingPolicyMappingUtil method fromTierListToDTO.

/**
 * Converts a List object of Tiers into a DTO.
 *
 * @param tiers  a list of Tier objects
 * @param limit  max number of objects returned
 * @param offset starting index
 * @return ThrottlingPolicyListDTO object containing ThrottlingPolicyDTOs
 */
public static ThrottlingPolicyListDTO fromTierListToDTO(List<Tier> tiers, String tierLevel, int limit, int offset) {
    ThrottlingPolicyListDTO throttlingPolicyListDTO = new ThrottlingPolicyListDTO();
    List<ThrottlingPolicyDTO> throttlingPolicyListDTOList = throttlingPolicyListDTO.getList();
    if (throttlingPolicyListDTOList == null) {
        throttlingPolicyListDTOList = new ArrayList<>();
        throttlingPolicyListDTO.setList(throttlingPolicyListDTOList);
    }
    // identifying the proper start and end indexes
    int size = tiers.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = Math.min(offset + limit - 1, size - 1);
    for (int i = start; i <= end; i++) {
        Tier tier = tiers.get(i);
        throttlingPolicyListDTOList.add(fromTierToDTO(tier, tierLevel));
    }
    throttlingPolicyListDTO.setCount(throttlingPolicyListDTOList.size());
    return throttlingPolicyListDTO;
}
Also used : ThrottlingPolicyListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyListDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) ThrottlingPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO)

Example 4 with ThrottlingPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO in project carbon-apimgt by wso2.

the class ThrottlingPolicyMappingUtil method fromTierToDTO.

/**
 * Converts a Tier object into ThrottlingPolicyDTO.
 *
 * @param tier      Tier object
 * @param tierLevel tier level (api/application or resource)
 * @return ThrottlingPolicyDTO corresponds to Tier object
 */
public static ThrottlingPolicyDTO fromTierToDTO(Tier tier, String tierLevel) {
    ThrottlingPolicyDTO dto = new ThrottlingPolicyDTO();
    dto.setName(tier.getName());
    dto.setDescription(tier.getDescription());
    if (StringUtils.isEmpty(tier.getDisplayName())) {
        dto.setDisplayName(tier.getName());
    } else {
        dto.setDisplayName(tier.getDisplayName());
    }
    dto.setRequestCount(tier.getRequestCount());
    dto.setUnitTime(tier.getUnitTime());
    dto.setStopOnQuotaReach(tier.isStopOnQuotaReached());
    dto.setPolicyLevel((ThrottlingPolicyDTO.PolicyLevelEnum.fromValue(tierLevel)));
    dto.setTimeUnit(tier.getTimeUnit());
    dto.setRateLimitCount(tier.getRateLimitCount());
    dto.setRateLimitTimeUnit(tier.getRateLimitTimeUnit());
    dto.setDataUnit(tier.getBandwidthDataUnit());
    if (tier.getQuotaPolicyType() != null) {
        dto.setQuotaPolicyType(mapQuotaPolicyTypeFromModeltoDTO(tier.getQuotaPolicyType()));
    }
    if (tier.getTierPlan() != null) {
        dto.setTierPlan(ThrottlingPolicyDTO.TierPlanEnum.fromValue(tier.getTierPlan()));
    }
    if (tier.getTierAttributes() != null) {
        Map<String, String> additionalProperties = new HashMap<>();
        for (String key : tier.getTierAttributes().keySet()) {
            additionalProperties.put(key, tier.getTierAttributes().get(key).toString());
        }
        dto.setAttributes(additionalProperties);
    }
    return dto;
}
Also used : HashMap(java.util.HashMap) ThrottlingPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO)

Example 5 with ThrottlingPolicyDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO in project carbon-apimgt by wso2.

the class ThrottlingPolicyMappingUtil method fromTierListToDTO.

/**
 * Converts a List object of Tiers into a DTO
 *
 * @param throttlingPolicyList a list of Tier objects
 * @param policyLevel          the policy level(eg: application or subscription)
 * @param limit                max number of objects returned
 * @param offset               starting index
 * @return TierListDTO object containing TierDTOs
 */
public static ThrottlingPolicyListDTO fromTierListToDTO(List<Tier> throttlingPolicyList, String policyLevel, int limit, int offset) {
    ThrottlingPolicyListDTO throttlingPolicyListDTO = new ThrottlingPolicyListDTO();
    List<ThrottlingPolicyDTO> throttlingPolicyDTOs = throttlingPolicyListDTO.getList();
    if (throttlingPolicyDTOs == null) {
        throttlingPolicyDTOs = new ArrayList<>();
        throttlingPolicyListDTO.setList(throttlingPolicyDTOs);
    }
    // identifying the proper start and end indexes
    int size = throttlingPolicyList.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Tier tier = throttlingPolicyList.get(i);
        throttlingPolicyDTOs.add(fromTierToDTO(tier, policyLevel));
    }
    throttlingPolicyListDTO.setCount(throttlingPolicyDTOs.size());
    return throttlingPolicyListDTO;
}
Also used : ThrottlingPolicyListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ThrottlingPolicyListDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) ThrottlingPolicyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ThrottlingPolicyDTO)

Aggregations

Tier (org.wso2.carbon.apimgt.api.model.Tier)3 ThrottlingPolicyDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyDTO)3 HashMap (java.util.HashMap)2 ThrottlingPolicyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ThrottlingPolicyDTO)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 ThrottlingPolicyListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottlingPolicyListDTO)1 MonetizationInfoDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.MonetizationInfoDTO)1 ThrottlingPolicyListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ThrottlingPolicyListDTO)1 ThrottlingPolicyPermissionInfoDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ThrottlingPolicyPermissionInfoDTO)1