aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/sa/rest/IndexApi.java
blob: f5840e29a4ca4dcc631635841aafdb6669660bd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.sa.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.onap.aai.cl.api.LogFields;
import org.onap.aai.cl.api.LogLine;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.sa.searchdbabstraction.elasticsearch.dao.DocumentStoreInterface;
import org.onap.aai.sa.searchdbabstraction.elasticsearch.exception.DocumentStoreOperationException;
import org.onap.aai.sa.searchdbabstraction.entity.OperationResult;
import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
// Spring Imports
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
// import org.springframework.http.server.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;


/**
 * This class encapsulates the REST end points associated with manipulating
 * indexes in the document store.
 */
public class IndexApi {


  private static final String HEADER_VALIDATION_SUCCESS = "SUCCESS";
  protected SearchServiceApi searchService = null;

  /**
   * Configuration for the custom analyzers that will be used for indexing.
   */
  protected AnalysisConfiguration analysisConfig;

  // Set up the loggers.
  private static Logger logger = LoggerFactory.getInstance()
    .getLogger(IndexApi.class.getName());
  private static Logger auditLogger = LoggerFactory.getInstance()
    .getAuditLogger(IndexApi.class.getName());


  public IndexApi(SearchServiceApi searchService) {
    this.searchService = searchService;
    init();
  }


  /**
   * Initializes the end point.
   *
   * @throws FileNotFoundException
   * @throws IOException
   * @throws DocumentStoreOperationException
   */
  public void init() {

    // Instantiate our analysis configuration object.
    analysisConfig = new AnalysisConfiguration();
  }


  /**
   * Processes client requests to create a new index and document type in the
   * document store.
   *
   * @param documentSchema - The contents of the request body which is expected
   *                       to be a JSON structure which corresponds to the
   *                       schema defined in document.schema.json
   * @param index          - The name of the index to create.
   * @return - A Standard REST response
   */
  public ResponseEntity<String> processCreateIndex (String documentSchema,
                                                    HttpServletRequest request,
                                                    HttpHeaders headers,
                                                    String index,
                                                    DocumentStoreInterface documentStore) {

    int resultCode = 500;
    String resultString = "Unexpected error";
   
    // Initialize the MDC Context for logging purposes.
    ApiUtils.initMdcContext(request, headers);

    // Validate that the request is correctly authenticated before going
    // any further.
    try {

      if (!searchService.validateRequest(headers, request,
                                         ApiUtils.Action.POST, ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
        logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Authentication failure.");
        return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
      }

    } catch (Exception e) {

      logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
                  "Unexpected authentication failure - cause: " + e.getMessage());
      return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
    }


    // We expect a payload containing the document schema.  Make sure
    // it is present.
    if (documentSchema == null) {
      logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Missing document schema payload");
      return errorResponse(HttpStatus.valueOf(resultCode), "Missing payload", request);
    }

    try {

      // Marshal the supplied json string into a document schema object.
      ObjectMapper mapper = new ObjectMapper();
      DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);

      // Now, ask the DAO to create the index.
      OperationResult result = documentStore.createIndex(index, schema);

      // Extract the result code and string from the OperationResult
      // object so that we can use them to generate a standard REST
      // response.
      // Note that we want to return a 201 result code on a successful
      // create, so if we get back a 200 from the document store,
      // translate that int a 201.
      resultCode = (result.getResultCode() == 200) ? 201 : result.getResultCode();
      resultString = (result.getFailureCause() == null)
        ? result.getResult() : result.getFailureCause();

    } catch (com.fasterxml.jackson.core.JsonParseException
             | com.fasterxml.jackson.databind.JsonMappingException e) {

      // We were unable to marshal the supplied json string into a valid
      // document schema, so return an appropriate error response.
      resultCode = HttpStatus.BAD_REQUEST.value();
      resultString = "Malformed schema: " + e.getMessage();

    } catch (IOException e) {

      // We'll treat this is a general internal error.
      resultCode = HttpStatus.INTERNAL_SERVER_ERROR.value();
      resultString = "IO Failure: " + e.getMessage();
    }

