2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sa.searchdbabstraction.util;
23 import com.jayway.jsonpath.DocumentContext;
24 import com.jayway.jsonpath.JsonPath;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.util.List;
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;
40 * This class as the name suggests is to translate the payload of PUT & POST requests
41 * to ElasticSearch (ES) to its compatible syntax, specially compatible with ES v6 or above.
43 * For example, data type such as "string" is now replaced by "text" or "keyword".
45 * So this class will make those translations reading off from a json configuration file, therefore
46 * the configuration can be updated with new translations as and when required without touching the code.
51 public class ElasticSearchPayloadTranslator {
53 private static Logger logger = LoggerFactory.getInstance().getLogger(ElasticSearchPayloadTranslator.class.getName());
54 private static final String CONFIG_DIRECTORY = System.getProperty("CONFIG_HOME");
55 private static final String ES_PAYLOAD_TRANSLATION_FILE = "es-payload-translation.json";
59 * Using JSON Path query to filter objects to translate the payload to ES compatible version
60 * The filter queries and the replacement attributes are configured in the es-payload-translation.json file.
63 * @return translated payload in String
66 public static String translateESPayload(String source) throws IOException {
67 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "translateESPayload, method-params[ source=" + source + "]");
68 String pathToTranslationFile = CONFIG_DIRECTORY + File.separator + ES_PAYLOAD_TRANSLATION_FILE;
72 JSONObject translationConfigPayload = new JSONObject(IOUtils.toString(
73 new FileInputStream(new File(pathToTranslationFile)), "UTF-8"));
74 JSONArray attrTranslations = translationConfigPayload.getJSONArray("attr-translations");
75 DocumentContext payloadToTranslate = JsonPath.parse(source);
77 for(Object obj : attrTranslations) {
78 JSONObject jsonObj = ((JSONObject) obj);
79 String query = jsonObj.get("query").toString();
80 JSONObject attrToUpdate = (JSONObject) jsonObj.get("update");
81 List<Map<String, Object>> filteredObjects = payloadToTranslate.read(query);
82 for(Map<String, Object> objMap : filteredObjects) {
83 objMap.putAll(attrToUpdate.toMap());
87 logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "Payload after translation: "+payloadToTranslate.jsonString());
88 return payloadToTranslate.jsonString();
90 } catch (JSONException | IOException e) {
91 logger.error(SearchDbMsgs.FILTERS_CONFIG_FAILURE, e, ES_PAYLOAD_TRANSLATION_FILE, e.getMessage());
92 if(e instanceof JSONException) {
93 throw new IOException("Payload translation configuration looks corrupted. Please correct!", e);
95 throw new IOException("Error in configuring payload translation file. Please check if it exists.", e);