2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.generator.merge;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.nio.charset.Charset;
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
35 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
36 import org.onap.sdnc.config.generator.tool.EscapeUtils;
37 import org.onap.sdnc.config.generator.tool.JSONTool;
38 import org.onap.sdnc.config.generator.tool.MergeTool;
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 {
45 /* TODO implement this method */
48 public void mergeJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
49 throws SvcLogicException {
50 log.info("Received mergeJsonDataOnTemplate call with params : " + inParams);
51 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
53 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
54 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
55 if (StringUtils.isBlank(jsonData)) {
56 throw new ParameterMissingException("JSON Data is missing");
59 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
60 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
62 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
63 throw new ParameterMissingException("Template data or Template file is missing");
65 if (StringUtils.isBlank(templateData)) {
66 templateData = IOUtils.toString(
67 MergeNode.class.getClassLoader().getResourceAsStream(templateFile), "UTF-8");
70 Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
71 log.info("Data Maps created :" + dataMap);
72 trySetContextAttribute(ctx, responsePrefix, templateData, dataMap);
73 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
74 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
75 log.info("Data Merge Successful :" + ctx);
76 } catch (Exception e) {
77 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
78 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
79 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
81 log.error("Failed in merging data to template", e);
82 throw new SvcLogicException(e.getMessage());
86 private void trySetContextAttribute(SvcLogicContext ctx, String responsePrefix, String templateData,
87 Map<String, String> dataMap) {
88 if (dataMap != null) {
89 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
90 if (mergedData != null) {
91 // Changed for E2E defect 266908 Quote issue
93 responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
94 EscapeUtils.unescapeSql(mergedData));
99 public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
100 throws SvcLogicException {
101 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
103 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
104 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
105 if (StringUtils.isBlank(jsonData)) {
106 throw new ParameterMissingException("JSON Data is missing");
109 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
110 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
112 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
113 throw new ParameterMissingException("Template data or Template file is missing");
115 if (StringUtils.isBlank(templateData)) {
116 templateData = IOUtils.toString(
117 MergeNode.class.getClassLoader().getResourceAsStream(templateFile),
118 Charset.defaultCharset());
121 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
122 String doPrettyOutput =
123 inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
125 String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData,
126 templateType, doPrettyOutput);
127 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
130 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
131 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
132 } catch (Exception e) {
133 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
134 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
135 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
137 log.error("Failed in merging data to template", e);
138 throw new SvcLogicException(e.getMessage());
142 public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
143 /* TODO implement this method */