    ResponseEntity<String> response = ResponseEntity.status(resultCode).contentType ( MediaType.APPLICATION_JSON ).body(resultString);


    // Log the result.
    if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
      logger.info(SearchDbMsgs.CREATED_INDEX, index);
    } else {
      logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, resultString);
    }

    // Generate our audit log.
    auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
                     new LogFields()
                     .setField(LogLine.DefinedFields.RESPONSE_CODE, resultCode)
                     .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION,
                               HttpStatus.valueOf(resultCode).toString()),
                     (request != null) ? request.getMethod().toString () : "Unknown",
                     (request != null) ? request.getRequestURL ().toString () : "Unknown",
                     (request != null) ? request.getRemoteHost () : "Unknown",
                     Integer.toString(response.getStatusCodeValue ()));



    // Clear the MDC context so that no other transaction inadvertently
    // uses our transaction id.
    ApiUtils.clearMdcContext();

    // 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 ResponseEntity<String> processCreateDynamicIndex(String dynamicSchema, HttpServletRequest request,
                                            HttpHeaders headers, String index, DocumentStoreInterface documentStore) {

    ResponseEntity<String> response = null;

    ResponseEntity<String> validationResponse = validateRequest(request, headers, index, SearchDbMsgs.INDEX_CREATE_FAILURE);


    if (validationResponse.getStatusCodeValue () != HttpStatus.OK.value ()) {
      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 = ResponseEntity.status(resultCode).body(resultString);
    }

    return response;
  }

  /**
   * Processes a client request to remove an index from the document store.
   * Note that this implicitly deletes all documents contained within that index.
   *
   * @param index - The index to be deleted.
   * @return - A standard REST response.
   */
  public ResponseEntity<String> processDelete(String index,
                                              HttpServletRequest request,
                                              HttpHeaders headers,
                                              DocumentStoreInterface documentStore) {

    // Initialize the MDC Context for logging purposes.
    ApiUtils.initMdcContext(request, headers);

    // Set a default response in case something unexpected goes wrong.
    ResponseEntity<String> response = ResponseEntity.status ( HttpStatus.INTERNAL_SERVER_ERROR ).body ( "Unknown" );

    // Validate that the request is correctly authenticated before going
    // any further.
    try {

      if (!searchService.validateRequest(headers, request, ApiUtils.Action.POST,
                                         ApiUtils.SEARCH_AUTH_POLICY_NAME)) {
        logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index, "Authentication failure.");
        return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
      }

    } catch (Exception e) {

      logger.warn(SearchDbMsgs.INDEX_CREATE_FAILURE, index,
                  "Unexpected authentication failure - cause: " + e.getMessage());
      return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
    }


    try {
      // Send the request to the document store.
      response = responseFromOperationResult(documentStore.deleteIndex(index));

    } catch (DocumentStoreOperationException e) {
      response = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType ( MediaType.APPLICATION_JSON ).body(e.getMessage());
    }

    // Log the result.
    if ((response.getStatusCodeValue() >= 200) && (response.getStatusCodeValue() < 300)) {
      logger.info(SearchDbMsgs.DELETED_INDEX, index);
    } else {
      logger.warn(SearchDbMsgs.INDEX_DELETE_FAILURE, index, (String) response.getBody ());
    }

    // Generate our audit log.
    auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
                     new LogFields()
                     .setField(LogLine.DefinedFields.RESPONSE_CODE, response.getStatusCodeValue())
                     .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION,
                               response.getStatusCode ().getReasonPhrase()),
                     (request != null) ? request.getMethod().toString () : "Unknown",
                     (request != null) ? request.getRequestURL ().toString () : "Unknown",
                     (request != null) ? request.getRemoteHost () : "Unknown",
                     Integer.toString(response.getStatusCodeValue()));

    // Clear the MDC context so that no other transaction inadvertently
    // uses our transaction id.
    ApiUtils.clearMdcContext();

    return response;
  }


  /**
   * This method takes a JSON format document schema and produces a set of
   * field mappings in the form that Elastic Search expects.
   *
   * @param documentSchema - A document schema expressed as a JSON string.
   * @return - A JSON string expressing an Elastic Search mapping configuration.
   * @throws com.fasterxml.jackson.core.JsonParseException
   * @throws com.fasterxml.jackson.databind.JsonMappingException
   * @throws IOException
   */
  public String generateDocumentMappings(String documentSchema)
    throws com.fasterxml.jackson.core.JsonParseException,
           com.fasterxml.jackson.databind.JsonMappingException, IOException {

    // Unmarshal the json content into a document schema object.
    ObjectMapper mapper = new ObjectMapper();
    DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);

    // Now, generate the Elastic Search mapping json and return it.
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    sb.append("\"properties\": {");

    boolean first = true;
    for (DocumentFieldSchema field : schema.getFields()) {

      if (!first) {
        sb.append(",");
      } else {
        first = false;
      }

      sb.append("\"").append(field.getName()).append("\": {");

      // The field type is mandatory.
      sb.append("\"type\": \"").append(field.getDataType()).append("\"");

      // If the index field was specified, then append it.
      if (field.getSearchable() != null) {
        sb.append(", \"index\": \"").append(field.getSearchable()
                                            ? "analyzed" : "not_analyzed").append("\"");
      }

      // If a search analyzer was specified, then append it.
      if (field.getSearchAnalyzer() != null) {
        sb.append(", \"search_analyzer\": \"").append(field.getSearchAnalyzer()).append("\"");
      }

      // If an indexing analyzer was specified, then append it.
      if (field.getIndexAnalyzer() != null) {
        sb.append(", \"analyzer\": \"").append(field.getIndexAnalyzer()).append("\"");
      } else {
        sb.append(", \"analyzer\": \"").append("whitespace").append("\"");
      }

      sb.append("}");
    }

    sb.append("}");
    sb.append("}");

    logger.debug("Generated document mappings: " + sb.toString());

    return sb.toString();
  }


  /**
   * Converts an {@link OperationResult} to a standard REST {@link ResponseEntity}
   * object.
   *
   * @param result - The {@link OperationResult} to be converted.
   * @return - The equivalent {@link ResponseEntity} object.
   */
  public ResponseEntity<String> responseFromOperationResult(OperationResult result) {

    if ((result.getResultCode() >= 200) && (result.getResultCode() < 300)) {
      return ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(result.getResult());
    } else {
      if (result.getFailureCause() != null) {
        return ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(result.getFailureCause());
      } else {
        return ResponseEntity.status(result.getResultCode()).contentType ( MediaType.APPLICATION_JSON ).body(result.getResult());
      }
    }
  }

  public ResponseEntity<String> errorResponse(HttpStatus status, String msg, HttpServletRequest request) {

    // Generate our audit log.
    auditLogger.info(SearchDbMsgs.PROCESS_REST_REQUEST,
                     new LogFields()
                     .setField(LogLine.DefinedFields.RESPONSE_CODE, status.value ())
                     .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, status.getReasonPhrase()),
                     (request != null) ? request.getMethod().toString () : "Unknown",
                     (request != null) ? request.getRequestURL ().toString () : "Unknown",
                     (request != null) ? request.getRemoteHost () : "Unknown",
                     Integer.toString(status.value ()));

    // Clear the MDC context so that no other transaction inadvertently
    // uses our transaction id.
    ApiUtils.clearMdcContext();

    return ResponseEntity.status(status).contentType ( MediaType.APPLICATION_JSON ).body(msg);
  }


  /**
   * 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 ResponseEntity<String> 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(HttpStatus.FORBIDDEN, "Authentication failure.", request);
      }
    } catch (Exception e) {
      logger.warn(failureMsgEnum, index, "Unexpected authentication failure - cause: " + e.getMessage());
      return errorResponse(HttpStatus.FORBIDDEN, "Authentication failure.", request);
    }
    return ResponseEntity.status(HttpStatus.OK).body(HEADER_VALIDATION_SUCCESS);
  }
}