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;
41 * This class encapsulates the REST end points associated with manipulating indexes in the document store.
43 public class IndexApi {
45 private static final String UNKNOWN_LOG_FIELD_STR = "Unknown";
46 private static final String MSG_UNEXPECTED_AUTHENTICATION_FAILURE_CAUSE =
47 "Unexpected authentication failure - cause: ";
48 private static final String MSG_AUTHENTICATION_FAILURE = "Authentication failure.";
49 private static final String HEADER_VALIDATION_SUCCESS = "SUCCESS";
50 protected SearchServiceApi searchService = null;
53 * Configuration for the custom analyzers that will be used for indexing.
55 protected AnalysisConfiguration analysisConfig;
57 // Set up the loggers.
58 private static Logger logger = LoggerFactory.getInstance().getLogger(IndexApi.class.getName());
59 private static Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(IndexApi.class.getName());
62 public IndexApi(SearchServiceApi searchService) {
63 this.searchService = searchService;
69 * Initializes the end point.
71 * @throws FileNotFoundException
73 * @throws DocumentStoreOperationException
77 // Instantiate our analysis configuration object.
78 analysisConfig = new AnalysisConfiguration();
83 * Processes client requests to create a new index and document type in the document store.
85 * @param documentSchema - The contents of the request body which is expected to be a JSON structure which
86 * corresponds to the schema defined in document.schema.json
87 * @param index - The name of the index to create.
88 * @return - A Standard REST response
90 public ResponseEntity<String> processCreateIndex(String documentSchema, HttpServletRequest request,
91 HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
95 // Initialize the MDC Context for logging purposes.
96 ApiUtils.initMdcContext(request, headers);
98 // Validate that the request is correctly authenticated before going any further.
100 if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
101 ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
102 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, MSG_AUTHENTICATION_FAILURE);
103 return errorResponse(HttpStatus.FORBIDDEN, MSG_AUTHENTICATION_FAILURE, request);
106 } catch (Exception e) {
107 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
108 MSG_UNEXPECTED_AUTHENTICATION_FAILURE_CAUSE + e.getMessage());
109 return errorResponse(HttpStatus.FORBIDDEN, MSG_AUTHENTICATION_FAILURE, request);
113 // We expect a payload containing the document schema. Make sure
115 if (documentSchema == null) {
116 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Missing document schema payload");
117 return errorResponse(HttpStatus.valueOf(resultCode), "Missing payload", request);
123 // Marshal the supplied json string into a document schema object.
124 ObjectMapper mapper = new ObjectMapper();
125 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
127 // Now, ask the DAO to create the index.
128 OperationResult result = documentStore.createIndex(index, schema);
130 // Extract the result code and string from the OperationResult
131 // object so that we can use them to generate a standard REST
133 // Note that we want to return a 201 result code on a successful
134 // create, so if we get back a 200 from the document store,
135 // translate that int a 201.
136 resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
137 resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
139 } catch (com.fasterxml.jackson.core.JsonParseException
140 | com.fasterxml.jackson.databind.JsonMappingException e) {
142 // We were unable to marshal the supplied json string into a valid
143 // document schema, so return an appropriate error response.
144 resultCode = HttpStatus.BAD_REQUEST.value();
145 resultString = "Malformed schema: " + e.getMessage();
147 } catch (IOException e) {
149 // We'll treat this is a general internal error.
150 resultCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
151 resultString = "IO Failure: " + e.getMessage();
154 ResponseEntity<String> response =
155 ResponseEntity.status(resultCode).contentType(MediaType.APPLICATION_JSON).body(resultString);
157 if (ApiUtils.isSuccessStatusCode(response.getStatusCodeValue())) {
158 logger.info(SearchDbMsgs.CREATED_INDEX, index);
160 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, resultString);
163 // Generate our audit log.
164 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
165 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, resultCode).setField(
166 LogLine.DefinedFields.RESPONSE_DESCRIPTION, HttpStatus.valueOf(resultCode).toString()),
167 (request != null) ? request.getMethod() : UNKNOWN_LOG_FIELD_STR,
168 (request != null) ? request.getRequestURL().toString() : UNKNOWN_LOG_FIELD_STR,
169 (request != null) ? request.getRemoteHost() : UNKNOWN_LOG_FIELD_STR,
170 Integer.toString(response.getStatusCodeValue()));
174 // Clear the MDC context so that no other transaction inadvertently
175 // uses our transaction id.
176 ApiUtils.clearMdcContext();
178 // Finally, return the response.
183 * This function accepts any JSON and will "blindly" write it to the document store.
185 * Note, eventually this "dynamic" flow should follow the same JSON-Schema validation procedure as the normal create
188 * @param dynamicSchema - The JSON string that will be sent to the document store.
189 * @param index - The name of the index to be created.
190 * @param documentStore - The document store specific interface.
191 * @return The result of the document store interface's operation.
193 public ResponseEntity<String> processCreateDynamicIndex(String dynamicSchema, HttpServletRequest request,
194 HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
196 ResponseEntity<String> response = null;
198 ResponseEntity<String> validationResponse =
199 validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);
202 if (validationResponse.getStatusCodeValue() != HttpStatus.OK.value()) {
203 response = validationResponse;
205 OperationResult result = documentStore.createDynamicIndex(index, dynamicSchema);
207 int resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
208 String resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
210 response = ResponseEntity.status(resultCode).body(resultString);
217 * Processes a client request to remove an index from the document store. Note that this implicitly deletes all
218 * documents contained within that index.
220 * @param index - The index to be deleted.
221 * @return - A standard REST response.
223 public ResponseEntity<String> processDelete(String index, HttpServletRequest request, HttpHeaders headers,
224 DocumentStoreInterface documentStore) {
226 // Initialize the MDC Context for logging purposes.
227 ApiUtils.initMdcContext(request, headers);
229 ResponseEntity<String> response;
231 // Validate that the request is correctly authenticated before going
234 if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
235 ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
236 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, MSG_AUTHENTICATION_FAILURE);
237 return errorResponse(HttpStatus.FORBIDDEN, MSG_AUTHENTICATION_FAILURE, request);
240 } catch (Exception e) {
241 logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
242 MSG_UNEXPECTED_AUTHENTICATION_FAILURE_CAUSE + e.getMessage());
243 return errorResponse(HttpStatus.FORBIDDEN, MSG_AUTHENTICATION_FAILURE, request);
247 // Send the request to the document store.
248 response = responseFromOperationResult(documentStore.deleteIndex(index));
249 } catch (DocumentStoreOperationException e) {
250 response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON)
251 .body(e.getMessage());
254 if (ApiUtils.isSuccessStatusCode(response.getStatusCodeValue())) {
255 logger.info(SearchDbMsgs.DELETED_INDEX, index);
257 logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, response.getBody());
260 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
261 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatusCodeValue()).setField(
262 LogLine.DefinedFields.RESPONSE_DESCRIPTION, response.getStatusCode().getReasonPhrase()),
263 (request != null) ? request.getMethod() : UNKNOWN_LOG_FIELD_STR,
264 (request != null) ? request.getRequestURL().toString() : UNKNOWN_LOG_FIELD_STR,
265 (request != null) ? request.getRemoteHost() : UNKNOWN_LOG_FIELD_STR,
266 Integer.toString(response.getStatusCodeValue()));
268 // Clear the MDC context so that no other transaction inadvertently uses our transaction id.
269 ApiUtils.clearMdcContext();
276 * This method takes a JSON format document schema and produces a set of field mappings in the form that Elastic
279 * @param documentSchema - A document schema expressed as a JSON string.
280 * @return - A JSON string expressing an Elastic Search mapping configuration.
281 * @throws com.fasterxml.jackson.core.JsonParseException
282 * @throws com.fasterxml.jackson.databind.JsonMappingException
283 * @throws IOException
285 public String generateDocumentMappings(String documentSchema) throws IOException {
287 // Unmarshal the json content into a document schema object.
288 ObjectMapper mapper = new ObjectMapper();
289 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
291 // Now, generate the Elastic Search mapping json and return it.
292 StringBuilder sb = new StringBuilder();
294 sb.append("\"properties\": {");
296 boolean first = true;
297 for (DocumentFieldSchema field : schema.getFields()) {
305 sb.append("\"").append(field.getName()).append("\": {");
307 // The field type is mandatory.
308 sb.append("\"type\": \"").append(field.getDataType()).append("\"");
310 // If the index field was specified, then append it.
311 if (field.getSearchable() != null) {
312 sb.append(", \"index\": \"").append(field.getSearchable() ? "analyzed" : "not_analyzed").append("\"");
315 // If a search analyzer was specified, then append it.
316 if (field.getSearchAnalyzer() != null) {
317 sb.append(", \"search_analyzer\": \"").append(field.getSearchAnalyzer()).append("\"");
320 // If an indexing analyzer was specified, then append it.
321 if (field.getIndexAnalyzer() != null) {
322 sb.append(", \"analyzer\": \"").append(field.getIndexAnalyzer()).append("\"");
324 sb.append(", \"analyzer\": \"").append("whitespace").append("\"");
333 logger.debug("Generated document mappings: " + sb.toString());
335 return sb.toString();
340 * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity} object.
342 * @param result - The {@link OperationResult} to be converted.
343 * @return - The equivalent {@link ResponseEntity} object.
345 public ResponseEntity<String> responseFromOperationResult(OperationResult result) {
347 if (ApiUtils.isSuccessStatusCode(result.getResultCode())) {
348 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
349 .body(result.getResult());
351 if (result.getFailureCause() != null) {
352 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
353 .body(result.getFailureCause());
355 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
356 .body(result.getResult());
361 public ResponseEntity<String> errorResponse(HttpStatus status, String msg, HttpServletRequest request) {
363 // Generate our audit log.
364 auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
365 new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, status.value())
366 .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, status.getReasonPhrase()),
367 (request != null) ? request.getMethod() : UNKNOWN_LOG_FIELD_STR,
368 (request != null) ? request.getRequestURL().toString() : UNKNOWN_LOG_FIELD_STR,
369 (request != null) ? request.getRemoteHost() : UNKNOWN_LOG_FIELD_STR, Integer.toString(status.value()));
371 // Clear the MDC context so that no other transaction inadvertently
372 // uses our transaction id.
373 ApiUtils.clearMdcContext();
375 return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).body(msg);
380 * A helper method used for validating/authenticating an incoming request.
382 * @param request - The http request that will be validated.
383 * @param headers - The http headers that will be validated.
384 * @param index - The name of the index that the document store request is being made against.
385 * @param failureMsgEnum - The logging message to be used upon validation failure.
386 * @return A success or failure response
388 private ResponseEntity<String> validateRequest(HttpServletRequest request, HttpHeaders headers, String index,
389 SearchDbMsgs failureMsgEnum) {
391 if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
392 ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
393 logger.warn(failureMsgEnum, index, MSG_AUTHENTICATION_FAILURE);
394 return errorResponse(HttpStatus.FORBIDDEN, MSG_AUTHENTICATION_FAILURE, request);
396 } catch (Exception e) {
397 logger.warn(failureMsgEnum, index, MSG_UNEXPECTED_AUTHENTICATION_FAILURE_CAUSE + e.getMessage());
398 return errorResponse(HttpStatus.FORBIDDEN, MSG_AUTHENTICATION_FAILURE, request);
400 return ResponseEntity.status(HttpStatus.OK).body(HEADER_VALIDATION_SUCCESS);