use of com.predic8.membrane.core.interceptor.apimanagement.policy.Policy 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;
}
use of com.predic8.membrane.core.interceptor.apimanagement.policy.Policy in project service-proxy by membrane.
the class Key method toString.
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Key: ").append(name).append("|");
int c = 1;
for (Policy p : policies) {
builder.append(" [").append(c++).append("] ").append(p.getName());
}
return builder.toString();
}
use of com.predic8.membrane.core.interceptor.apimanagement.policy.Policy in project service-proxy by membrane.
the class AMQuota method addRequestEntry.
private void addRequestEntry(String apiKey, long sizeOfBytes) {
synchronized (keyByteCounter) {
if (!keyByteCounter.containsKey(apiKey)) {
ApiKeyByteCounter value = new ApiKeyByteCounter();
Key key = amc.getKeys().get(apiKey);
for (Policy p : key.getPolicies()) {
value.getPolicyByteCounters().put(p.getName(), new AtomicLong());
}
keyByteCounter.put(apiKey, value);
}
}
ApiKeyByteCounter keyInfo = keyByteCounter.get(apiKey);
for (AtomicLong counter : keyInfo.getPolicyByteCounters().values()) {
counter.addAndGet(sizeOfBytes);
}
}
use of com.predic8.membrane.core.interceptor.apimanagement.policy.Policy in project service-proxy by membrane.
the class AMRateLimiter method fillPolicyCleanupTimes.
private void fillPolicyCleanupTimes() {
policyRateLimits.clear();
for (Policy policy : amc.getPolicies().values()) {
String name = policy.getName();
int requests = policy.getRateLimit().getRequests();
Duration interval = Duration.standardSeconds(policy.getRateLimit().getInterval());
HashSet<String> services = new HashSet<String>(policy.getServiceProxies());
PolicyRateLimit prl = new PolicyRateLimit();
prl.setName(name);
prl.setRequests(requests);
prl.setInterval(interval);
prl.setServices(services);
prl.incrementNextCleanup();
policyRateLimits.put(name, prl);
}
}
use of com.predic8.membrane.core.interceptor.apimanagement.policy.Policy in project service-proxy by membrane.
the class ApiManagementConfigurationTest method testParseYaml.
@Test
public void testParseYaml() throws Exception {
String source = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "\\src\\test\\resources\\apimanagement\\api.yaml")), Charset.defaultCharset());
ApiManagementConfiguration conf = new ApiManagementConfiguration();
conf.setLocation(source);
Map<String, Policy> policies = conf.getPolicies();
for (Policy p : policies.values()) {
System.out.println(p);
}
Map<String, Key> keys = conf.getKeys();
for (Key k : keys.values()) {
System.out.println(k);
}
}
Aggregations