diff options
author | rv871f <richard.vondadelszen@amdocs.com> | 2018-03-16 11:59:32 -0400 |
---|---|---|
committer | rv871f <richard.vondadelszen@amdocs.com> | 2018-03-16 12:02:30 -0400 |
commit | 1d1fbefced829fed4f3d6bb4123f9df8c9cb8d3c (patch) | |
tree | 1504fc245a6a7518be0f27a51431091fc3582b14 /src/main | |
parent | d6348739c632fb69c8833078effbb902adc7f702 (diff) |
Adding pass through shema creation
Issue-ID: AAI-888
Change-Id: Ifc094758682b39a5f2c5607bccd418de6e36d841
Signed-off-by: rv871f <richard.vondadelszen@amdocs.com>
Diffstat (limited to 'src/main')
4 files changed, 133 insertions, 5 deletions
diff --git a/src/main/java/org/onap/aai/sa/rest/IndexApi.java b/src/main/java/org/onap/aai/sa/rest/IndexApi.java index af934d1..36570dc 100644 --- a/src/main/java/org/onap/aai/sa/rest/IndexApi.java +++ b/src/main/java/org/onap/aai/sa/rest/IndexApi.java @@ -46,6 +46,8 @@ import javax.ws.rs.core.Response; */ public class IndexApi { + private static final String HEADER_VALIDATION_SUCCESS = "SUCCESS"; + protected SearchServiceApi searchService = null; /** @@ -188,6 +190,39 @@ public class IndexApi { // Finally, return the response. return response; } + + /** + * This function accepts any JSON and will "blindly" write it to the + * document store. + * + * Note, eventually this "dynamic" flow should follow the same JSON-Schema + * validation procedure as the normal create index flow. + * + * @param dynamicSchema - The JSON string that will be sent to the document store. + * @param index - The name of the index to be created. + * @param documentStore - The document store specific interface. + * @return The result of the document store interface's operation. + */ + public Response processCreateDynamicIndex(String dynamicSchema, HttpServletRequest request, + HttpHeaders headers, String index, DocumentStoreInterface documentStore) { + + Response response = null; + + Response validationResponse = validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE); + + if (validationResponse.getStatus() != Response.Status.OK.getStatusCode()) { + response = validationResponse; + } else { + OperationResult result = documentStore.createDynamicIndex(index, dynamicSchema); + + int resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode(); + String resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause(); + + response = Response.status(resultCode).entity(resultString).build(); + } + + return response; + } /** @@ -227,7 +262,6 @@ public class IndexApi { return errorResponse(Response.Status.FORBIDDEN, "Authentication failure.", request); } - try { // Send the request to the document store. response = responseFromOperationResult(documentStore.deleteIndex(index)); @@ -237,8 +271,7 @@ public class IndexApi { .entity(e.getMessage()) .build(); } - - + // Log the result. if ((response.getStatus() >= 200) && (response.getStatus() < 300)) { logger.info(SearchDbMsgs.DELETED_INDEX, index); @@ -372,6 +405,26 @@ public class IndexApi { .entity(msg) .build(); } - - + + /** + * A helper method used for validating/authenticating an incoming request. + * + * @param request - The http request that will be validated. + * @param headers - The http headers that will be validated. + * @param index - The name of the index that the document store request is being made against. + * @param failureMsgEnum - The logging message to be used upon validation failure. + * @return A success or failure response + */ + private Response validateRequest(HttpServletRequest request, HttpHeaders headers, String index, SearchDbMsgs failureMsgEnum) { + try { + if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST, ApiUtils.SEARCH_AUTH_POLICY_NAME)) { + logger.warn(failureMsgEnum, index, "Authentication failure."); + return errorResponse(Response.Status.FORBIDDEN, "Authentication failure.", request); + } + } catch (Exception e) { + logger.warn(failureMsgEnum, index, "Unexpected authentication failure - cause: " + e.getMessage()); + return errorResponse(Response.Status.FORBIDDEN, "Authentication failure.", request); + } + return Response.status(Response.Status.OK).entity(HEADER_VALIDATION_SUCCESS).build(); + } } diff --git a/src/main/java/org/onap/aai/sa/rest/SearchServiceApi.java b/src/main/java/org/onap/aai/sa/rest/SearchServiceApi.java index dc091dd..249f6b1 100644 --- a/src/main/java/org/onap/aai/sa/rest/SearchServiceApi.java +++ b/src/main/java/org/onap/aai/sa/rest/SearchServiceApi.java @@ -86,6 +86,18 @@ public class SearchServiceApi { return indexApi.processCreateIndex(requestBody, request, headers, index, documentStore); } + @PUT + @Path("/indexes/dynamic/{index}") + @Consumes({MediaType.APPLICATION_JSON}) + public Response processCreateDynamicIndex(String requestBody, + @Context HttpServletRequest request, + @Context HttpHeaders headers, + @PathParam("index") String index) { + + // Forward the request to our index API to create the index. + IndexApi indexApi = new IndexApi(this); + return indexApi.processCreateDynamicIndex(requestBody, request, headers, index, documentStore); + } @DELETE @Path("/indexes/{index}") diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java index b0ee35c..2f3350c 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/DocumentStoreInterface.java @@ -33,6 +33,8 @@ public interface DocumentStoreInterface { public OperationResult createIndex(String index, DocumentSchema documentSchema); + public OperationResult createDynamicIndex(String index, String dynamicSchema); + public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException; public DocumentOperationResult createDocument(String indexName, diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java index 3c19610..ef141ec 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/elasticsearch/dao/ElasticSearchHttpController.java @@ -183,6 +183,26 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { return result; } + @Override + public OperationResult createDynamicIndex(String index, String dynamicSchema) { + OperationResult result = new OperationResult(); + result.setResultCode(500); + + try { + result = createTable(index, dynamicSchema); + + // ElasticSearch will return us a 200 code on success when we + // want to report a 201, so translate the result here. + result.setResultCode((result.getResultCode() == 200) ? 201 : result.getResultCode()); + if (isSuccess(result)) { + result.setResult("{\"url\": \"" + ApiUtils.buildIndexUri(index) + "\"}"); + } + } catch (DocumentStoreOperationException e) { + result.setFailureCause("Document store operation failure. Cause: " + e.getMessage()); + } + + return result; + } @Override public OperationResult deleteIndex(String indexName) throws DocumentStoreOperationException { @@ -360,6 +380,47 @@ public class ElasticSearchHttpController implements DocumentStoreInterface { return opResult; } + /** + * Will send the passed in JSON payload to Elasticsearch using the + * provided index name in an attempt to create the index. + * + * @param indexName - The name of the index to be created + * @param settingsAndMappings - The actual JSON object that will define the index + * @return - The operation result of writing into Elasticsearch + * @throws DocumentStoreOperationException + */ + protected OperationResult createTable(String indexName, String settingsAndMappings) throws DocumentStoreOperationException { + OperationResult result = new OperationResult(); + result.setResultCode(500); + result.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT); + + // Grab the current time so we can use it to generate a metrics log. + MdcOverride override = getStartTime(new MdcOverride()); + + String fullUrl = getFullUrl("/" + indexName + "/", false); + HttpURLConnection conn = initializeConnection(fullUrl); + + try { + conn.setRequestMethod("PUT"); + } catch (ProtocolException e) { + shutdownConnection(conn); + throw new DocumentStoreOperationException("Failed to set HTTP request method to PUT.", e); + } + + attachContent(conn, settingsAndMappings); + handleResponse(conn, result); + + // Generate a metrics log so we can track how long the operation took. + metricsLogger.info(SearchDbMsgs.CREATE_INDEX_TIME, + new LogFields() + .setField(LogLine.DefinedFields.RESPONSE_CODE, result.getResultCode()) + .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, result.getResultCode()), + override, + indexName); + + return result; + } + @Override public DocumentOperationResult createDocument(String indexName, DocumentStoreDataEntity document, |