3 * ============LICENSE_START=======================================================
5 * ================================================================================
6 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 i * 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=========================================================
22 package org.openecomp.sdnc.config.generator.merge;
24 import java.nio.charset.Charset;
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
30 import org.openecomp.sdnc.config.generator.tool.JSONTool;
31 import org.openecomp.sdnc.config.generator.tool.MergeTool;
32 import org.openecomp.sdnc.config.generator.tool.EscapeUtils;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
40 public class MergeNode implements SvcLogicJavaPlugin {
42 private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeNode.class);
44 public void mergeDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
48 public void mergeJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
49 log.info("Received mergeJsonDataOnTemplate call with params : " + inParams);
50 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
52 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
53 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
54 if(StringUtils.isBlank(jsonData)){
55 throw new Exception("JSON Data is missing");
58 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
59 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
61 if(StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)){
62 throw new Exception("Template data or Template file is missing");
64 if(StringUtils.isBlank(templateData)){
65 String path = MergeNode.class.getClassLoader().getResource(".").toString();
66 templateData = IOUtils.toString(MergeNode.class.getClassLoader().getResourceAsStream(templateFile));
69 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
71 Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
72 log.info("Data Maps created :" + dataMap);
74 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
75 if(mergedData != null){
76 // ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
77 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,EscapeUtils.unescapeSql(mergedData));
81 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
82 log.info("Data Merge Successful :" + ctx);
83 } catch (Exception e) {
84 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
85 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
86 log.error("Failed in merging data to template " + e.getMessage());
87 throw new SvcLogicException(e.getMessage());
91 public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
92 //log.info("Received mergeJsonComplexDataOnTemplate call with params : " + inParams);
93 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
95 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
96 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
97 if(StringUtils.isBlank(jsonData)){
98 throw new Exception("JSON Data is missing");
101 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
102 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
104 if(StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)){
105 throw new Exception("Template data or Template file is missing");
107 if(StringUtils.isBlank(templateData)){
108 //String path = MergeNode.class.getClassLoader().getResource(".").toString();
109 templateData = IOUtils.toString(MergeNode.class.getClassLoader().getResourceAsStream(templateFile), Charset.defaultCharset());
112 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
113 String doPrettyOutput = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
115 String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData, templateType, doPrettyOutput);
116 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
118 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
119 //log.info("Data Merge Successful :" + ctx);
120 } catch (Exception e) {
121 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
122 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
123 log.error("Failed in merging data to template " + e.getMessage());
124 throw new SvcLogicException(e.getMessage());
128 public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {