Search in sources :

Example 6 with Policy

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

the class ApiManagementConfiguration method parsePoliciesForKeys.

private Map<String, Key> parsePoliciesForKeys(Map<String, Object> yaml) {
    Map<String, Key> result = new HashMap<String, Key>();
    // assumption: the yaml is valid
    List<Object> keys = (List<Object>) yaml.get("keys");
    if (keys == null) {
        log.info("No API keys in policy file");
        return result;
    }
    for (Object keyObject : keys) {
        LinkedHashMap<String, Object> key = (LinkedHashMap<String, Object>) keyObject;
        Key keyRes = new Key();
        String keyName = (String) key.get("key");
        keyRes.setName(keyName);
        parseExpiration(key, keyRes);
        List<Object> policiesForKey = (List<Object>) key.get("policies");
        HashSet<Policy> policies = new HashSet<Policy>();
        for (Object polObj : policiesForKey) {
            String policyName = (String) polObj;
            Policy p = this.policies.get(policyName);
            policies.add(p);
        }
        keyRes.setPolicies(policies);
        result.put(keyName, keyRes);
    }
    return result;
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 7 with Policy

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

the class AMQuota method fillPolicyQuotas.

private void fillPolicyQuotas() {
    policyQuotas.clear();
    for (Policy policy : amc.getPolicies().values()) {
        String name = policy.getName();
        long quotaSize = policy.getQuota().getSize();
        int interval = policy.getQuota().getInterval();
        HashSet<String> services = new HashSet<String>(policy.getServiceProxies());
        PolicyQuota pq = new PolicyQuota();
        pq.setName(name);
        pq.setSize(quotaSize);
        pq.setInterval(Duration.standardSeconds(interval));
        pq.incrementNextCleanup();
        pq.setServices(services);
        policyQuotas.put(name, pq);
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) HashSet(java.util.HashSet)

Example 8 with Policy

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

the class AMRateLimiter method addRequestEntry.

private void addRequestEntry(String apiKey) {
    synchronized (keyInformation) {
        if (!keyInformation.containsKey(apiKey)) {
            ApiKeyRequestCounter value = new ApiKeyRequestCounter();
            Key key = amc.getKeys().get(apiKey);
            for (Policy p : key.getPolicies()) {
                value.getPolicyCounters().put(p.getName(), new AtomicInteger());
            }
            keyInformation.put(apiKey, value);
        }
    }
    ApiKeyRequestCounter keyInfo = keyInformation.get(apiKey);
    for (AtomicInteger counter : keyInfo.getPolicyCounters().values()) {
        counter.incrementAndGet();
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Key(com.predic8.membrane.core.interceptor.apimanagement.Key)

Aggregations

Policy (com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)8 Key (com.predic8.membrane.core.interceptor.apimanagement.Key)2 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Quota (com.predic8.membrane.core.interceptor.apimanagement.policy.Quota)1 RateLimit (com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Duration (org.joda.time.Duration)1 Test (org.junit.Test)1