Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service-app / src / main / java / org / onap / aai / sa / searchdbabstraction / util / ElasticSearchPayloadTranslator.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.searchdbabstraction.util;
22
23 import com.jayway.jsonpath.DocumentContext;
24 import com.jayway.jsonpath.JsonPath;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.Map;
30 import org.apache.commons.io.IOUtils;
31 import org.json.JSONArray;
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 import org.onap.aai.cl.api.Logger;
35 import org.onap.aai.cl.eelf.LoggerFactory;
36 import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
37
38
39 /**
40  * This class as the name suggests is to translate the payload of PUT & POST requests to ElasticSearch (ES) to its
41  * compatible syntax, specially compatible with ES v6 or above.
42  *
43  * For example, data type such as "string" is now replaced by "text" or "keyword".
44  *
45  * So this class will make those translations reading off from a json configuration file, therefore the configuration
46  * can be updated with new translations as and when required without touching the code.
47  *
48  * @author EDWINL
49  *
50  */
51 public class ElasticSearchPayloadTranslator {
52
53     private static Logger logger =
54             LoggerFactory.getInstance().getLogger(ElasticSearchPayloadTranslator.class.getName());
55     private static final String CONFIG_DIRECTORY = System.getProperty("CONFIG_HOME");
56     private static final String ES_PAYLOAD_TRANSLATION_FILE = "es-payload-translation.json";
57
58     private ElasticSearchPayloadTranslator() { // Do not instantiate
59     }
60
61     /**
62      * Using JSON Path query to filter objects to translate the payload to ES compatible version The filter queries and
63      * the replacement attributes are configured in the es-payload-translation.json file.
64      *
65      * @param source
66      * @return translated payload in String
67      * @throws IOException
68      */
69     public static String translateESPayload(String source) throws IOException {
70         logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "translateESPayload, method-params[ source=" + source + "]",
71                 "(unknown)");
72         String pathToTranslationFile = CONFIG_DIRECTORY + File.separator + ES_PAYLOAD_TRANSLATION_FILE;
73
74         try {
75             JSONObject translationConfigPayload =
76                     new JSONObject(IOUtils.toString(new FileInputStream(new File(pathToTranslationFile)), "UTF-8"));
77             JSONArray attrTranslations = translationConfigPayload.getJSONArray("attr-translations");
78             DocumentContext payloadToTranslate = JsonPath.parse(source);
79
80             for (Object obj : attrTranslations) {
81                 JSONObject jsonObj = ((JSONObject) obj);
82                 String query = jsonObj.get("query").toString();
83                 JSONObject attrToUpdate = (JSONObject) jsonObj.get("update");
84                 List<Map<String, Object>> filteredObjects = payloadToTranslate.read(query);
85                 for (Map<String, Object> objMap : filteredObjects) {
86                     objMap.putAll(attrToUpdate.toMap());
87                 }
88             }
89
90             logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY,
91                     "Payload after translation: " + payloadToTranslate.jsonString());
92             return payloadToTranslate.jsonString();
93         } catch (JSONException ex) {
94             logger.error(SearchDbMsgs.FILTERS_CONFIG_FAILURE, ex, ES_PAYLOAD_TRANSLATION_FILE, ex.getMessage());
95             throw new IOException("Payload translation configuration looks corrupted. Please correct!", ex);
96         } catch (IOException ex) {
97             logger.error(SearchDbMsgs.FILTERS_CONFIG_FAILURE, ex, ES_PAYLOAD_TRANSLATION_FILE, ex.getMessage());
98             throw new IOException("Error in configuring payload translation file. Please check if it exists.", ex);
99         }
100     }
101 }