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