Fix file formatting issues
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / rest / IndexApi.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sa.rest;
22
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;
39
40 /**
41  * This class encapsulates the REST end points associated with manipulating indexes in the document store.
42  */
43 public class IndexApi {
44
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;
51
52     /**
53      * Configuration for the custom analyzers that will be used for indexing.
54      */
55     protected AnalysisConfiguration analysisConfig;
56
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());
60
61
62     public IndexApi(SearchServiceApi searchService) {
63         this.searchService = searchService;
64         init();
65     }
66
67
68     /**
69      * Initializes the end point.
70      *
71      * @throws FileNotFoundException
72      * @throws IOException
73      * @throws DocumentStoreOperationException
74      */
75     public void init() {
76
77         // Instantiate our analysis configuration object.
78         analysisConfig = new AnalysisConfiguration();
79     }
80
81
82     /**
83      * Processes client requests to create a new index and document type in the document store.
84      *
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
89      */
90     public ResponseEntity<String> processCreateIndex(String documentSchema, HttpServletRequest request,
91             HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
92
93         int resultCode = 500;
94
95         // Initialize the MDC Context for logging purposes.
96         ApiUtils.initMdcContext(request, headers);
97
98         // Validate that the request is correctly authenticated before going any further.
99         try {
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);
104             }
105
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);
110         }
111
112
113         // We expect a payload containing the document schema. Make sure
114         // it is present.
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);
118         }
119
120         String resultString;
121
122         try {
123             // Marshal the supplied json string into a document schema object.
124             ObjectMapper mapper = new ObjectMapper();
125             DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
126
127             // Now, ask the DAO to create the index.
128             OperationResult result = documentStore.createIndex(index, schema);
129
130             // Extract the result code and string from the OperationResult
131             // object so that we can use them to generate a standard REST
132             // response.
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();
138
139         } catch (com.fasterxml.jackson.core.JsonParseException
140                 | com.fasterxml.jackson.databind.JsonMappingException e) {
141
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();
146
147         } catch (IOException e) {
148
149             // We'll treat this is a general internal error.
150             resultCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
151             resultString = "IO Failure: " + e.getMessage();
152         }
153
154         ResponseEntity<String> response =
155                 ResponseEntity.status(resultCode).contentType(MediaType.APPLICATION_JSON).body(resultString);
156
157         if (ApiUtils.isSuccessStatusCode(response.getStatusCodeValue())) {
158             logger.info(SearchDbMsgs.CREATED_INDEX, index);
159         } else {
160             logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, resultString);
161         }
162
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()));
171
172
173
174         // Clear the MDC context so that no other transaction inadvertently
175         // uses our transaction id.
176         ApiUtils.clearMdcContext();
177
178         // Finally, return the response.
179         return response;
180     }
181
182     /**
183      * This function accepts any JSON and will "blindly" write it to the document store.
184      *
185      * Note, eventually this "dynamic" flow should follow the same JSON-Schema validation procedure as the normal create
186      * index flow.
187      *
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.
192      */
193     public ResponseEntity<String> processCreateDynamicIndex(String dynamicSchema, HttpServletRequest request,
194             HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
195
196         ResponseEntity<String> response = null;
197
198         ResponseEntity<String> validationResponse =
199                 validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);
200
201
202         if (validationResponse.getStatusCodeValue() != HttpStatus.OK.value()) {
203             response = validationResponse;
204         } else {
205             OperationResult result = documentStore.createDynamicIndex(index, dynamicSchema);
206
207             int resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
208             String resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
209
210             response = ResponseEntity.status(resultCode).body(resultString);
211         }
212
213         return response;
214     }
215
216     /**
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.
219      *
220      * @param index - The index to be deleted.
221      * @return - A standard REST response.
222      */
223     public ResponseEntity<String> processDelete(String index, HttpServletRequest request, HttpHeaders headers,
224             DocumentStoreInterface documentStore) {
225
226         // Initialize the MDC Context for logging purposes.
227         ApiUtils.initMdcContext(request, headers);
228
229         ResponseEntity<String> response;
230
231         // Validate that the request is correctly authenticated before going
232         // any further.
233         try {
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);
238             }
239
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);
244         }
245
246         try {
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());
252         }
253
254         if (ApiUtils.isSuccessStatusCode(response.getStatusCodeValue())) {
255             logger.info(SearchDbMsgs.DELETED_INDEX, index);
256         } else {
257             logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, response.getBody());
258         }
259
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()));
267
268         // Clear the MDC context so that no other transaction inadvertently uses our transaction id.
269         ApiUtils.clearMdcContext();
270
271         return response;
272     }
273
274
275     /**
276      * This method takes a JSON format document schema and produces a set of field mappings in the form that Elastic
277      * Search expects.
278      *
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
284      */
285     public String generateDocumentMappings(String documentSchema) throws IOException {
286
287         // Unmarshal the json content into a document schema object.
288         ObjectMapper mapper = new ObjectMapper();
289         DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
290
291         // Now, generate the Elastic Search mapping json and return it.
292         StringBuilder sb = new StringBuilder();
293         sb.append("{");
294         sb.append("\"properties\": {");
295
296         boolean first = true;
297         for (DocumentFieldSchema field : schema.getFields()) {
298
299             if (!first) {
300                 sb.append(",");
301             } else {
302                 first = false;
303             }
304
305             sb.append("\"").append(field.getName()).append("\": {");
306
307             // The field type is mandatory.
308             sb.append("\"type\": \"").append(field.getDataType()).append("\"");
309
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("\"");
313             }
314
315             // If a search analyzer was specified, then append it.
316             if (field.getSearchAnalyzer() != null) {
317                 sb.append(", \"search_analyzer\": \"").append(field.getSearchAnalyzer()).append("\"");
318             }
319
320             // If an indexing analyzer was specified, then append it.
321             if (field.getIndexAnalyzer() != null) {
322                 sb.append(", \"analyzer\": \"").append(field.getIndexAnalyzer()).append("\"");
323             } else {
324                 sb.append(", \"analyzer\": \"").append("whitespace").append("\"");
325             }
326
327             sb.append("}");
328         }
329
330         sb.append("}");
331         sb.append("}");
332
333         logger.debug("Generated document mappings: " + sb.toString());
334
335         return sb.toString();
336     }
337
338
339     /**
340      * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity} object.
341      *
342      * @param result - The {@link OperationResult} to be converted.
343      * @return - The equivalent {@link ResponseEntity} object.
344      */
345     public ResponseEntity<String> responseFromOperationResult(OperationResult result) {
346
347         if (ApiUtils.isSuccessStatusCode(result.getResultCode())) {
348             return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
349                     .body(result.getResult());
350         } else {
351             if (result.getFailureCause() != null) {
352                 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
353                         .body(result.getFailureCause());
354             } else {
355                 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
356                         .body(result.getResult());
357             }
358         }
359     }
360
361     public ResponseEntity<String> errorResponse(HttpStatus status, String msg, HttpServletRequest request) {
362
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()));
370
371         // Clear the MDC context so that no other transaction inadvertently
372         // uses our transaction id.
373         ApiUtils.clearMdcContext();
374
375         return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).body(msg);
376     }
377
378
379     /**
380      * A helper method used for validating/authenticating an incoming request.
381      *
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
387      */
388     private ResponseEntity<String> validateRequest(HttpServletRequest request, HttpHeaders headers, String index,
389             SearchDbMsgs failureMsgEnum) {
390         try {
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);
395             }
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);
399         }
400         return ResponseEntity.status(HttpStatus.OK).body(HEADER_VALIDATION_SUCCESS);
401     }
402 }