Format Java code to ONAP standard
[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 /**
42  * This class encapsulates the REST end points associated with manipulating indexes in the document store.
43  */
44 public class IndexApi {
45
46
47     private static final String HEADER_VALIDATION_SUCCESS = "SUCCESS";
48     protected SearchServiceApi searchService = null;
49
50     /**
51      * Configuration for the custom analyzers that will be used for indexing.
52      */
53     protected AnalysisConfiguration analysisConfig;
54
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());
58
59
60     public IndexApi(SearchServiceApi searchService) {
61         this.searchService = searchService;
62         init();
63     }
64
65
66     /**
67      * Initializes the end point.
68      *
69      * @throws FileNotFoundException
70      * @throws IOException
71      * @throws DocumentStoreOperationException
72      */
73     public void init() {
74
75         // Instantiate our analysis configuration object.
76         analysisConfig = new AnalysisConfiguration();
77     }
78
79
80     /**
81      * Processes client requests to create a new index and document type in the document store.
82      *
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
87      */
88     public ResponseEntity<String> processCreateIndex(String documentSchema, HttpServletRequest request,
89             HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
90
91         int resultCode = 500;
92         String resultString = "Unexpected error";
93
94         // Initialize the MDC Context for logging purposes.
95         ApiUtils.initMdcContext(request, headers);
96
97         // Validate that the request is correctly authenticated before going
98         // any further.
99         try {
100
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);
105             }
106
107         } catch (Exception e) {
108
109             logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
110                     "Unexpected authentication failure - cause: " + e.getMessage());
111             return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
112         }
113
114
115         // We expect a payload containing the document schema. Make sure
116         // it is present.
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);
120         }
121
122         try {
123
124             // Marshal the supplied json string into a document schema object.
125             ObjectMapper mapper = new ObjectMapper();
126             DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
127
128             // Now, ask the DAO to create the index.
129             OperationResult result = documentStore.createIndex(index, schema);
130
131             // Extract the result code and string from the OperationResult
132             // object so that we can use them to generate a standard REST
133             // response.
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();
139
140         } catch (com.fasterxml.jackson.core.JsonParseException
141                 | com.fasterxml.jackson.databind.JsonMappingException e) {
142
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();
147
148         } catch (IOException e) {
149
150             // We'll treat this is a general internal error.
151             resultCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
152             resultString = "IO Failure: " + e.getMessage();
153         }
154
155         ResponseEntity<String> response =
156                 ResponseEntity.status(resultCode).contentType(MediaType.APPLICATION_JSON).body(resultString);
157
158
159         // Log the result.
160         if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
161             logger.info(SearchDbMsgs.CREATED_INDEX, index);
162         } else {
163             logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, resultString);
164         }
165
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()));
174
175
176
177         // Clear the MDC context so that no other transaction inadvertently
178         // uses our transaction id.
179         ApiUtils.clearMdcContext();
180
181         // Finally, return the response.
182         return response;
183     }
184
185     /**
186      * This function accepts any JSON and will "blindly" write it to the document store.
187      *
188      * Note, eventually this "dynamic" flow should follow the same JSON-Schema validation procedure as the normal create
189      * index flow.
190      *
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.
195      */
196     public ResponseEntity<String> processCreateDynamicIndex(String dynamicSchema, HttpServletRequest request,
197             HttpHeaders headers, String index, DocumentStoreInterface documentStore) {
198
199         ResponseEntity<String> response = null;
200
201         ResponseEntity<String> validationResponse =
202                 validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);
203
204
205         if (validationResponse.getStatusCodeValue() != HttpStatus.OK.value()) {
206             response = validationResponse;
207         } else {
208             OperationResult result = documentStore.createDynamicIndex(index, dynamicSchema);
209
210             int resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
211             String resultString = (result.getFailureCause() == null) ? result.getResult() : result.getFailureCause();
212
213             response = ResponseEntity.status(resultCode).body(resultString);
214         }
215
216         return response;
217     }
218
219     /**
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.
222      *
223      * @param index - The index to be deleted.
224      * @return - A standard REST response.
225      */
226     public ResponseEntity<String> processDelete(String index, HttpServletRequest request, HttpHeaders headers,
227             DocumentStoreInterface documentStore) {
228
229         // Initialize the MDC Context for logging purposes.
230         ApiUtils.initMdcContext(request, headers);
231
232         // Set a default response in case something unexpected goes wrong.
233         ResponseEntity<String> response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unknown");
234
235         // Validate that the request is correctly authenticated before going
236         // any further.
237         try {
238
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);
243             }
244
245         } catch (Exception e) {
246
247             logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
248                     "Unexpected authentication failure - cause: " + e.getMessage());
249             return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
250         }
251
252
253         try {
254             // Send the request to the document store.
255             response = responseFromOperationResult(documentStore.deleteIndex(index));
256
257         } catch (DocumentStoreOperationException e) {
258             response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON)
259                     .body(e.getMessage());
260         }
261
262         // Log the result.
263         if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
264             logger.info(SearchDbMsgs.DELETED_INDEX, index);
265         } else {
266             logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, response.getBody());
267         }
268
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()));
277
278         // Clear the MDC context so that no other transaction inadvertently
279         // uses our transaction id.
280         ApiUtils.clearMdcContext();
281
282         return response;
283     }
284
285
286     /**
287      * This method takes a JSON format document schema and produces a set of field mappings in the form that Elastic
288      * Search expects.
289      *
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
295      */
296     public String generateDocumentMappings(String documentSchema) throws com.fasterxml.jackson.core.JsonParseException,
297             com.fasterxml.jackson.databind.JsonMappingException, IOException {
298
299         // Unmarshal the json content into a document schema object.
300         ObjectMapper mapper = new ObjectMapper();
301         DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
302
303         // Now, generate the Elastic Search mapping json and return it.
304         StringBuilder sb = new StringBuilder();
305         sb.append("{");
306         sb.append("\"properties\": {");
307
308         boolean first = true;
309         for (DocumentFieldSchema field : schema.getFields()) {
310
311             if (!first) {
312                 sb.append(",");
313             } else {
314                 first = false;
315             }
316
317             sb.append("\"").append(field.getName()).append("\": {");
318
319             // The field type is mandatory.
320             sb.append("\"type\": \"").append(field.getDataType()).append("\"");
321
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("\"");
325             }
326
327             // If a search analyzer was specified, then append it.
328             if (field.getSearchAnalyzer() != null) {
329                 sb.append(", \"search_analyzer\": \"").append(field.getSearchAnalyzer()).append("\"");
330             }
331
332             // If an indexing analyzer was specified, then append it.
333             if (field.getIndexAnalyzer() != null) {
334                 sb.append(", \"analyzer\": \"").append(field.getIndexAnalyzer()).append("\"");
335             } else {
336                 sb.append(", \"analyzer\": \"").append("whitespace").append("\"");
337             }
338
339             sb.append("}");
340         }
341
342         sb.append("}");
343         sb.append("}");
344
345         logger.debug("Generated document mappings: " + sb.toString());
346
347         return sb.toString();
348     }
349
350
351     /**
352      * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity} object.
353      *
354      * @param result - The {@link OperationResult} to be converted.
355      * @return - The equivalent {@link ResponseEntity} object.
356      */
357     public ResponseEntity<String> responseFromOperationResult(OperationResult result) {
358
359         if ((result.getResultCode() >= 200) && (result.getResultCode() < 300)) {
360             return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
361                     .body(result.getResult());
362         } else {
363             if (result.getFailureCause() != null) {
364                 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
365                         .body(result.getFailureCause());
366             } else {
367                 return ResponseEntity.status(result.getResultCode()).contentType(MediaType.APPLICATION_JSON)
368                         .body(result.getResult());
369             }
370         }
371     }
372
373     public ResponseEntity<String> errorResponse(HttpStatus status, String msg, HttpServletRequest request) {
374
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()));
382
383         // Clear the MDC context so that no other transaction inadvertently
384         // uses our transaction id.
385         ApiUtils.clearMdcContext();
386
387         return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).body(msg);
388     }
389
390
391     /**
392      * A helper method used for validating/authenticating an incoming request.
393      *
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
399      */
400     private ResponseEntity<String> validateRequest(HttpServletRequest request, HttpHeaders headers, String index,
401             SearchDbMsgs failureMsgEnum) {
402         try {
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);
407             }
408         } catch (Exception e) {
409             logger.warn(failureMsgEnum, index, "Unexpected authentication failure - cause: " + e.getMessage());
410             return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
411         }
412         return ResponseEntity.status(HttpStatus.OK).body(HEADER_VALIDATION_SUCCESS);
413     }
414 }