X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsa%2Frest%2FBulkApi.java;h=3a7b797f84e6f0e9ba2289e32d525cd4b17e939f;hb=ecd35f117eb0118f9cbd96fe132a1c359214bfc7;hp=2f5a40854bc570352fb369b15eadc2727a99834c;hpb=d6348739c632fb69c8833078effbb902adc7f702;p=aai%2Fsearch-data-service.git diff --git a/src/main/java/org/onap/aai/sa/rest/BulkApi.java b/src/main/java/org/onap/aai/sa/rest/BulkApi.java index 2f5a408..3a7b797 100644 --- a/src/main/java/org/onap/aai/sa/rest/BulkApi.java +++ b/src/main/java/org/onap/aai/sa/rest/BulkApi.java @@ -1,4 +1,4 @@ -/** +/** * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ @@ -23,232 +23,188 @@ package org.onap.aai.sa.rest; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.fge.jsonschema.main.JsonSchema; import com.github.fge.jsonschema.main.JsonSchemaFactory; - -import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface; -import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException; -import org.onap.aai.sa.searchdbabstraction.entity.OperationResult; -import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.servlet.http.HttpServletRequest; import org.onap.aai.cl.api.LogFields; import org.onap.aai.cl.api.LogLine; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; - -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.Path; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.Response; +import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface; +import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException; +import org.onap.aai.sa.searchdbabstraction.entity.OperationResult; +import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; /** - * This class encapsulates the REST end points associated with performing - * bulk operations against the document store. + * This class encapsulates the REST end points associated with performing bulk operations against the document store. */ -@Path("/bulk") public class BulkApi { - /** - * Indicates whether or not we have performed the one-time static - * initialization required for performing schema validation. - */ - protected static AtomicBoolean validationInitialized = new AtomicBoolean(false); - - /** - * Factory used for importing our payload schema for validation purposes. - */ - protected static JsonSchemaFactory schemaFactory = null; - - /** - * Imported payload schema that will be used by our validation methods. - */ - protected static JsonSchema schema = null; - - protected SearchServiceApi searchService = null; - - // Instantiate the loggers. - private static Logger logger = LoggerFactory.getInstance().getLogger(BulkApi.class.getName()); - private static Logger auditLogger = LoggerFactory.getInstance() - .getAuditLogger(BulkApi.class.getName()); - - - /** - * Create a new instance of the BulkApi end point. - */ - public BulkApi(SearchServiceApi searchService) { - this.searchService = searchService; - } - - - /** - * Processes client requests containing a set of operations to be - * performed in bulk. - * - *
Method: POST - * - * @param operations - JSON structure enumerating the operations to be - * performed. - * @param request - Raw HTTP request. - * @param headers - HTTP headers. - * @return - A standard REST response structure. - */ - public Response processPost(String operations, - HttpServletRequest request, - HttpHeaders headers, - DocumentStoreInterface documentStore, - ApiUtils apiUtils) { - - - // Initialize the MDC Context for logging purposes. - ApiUtils.initMdcContext(request, headers); - - // Set a default result code and entity string for the request. - int resultCode = 500; - String resultString = "Unexpected error"; - - if (logger.isDebugEnabled()) { - logger.debug("SEARCH: Process Bulk Request - operations = [" - + operations.replaceAll("\n", "") + " ]"); - } - - try { - - // Validate that the request is correctly authenticated before going - // any further. - if (!searchService.validateRequest(headers, request, - ApiUtils.Action.POST, ApiUtils.SEARCH_AUTH_POLICY_NAME)) { - logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Authentication failure."); - - return buildResponse(Response.Status.FORBIDDEN.getStatusCode(), - "Authentication failure.", request, apiUtils); - } - - } catch (Exception e) { - - // This is a catch all for any unexpected failure trying to perform - // the authentication. - logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, - "Unexpected authentication failure - cause: " + e.getMessage()); - if (logger.isDebugEnabled()) { - logger.debug("Stack Trace:\n" + e.getStackTrace()); - } - - return buildResponse(Response.Status.FORBIDDEN.getStatusCode(), - "Authentication failure - cause " + e.getMessage(), - request, - apiUtils); - } - - // We expect a payload containing a JSON structure enumerating the - // operations to be performed. - if (operations == null) { - logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Missing operations list payload"); - - return buildResponse(resultCode, "Missing payload", request, apiUtils); - } - + /** + * Indicates whether or not we have performed the one-time static initialization required for performing schema + * validation. + */ + protected static AtomicBoolean validationInitialized = new AtomicBoolean(false); - // Marshal the supplied json string into a Java object. - ObjectMapper mapper = new ObjectMapper(); - BulkRequest[] requests = null; - try { - requests = mapper.readValue(operations, BulkRequest[].class); + /** + * Factory used for importing our payload schema for validation purposes. + */ + protected static JsonSchemaFactory schemaFactory = null; - } catch (IOException e) { + /** + * Imported payload schema that will be used by our validation methods. + */ + protected static JsonSchema schema = null; - logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, - "Failed to marshal operations list: " + e.getMessage()); - if (logger.isDebugEnabled()) { - logger.debug("Stack Trace:\n" + e.getStackTrace()); - } + protected SearchServiceApi searchService = null; - // Populate the result code and entity string for our HTTP response - // and return the response to the client.. - return buildResponse(javax.ws.rs.core.Response.Status.BAD_REQUEST.getStatusCode(), - "Unable to marshal operations: " + e.getMessage(), - request, - apiUtils); - } + // Instantiate the loggers. + private static Logger logger = LoggerFactory.getInstance().getLogger(BulkApi.class.getName()); + private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(BulkApi.class.getName()); - // Verify that our parsed operations list actually contains some valid - // operations. - if (requests.length == 0) { - logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, "Empty operations list in bulk request"); + private static final String MSG_STACK_TRACE = "Stack Trace:\n"; - - // Populate the result code and entity string for our HTTP response - // and return the response to the client.. - return buildResponse(javax.ws.rs.core.Response.Status.BAD_REQUEST.getStatusCode(), - "Empty operations list in bulk request", - request, - apiUtils); + /** + * Create a new instance of the BulkApi end point. + */ + public BulkApi(SearchServiceApi searchService) { + this.searchService = searchService; } - try { - - // Now, forward the set of bulk operations to the DAO for processing. - OperationResult result = documentStore.performBulkOperations(requests); - // Populate the result code and entity string for our HTTP response. - resultCode = result.getResultCode(); - resultString = (result.getFailureCause() == null) - ? result.getResult() : result.getFailureCause(); - } catch (DocumentStoreOperationException e) { - - logger.warn(SearchDbMsgs.BULK_OPERATION_FAILURE, - "Unexpected failure communicating with document store: " + e.getMessage()); - if (logger.isDebugEnabled()) { - logger.debug("Stack Trace:\n" + e.getStackTrace()); - } - - // Populate the result code and entity string for our HTTP response. - resultCode = javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(); - resultString = "Unexpected failure processing bulk operations: " + e.getMessage(); + /** + * Processes client requests containing a set of operations to be performed in bulk. + * + *
+ * Method: POST
+ *
+ * @param operations - JSON structure enumerating the operations to be performed.
+ * @param request - Raw HTTP request.
+ * @param headers - HTTP headers.
+ * @return - A standard REST response structure.
+ */
+ public ResponseEntity