X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsa%2Fsearchdbabstraction%2Futil%2FElasticSearchPayloadTranslator.java;h=79b145d7353dde2537b4905a5cd56432233d5e88;hb=15af66b115f3e8046b2d0f2634fb77b3d835f730;hp=d73001bb7da00e988e33bb5a93747e78ee05b92b;hpb=71393bb2bf9fb10962723e61cf0e70d9aa202645;p=aai%2Fsearch-data-service.git diff --git a/src/main/java/org/onap/aai/sa/searchdbabstraction/util/ElasticSearchPayloadTranslator.java b/src/main/java/org/onap/aai/sa/searchdbabstraction/util/ElasticSearchPayloadTranslator.java index d73001b..79b145d 100644 --- a/src/main/java/org/onap/aai/sa/searchdbabstraction/util/ElasticSearchPayloadTranslator.java +++ b/src/main/java/org/onap/aai/sa/searchdbabstraction/util/ElasticSearchPayloadTranslator.java @@ -37,62 +37,64 @@ import org.onap.aai.sa.searchdbabstraction.logging.SearchDbMsgs; /** - * This class as the name suggests is to translate the payload of PUT & POST requests - * to ElasticSearch (ES) to its compatible syntax, specially compatible with ES v6 or above. + * This class as the name suggests is to translate the payload of PUT & POST requests to ElasticSearch (ES) to its + * compatible syntax, specially compatible with ES v6 or above. * - * For example, data type such as "string" is now replaced by "text" or "keyword". + * For example, data type such as "string" is now replaced by "text" or "keyword". * - * So this class will make those translations reading off from a json configuration file, therefore - * the configuration can be updated with new translations as and when required without touching the code. + * So this class will make those translations reading off from a json configuration file, therefore the configuration + * can be updated with new translations as and when required without touching the code. * * @author EDWINL * */ public class ElasticSearchPayloadTranslator { - private static Logger logger = LoggerFactory.getInstance().getLogger(ElasticSearchPayloadTranslator.class.getName()); - private static final String CONFIG_DIRECTORY = System.getProperty("CONFIG_HOME"); - private static final String ES_PAYLOAD_TRANSLATION_FILE = "es-payload-translation.json"; + private static Logger logger = + LoggerFactory.getInstance().getLogger(ElasticSearchPayloadTranslator.class.getName()); + private static final String CONFIG_DIRECTORY = System.getProperty("CONFIG_HOME"); + private static final String ES_PAYLOAD_TRANSLATION_FILE = "es-payload-translation.json"; - /** - * Using JSON Path query to filter objects to translate the payload to ES compatible version - * The filter queries and the replacement attributes are configured in the es-payload-translation.json file. - * - * @param source - * @return translated payload in String - * @throws IOException - */ - public static String translateESPayload(String source) throws IOException { - logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "translateESPayload, method-params[ source=" + source + "]"); - String pathToTranslationFile = CONFIG_DIRECTORY + File.separator + ES_PAYLOAD_TRANSLATION_FILE; + /** + * Using JSON Path query to filter objects to translate the payload to ES compatible version The filter queries and + * the replacement attributes are configured in the es-payload-translation.json file. + * + * @param source + * @return translated payload in String + * @throws IOException + */ + public static String translateESPayload(String source) throws IOException { + logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "translateESPayload, method-params[ source=" + source + "]"); + String pathToTranslationFile = CONFIG_DIRECTORY + File.separator + ES_PAYLOAD_TRANSLATION_FILE; - try { - - JSONObject translationConfigPayload = new JSONObject(IOUtils.toString( - new FileInputStream(new File(pathToTranslationFile)), "UTF-8")); - JSONArray attrTranslations = translationConfigPayload.getJSONArray("attr-translations"); - DocumentContext payloadToTranslate = JsonPath.parse(source); + try { - for(Object obj : attrTranslations) { - JSONObject jsonObj = ((JSONObject) obj); - String query = jsonObj.get("query").toString(); - JSONObject attrToUpdate = (JSONObject) jsonObj.get("update"); - List> filteredObjects = payloadToTranslate.read(query); - for(Map objMap : filteredObjects) { - objMap.putAll(attrToUpdate.toMap()); - } - } - - logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, "Payload after translation: "+payloadToTranslate.jsonString()); - return payloadToTranslate.jsonString(); - - } catch (JSONException | IOException e) { - logger.error(SearchDbMsgs.FILTERS_CONFIG_FAILURE, e, ES_PAYLOAD_TRANSLATION_FILE, e.getMessage()); - if(e instanceof JSONException) { - throw new IOException("Payload translation configuration looks corrupted. Please correct!", e); - } - throw new IOException("Error in configuring payload translation file. Please check if it exists.", e); - } - } + JSONObject translationConfigPayload = + new JSONObject(IOUtils.toString(new FileInputStream(new File(pathToTranslationFile)), "UTF-8")); + JSONArray attrTranslations = translationConfigPayload.getJSONArray("attr-translations"); + DocumentContext payloadToTranslate = JsonPath.parse(source); + + for (Object obj : attrTranslations) { + JSONObject jsonObj = ((JSONObject) obj); + String query = jsonObj.get("query").toString(); + JSONObject attrToUpdate = (JSONObject) jsonObj.get("update"); + List> filteredObjects = payloadToTranslate.read(query); + for (Map objMap : filteredObjects) { + objMap.putAll(attrToUpdate.toMap()); + } + } + + logger.info(SearchDbMsgs.PROCESS_PAYLOAD_QUERY, + "Payload after translation: " + payloadToTranslate.jsonString()); + return payloadToTranslate.jsonString(); + + } catch (JSONException | IOException e) { + logger.error(SearchDbMsgs.FILTERS_CONFIG_FAILURE, e, ES_PAYLOAD_TRANSLATION_FILE, e.getMessage()); + if (e instanceof JSONException) { + throw new IOException("Payload translation configuration looks corrupted. Please correct!", e); + } + throw new IOException("Error in configuring payload translation file. Please check if it exists.", e); + } + } }