2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.generator.merge;
27 import java.nio.charset.Charset;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
35 import org.onap.sdnc.config.generator.tool.EscapeUtils;
36 import org.onap.sdnc.config.generator.tool.JSONTool;
37 import org.onap.sdnc.config.generator.tool.MergeTool;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
41 public class MergeNode implements SvcLogicJavaPlugin {
43 private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeNode.class);
45 public void mergeDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
46 throws SvcLogicException {
50 public void mergeJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
51 throws SvcLogicException {
52 log.info("Received mergeJsonDataOnTemplate call with params : " + inParams);
53 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
55 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
56 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
57 if (StringUtils.isBlank(jsonData)) {
58 throw new Exception("JSON Data is missing");
61 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
62 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
64 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
65 throw new Exception("Template data or Template file is missing");
67 if (StringUtils.isBlank(templateData)) {
68 String path = MergeNode.class.getClassLoader().getResource(".").toString();
69 templateData = IOUtils.toString(
70 MergeNode.class.getClassLoader().getResourceAsStream(templateFile));
73 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
75 Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
76 log.info("Data Maps created :" + dataMap);
77 if (dataMap != null) {
78 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
79 if (mergedData != null) {
80 // Changed for E2E defect 266908 Quote issue
81 // ctx.setAttribute(responsePrefix +
82 // ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
84 responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
85 EscapeUtils.unescapeSql(mergedData));
88 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
89 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
90 log.info("Data Merge Successful :" + ctx);
91 } catch (Exception e) {
92 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
93 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
94 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
96 log.error("Failed in merging data to template " + e.getMessage());
97 throw new SvcLogicException(e.getMessage());
101 public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
102 throws SvcLogicException {
103 // log.info("Received mergeJsonComplexDataOnTemplate call with params : " + inParams);
104 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
106 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
107 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
108 if (StringUtils.isBlank(jsonData)) {
109 throw new Exception("JSON Data is missing");
112 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
113 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
115 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
116 throw new Exception("Template data or Template file is missing");
118 if (StringUtils.isBlank(templateData)) {
119 // String path = MergeNode.class.getClassLoader().getResource(".").toString();
120 templateData = IOUtils.toString(
121 MergeNode.class.getClassLoader().getResourceAsStream(templateFile),
122 Charset.defaultCharset());
125 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
126 String doPrettyOutput =
127 inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
129 String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData,
130 templateType, doPrettyOutput);
131 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
134 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
135 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
136 // log.info("Data Merge Successful :" + ctx);
137 } catch (Exception e) {
138 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
139 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
140 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
142 log.error("Failed in merging data to template " + e.getMessage());
143 throw new SvcLogicException(e.getMessage());
147 public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
148 throws SvcLogicException {