From 944224b1a452ba90913bac6bee842f5a6bd61770 Mon Sep 17 00:00:00 2001 From: "Maharajh, Robby (rx2202)" Date: Mon, 4 Dec 2017 16:43:59 -0500 Subject: Update the aai-resources to verify bulk limits Issue-ID: AAI-529 Change-Id: Ia87e3298ce9c46f3b409587df6e9bd6e2065ec8a Signed-off-by: Maharajh, Robby (rx2202) --- .../main/java/org/onap/aai/rest/BulkConsumer.java | 55 +++++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) (limited to 'aai-resources/src/main/java') diff --git a/aai-resources/src/main/java/org/onap/aai/rest/BulkConsumer.java b/aai-resources/src/main/java/org/onap/aai/rest/BulkConsumer.java index 5591ae3..5ff0e35 100644 --- a/aai-resources/src/main/java/org/onap/aai/rest/BulkConsumer.java +++ b/aai-resources/src/main/java/org/onap/aai/rest/BulkConsumer.java @@ -24,8 +24,11 @@ package org.onap.aai.rest; import java.io.UnsupportedEncodingException; import java.net.URI; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Map.Entry; +import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; @@ -60,6 +63,8 @@ import org.onap.aai.restcore.HttpMethod; import org.onap.aai.restcore.RESTAPI; import org.onap.aai.serialization.engines.QueryStyle; import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import org.onap.aai.util.AAIConfig; +import org.onap.aai.util.AAIConstants; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; @@ -250,12 +255,7 @@ public abstract class BulkConsumer extends RESTAPI { throw new AAIException("AAI_6111", String.format("input payload does not follow %s interface", module)); } JsonArray transactions = transactionsObj.getAsJsonArray(); - if (transactions.size() == 0) { - //case where user sends a validly formatted transactions object but - //which has no actual things in it for A&AI to do anything with - //assuming we should count this as a user error - throw new AAIException("AAI_6118", "payload had no objects to operate on"); - } + validateRequest(transactions); return transactions; } @@ -531,6 +531,49 @@ public abstract class BulkConsumer extends RESTAPI { } } + /** + * Pulls the config value for the limit of operations allowed in a bulk add/process request + * + * @throws AAIException + */ + private int getPayLoadLimit() throws AAIException{ + return Integer.parseInt(AAIConfig.get(AAIConstants.AAI_BULKCONSUMER_LIMIT)); + } + + /** + * Validates the amount of operations in a request payload is allowed + * + * @param transactions - a JsonArray of all the transactions in the request payload + * @throws AAIException + */ + private void validateRequest(JsonArray transactions) throws AAIException{ + if (transactions.size() == 0) { + //case where user sends a validly formatted transactions object but + //which has no actual things in it for A&AI to do anything with + //assuming we should count this as a user error + throw new AAIException("AAI_6118", "payload had no objects to operate on"); + }else if(transactions.size() > getPayLoadLimit()) { + throw new AAIException("AAI_6147", String.format("Payload limit of %s reached, please reduce payload.", getPayLoadLimit())); + } + int operationCount = 0; + int payLoadLimit = getPayLoadLimit(); + for(int i = 0; i < transactions.size(); i++){ + Set> entrySet = transactions.get(i).getAsJsonObject().entrySet(); + Iterator> it = entrySet.iterator(); + while(it.hasNext()){ + Map.Entry element = it.next(); + if(element.getValue() instanceof JsonArray) { + operationCount += ((JsonArray) element.getValue()).size(); + }else{ + operationCount++; + } + } + if(operationCount > payLoadLimit){ + throw new AAIException("AAI_6147", String.format("Payload limit of %s reached, please reduce payload.", payLoadLimit)); + } + } + } + protected abstract String getModule(); protected abstract boolean functionAllowed(HttpMethod method); -- cgit 1.2.3-korg