Search in sources :

Example 1 with RateLimit

use of com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit in project service-proxy by membrane.

the class ApiManagementConfiguration method parsePolicies.

private Map<String, Policy> parsePolicies(Map<String, Object> yaml) {
    Map<String, Policy> result = new HashMap<String, Policy>();
    Object policies = yaml.get("policies");
    if (policies == null) {
        log.warn("No policies in policy file");
        return result;
    }
    List<Object> yamlPolicies = (List<Object>) policies;
    for (Object yamlPolicyObj : yamlPolicies) {
        if (yamlPolicyObj == null) {
            continue;
        }
        LinkedHashMap<String, Object> yamlPolicy = (LinkedHashMap<String, Object>) yamlPolicyObj;
        for (Object polObj : yamlPolicy.values()) {
            if (polObj == null) {
                continue;
            }
            LinkedHashMap<String, Object> yamlPolicyDef = (LinkedHashMap<String, Object>) polObj;
            Policy policy = new Policy();
            Object name = yamlPolicyDef.get("id");
            if (name == null) {
                log.warn("Policy object found, but no \"id\" field");
                continue;
            }
            String policyName = (String) name;
            policy.setName(policyName);
            Object serviceProxiesObj = yamlPolicyDef.get("serviceProxy");
            if (serviceProxiesObj == null) {
                log.warn("Policy object found, but no service proxies specified ");
                continue;
            }
            List<String> serviceProxyNames = (List<String>) serviceProxiesObj;
            for (String sp : serviceProxyNames) {
                policy.getServiceProxies().add(sp);
            }
            // Optionals like rateLimit/quota etc. follow
            Object rateLimitObj = yamlPolicyDef.get("rateLimit");
            if (rateLimitObj != null) {
                LinkedHashMap<String, Object> rateLimitData = (LinkedHashMap<String, Object>) rateLimitObj;
                RateLimit rateLimit = new RateLimit();
                int requests = parseInteger(rateLimitData.get("requests"), RateLimit.REQUESTS_DEFAULT);
                int interval = parseInteger(rateLimitData.get("interval"), RateLimit.INTERVAL_DEFAULT);
                rateLimit.setRequests(requests);
                rateLimit.setInterval(interval);
                policy.setRateLimit(rateLimit);
            }
            Object quotaObj = yamlPolicyDef.get("quota");
            if (quotaObj != null) {
                LinkedHashMap<String, Object> quota = (LinkedHashMap<String, Object>) quotaObj;
                Object quotaSizeObj = quota.get("size");
                long quotaNumber = getQuotaNumber(quotaSizeObj);
                int quotaInterval = parseInteger(quota.get("interval"), Quota.INTERVAL_DEFAULT);
                Quota q = new Quota();
                q.setSize(quotaNumber);
                q.setInterval(quotaInterval);
                policy.setQuota(q);
            }
            parseUnauthenticatedField(yamlPolicyDef, policy);
            result.put(policyName, policy);
        }
    }
    return result;
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RateLimit(com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit) Quota(com.predic8.membrane.core.interceptor.apimanagement.policy.Quota)

Aggregations

Policy (com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)1 Quota (com.predic8.membrane.core.interceptor.apimanagement.policy.Quota)1 RateLimit (com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1