Updating Search service to be ES 6.1.2 compliant
[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
27 import org.apache.commons.io.IOUtils;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs;
33
34
35 /**
36  * This class as the name suggests is to translate the payload of PUT & POST requests
37  * to ElasticSearch (ES) to its compatible syntax, specially compatible with ES v6 or above.
38  * 
39  * For example, data type such as "string" is now replaced by "text" or "keyword". 
40  * 
41  * So this class will make those translations reading off from a json configuration file, therefore
42  * the configuration can be updated with new translations as and when required without touching the code. 
43  * 
44  * @author EDWINL
45  *
46  */
47 public class ElasticSearchPayloadTranslator {
48
49         private static Logger logger = LoggerFactory.getInstance().getLogger(ElasticSearchPayloadTranslator.class.getName());
50         private static final String CONFIG_DIRECTORY = System.getProperty("CONFIG_HOME");
51         private static final String ES_PAYLOAD_TRANSLATION_FILE = "es-payload-translation.json";
52
53
54         /**
55          *  Using String replacement to translate the payload to ES compatible version
56          * 
57          * @param source
58          * @return translated payload in String
59          * @throws IOException
60          */
61         public static String translateESPayload(String source) throws IOException {
62                 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "translateESPayload, method-params[ source=" + source + "]");
63                 String pathToTranslationFile = CONFIG_DIRECTORY + File.separator + ES_PAYLOAD_TRANSLATION_FILE;
64                 
65                 String translatedPayload = source.replaceAll("\\s+", ""); // removing whitespaces
66                 JSONObject translationConfigPayload = new JSONObject(IOUtils.toString(
67                                 new FileInputStream(new File(pathToTranslationFile)), "UTF-8"));
68
69                 JSONArray attrTranslations = translationConfigPayload.getJSONArray("attr-translations");
70
71                 for(Object obj : attrTranslations) {
72                         JSONObject jsonObj = ((JSONObject)obj);
73                         String from = jsonObj.get("from").toString();
74                         String to = jsonObj.get("to").toString();
75                         if(translatedPayload.indexOf(from) > 0) {
76                                 translatedPayload = translatedPayload.replaceAll(from, to);
77                         }
78                 }
79
80                 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "Payload after translation: "+translatedPayload);
81                 return translatedPayload;
82         }
83 }