2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.rest;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import javax.servlet.http.HttpServletRequest;
27 import org.onap.aai.cl.api.LogFields;
28 import org.onap.aai.cl.api.LogLine;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
31 import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
32 import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
33 import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
34 import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.MediaType;
38 import org.springframework.http.ResponseEntity;
42 * This class encapsulates the REST end points associated with manipulating indexes in the document store.
44 public class IndexApi {
47 private static final String HEADER_VALIDATION_SUCCESS = "SUCCESS";
48 protected SearchServiceApi searchService = null;
51 * Configuration for the custom analyzers that will be used for indexing.
53 protected AnalysisConfiguration analysisConfig;
55 // Set up the loggers.
56 private static Logger logger = LoggerFactory.getInstance().getLogger(IndexApi.class.getName());
57 private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(IndexApi.class.getName());
60 public IndexApi(SearchServiceApi searchService) {
61 this.searchService = searchService;
67 * Initializes the end point.
69 * @throws FileNotFoundException
71 * @throws DocumentStoreOperationException
75 // Instantiate our analysis configuration object.
76 analysisConfig = new AnalysisConfiguration();
81 * Processes client requests to create a new index and document type in the document store.
83 * @param documentSchema - The contents of the request body which is expected to be a JSON structure which
84 * corresponds to the schema defined in document.schema.json
85 * @param index - The name of the index to create.
86 * @return - A Standard REST response
88 public ResponseEntity<String> processCreateIndex(String documentSchema, HttpServletRequest request,
89 HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
92 String resultString = "Unexpected error";
94 // Initialize the MDC Context for logging purposes.
95 ApiUtils.initMdcContext(request, headers);
97 // Validate that the request is correctly authenticated before going
101 if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
102 ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
103 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Authentication failure.");
104 return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
107 } catch (Exception e) {
109 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
110 "Unexpected authentication failure - cause: " + e.getMessage());
111 return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
115 // We expect a payload containing the document schema. Make sure
117 if (documentSchema == null) {
118 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Missing document schema payload");
119 return errorResponse(HttpStatus.valueOf(resultCode), "Missing payload", request);
124 // Marshal the supplied json string into a document schema object.
125 ObjectMapper mapper = new ObjectMapper();
126 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
128 // Now, ask the DAO to create the index.
129 OperationResult result = documentStore.createIndex(index, schema);
131 // Extract the result code and string from the OperationResult
132 // object so that we can use them to generate a standard REST
134 // Note that we want to return a 201 result code on a successful
135 // create, so if we get back a 200 from the document store,
136 // translate that int a 201.
137 resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
138 resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
140 } catch (com.fasterxml.jackson.core.JsonParseException
141 | com.fasterxml.jackson.databind.JsonMappingException e) {
143 // We were unable to marshal the supplied json string into a valid
144 // document schema, so return an appropriate error response.
145 resultCode = HttpStatus.BAD_REQUEST.value();
146 resultString = "Malformed schema: " + e.getMessage();
148 } catch (IOException e) {
150 // We'll treat this is a general internal error.
151 resultCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
152 resultString = "IO Failure: " + e.getMessage();
155 ResponseEntity<String> response =
156 ResponseEntity.status(resultCode).contentType(MediaType.APPLICATION_JSON).body(resultString);
160 if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
161 logger.info(SearchDbMsgs.CREATED_INDEX, index);
163 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, resultString);
166 // Generate our audit log.
167 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
168 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, resultCode).setField(
169 LogLine.DefinedFields.RESPONSE_DESCRIPTION, HttpStatus.valueOf(resultCode).toString()),
170 (request != null) ? request.getMethod().toString() : "Unknown",
171 (request != null) ? request.getRequestURL().toString() : "Unknown",
172 (request != null) ? request.getRemoteHost() : "Unknown",
173 Integer.toString(response.getStatusCodeValue()));
177 // Clear the MDC context so that no other transaction inadvertently
178 // uses our transaction id.
179 ApiUtils.clearMdcContext();
181 // Finally, return the response.
186 * This function accepts any JSON and will "blindly" write it to the document store.
188 * Note, eventually this "dynamic" flow should follow the same JSON-Schema validation procedure as the normal create
191 * @param dynamicSchema - The JSON string that will be sent to the document store.
192 * @param index - The name of the index to be created.
193 * @param documentStore - The document store specific interface.
194 * @return The result of the document store interface's operation.
196 public ResponseEntity<String> processCreateDynamicIndex(String dynamicSchema, HttpServletRequest request,
197 HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
199 ResponseEntity<String> response = null;
201 ResponseEntity<String> validationResponse =
202 validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);
205 if (validationResponse.getStatusCodeValue() != HttpStatus.OK.value()) {
206 response = validationResponse;
208 OperationResult result = documentStore.createDynamicIndex(index, dynamicSchema);
210 int resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
211 String resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
213 response = ResponseEntity.status(resultCode).body(resultString);
220 * Processes a client request to remove an index from the document store. Note that this implicitly deletes all
221 * documents contained within that index.
223 * @param index - The index to be deleted.
224 * @return - A standard REST response.
226 public ResponseEntity<String> processDelete(String index, HttpServletRequest request, HttpHeaders headers,
227 DocumentStoreInterface documentStore) {
229 // Initialize the MDC Context for logging purposes.
230 ApiUtils.initMdcContext(request, headers);
232 // Set a default response in case something unexpected goes wrong.
233 ResponseEntity<String> response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unknown");
235 // Validate that the request is correctly authenticated before going
239 if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
240 ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
241 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Authentication failure.");
242 return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
245 } catch (Exception e) {
247 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
248 "Unexpected authentication failure - cause: " + e.getMessage());
249 return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
254 // Send the request to the document store.
255 response = responseFromOperationResult(documentStore.deleteIndex(index));
257 } catch (DocumentStoreOperationException e) {
258 response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON)
259 .body(e.getMessage());
263 if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
264 logger.info(SearchDbMsgs.DELETED_INDEX, index);
266 logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, response.getBody());
269 // Generate our audit log.
270 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
271 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatusCodeValue()).setField(
272 LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getStatusCode().getReasonPhrase()),
273 (request != null) ? request.getMethod().toString() : "Unknown",
274 (request != null) ? request.getRequestURL().toString() : "Unknown",
275 (request != null) ? request.getRemoteHost() : "Unknown",
276 Integer.toString(response.getStatusCodeValue()));
278 // Clear the MDC context so that no other transaction inadvertently
279 // uses our transaction id.
280 ApiUtils.clearMdcContext();
287 * This method takes a JSON format document schema and produces a set of field mappings in the form that Elastic
290 * @param documentSchema - A document schema expressed as a JSON string.
291 * @return - A JSON string expressing an Elastic Search mapping configuration.
292 * @throws com.fasterxml.jackson.core.JsonParseException
293 * @throws com.fasterxml.jackson.databind.JsonMappingException
294 * @throws IOException
296 public String generateDocumentMappings(String documentSchema) throws com.fasterxml.jackson.core.JsonParseException,
297 com.fasterxml.jackson.databind.JsonMappingException, IOException {
299 // Unmarshal the json content into a document schema object.
300 ObjectMapper mapper = new ObjectMapper();
301 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
303 // Now, generate the Elastic Search mapping json and return it.
304 StringBuilder sb = new StringBuilder();
306 sb.append("\"properties\": {");
308 boolean first = true;
309 for (DocumentFieldSchema field : schema.getFields()) {
317 sb.append("\"").append(field.getName()).append("\": {");
319 // The field type is mandatory.
320 sb.append("\"type\": \"").append(field.getDataType()).append("\"");
322 // If the index field was specified, then append it.
323 if (field.getSearchable() != null) {
324 sb.append(", \"index\": \"").append(field.getSearchable() ? "analyzed" : "not_analyzed").append("\"");
327 // If a search analyzer was specified, then append it.
328 if (field.getSearchAnalyzer() != null) {
329 sb.append(", \"search_analyzer\": \"").append(field.getSearchAnalyzer()).append("\"");
332 // If an indexing analyzer was specified, then append it.
333 if (field.getIndexAnalyzer() != null) {
334 sb.append(", \"analyzer\": \"").append(field.getIndexAnalyzer()).append("\"");
336 sb.append(", \"analyzer\": \"").append("whitespace").append("\"");
345 logger.debug("Generated document mappings: " + sb.toString());
347 return sb.toString();
352 * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity} object.
354 * @param result - The {@link OperationResult} to be converted.
355 * @return - The equivalent {@link ResponseEntity} object.
357 public ResponseEntity<String> responseFromOperationResult(OperationResult result) {
359 if ((result.getResultCode() >= 200) && (result.getResultCode() < 300)) {
360 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
361 .body(result.getResult());
363 if (result.getFailureCause() != null) {
364 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
365 .body(result.getFailureCause());
367 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
368 .body(result.getResult());
373 public ResponseEntity<String> errorResponse(HttpStatus status, String msg, HttpServletRequest request) {
375 // Generate our audit log.
376 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
377 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, status.value())
378 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, status.getReasonPhrase()),
379 (request != null) ? request.getMethod().toString() : "Unknown",
380 (request != null) ? request.getRequestURL().toString() : "Unknown",
381 (request != null) ? request.getRemoteHost() : "Unknown", Integer.toString(status.value()));
383 // Clear the MDC context so that no other transaction inadvertently
384 // uses our transaction id.
385 ApiUtils.clearMdcContext();
387 return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).body(msg);
392 * A helper method used for validating/authenticating an incoming request.
394 * @param request - The http request that will be validated.
395 * @param headers - The http headers that will be validated.
396 * @param index - The name of the index that the document store request is being made against.
397 * @param failureMsgEnum - The logging message to be used upon validation failure.
398 * @return A success or failure response
400 private ResponseEntity<String> validateRequest(HttpServletRequest request, HttpHeaders headers, String index,
401 SearchDbMsgs failureMsgEnum) {
403 if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
404 ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
405 logger.warn(failureMsgEnum, index, "Authentication failure.");
406 return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
408 } catch (Exception e) {
409 logger.warn(failureMsgEnum, index, "Unexpected authentication failure - cause: " + e.getMessage());
410 return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
412 return ResponseEntity.status(HttpStatus.OK).body(HEADER_VALIDATION_SUCCESS);