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 com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.nio.charset.Charset;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
36 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
37 import org.onap.sdnc.config.generator.tool.EscapeUtils;
38 import org.onap.sdnc.config.generator.tool.JSONTool;
39 import org.onap.sdnc.config.generator.tool.MergeTool;
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) throws SvcLogicException {
46 /* TODO implement this method */
49 public void mergeJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
50 throws SvcLogicException {
51 log.info("Received mergeJsonDataOnTemplate call with params : " + inParams);
52 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
54 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
55 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
56 if (StringUtils.isBlank(jsonData)) {
57 throw new ParameterMissingException("JSON Data is missing");
60 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
61 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
63 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
64 throw new ParameterMissingException("Template data or Template file is missing");
66 if (StringUtils.isBlank(templateData)) {
67 templateData = IOUtils.toString(
68 MergeNode.class.getClassLoader().getResourceAsStream(templateFile), "UTF-8");
71 Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
72 log.info("Data Maps created :" + dataMap);
73 trySetContextAttribute(ctx, responsePrefix, templateData, dataMap);
74 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
75 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
76 log.info("Data Merge Successful :" + ctx);
77 } catch (Exception e) {
78 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
79 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
80 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
82 log.error("Failed in merging data to template", e);
83 throw new SvcLogicException(e.getMessage());
87 private void trySetContextAttribute(SvcLogicContext ctx, String responsePrefix, String templateData,
88 Map<String, String> dataMap) {
89 if (dataMap != null) {
90 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
91 if (mergedData != null) {
92 // Changed for E2E defect 266908 Quote issue
94 responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
95 EscapeUtils.unescapeSql(mergedData));
100 public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
101 throws SvcLogicException {
102 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
104 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
105 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
106 if (StringUtils.isBlank(jsonData)) {
107 throw new ParameterMissingException("JSON Data is missing");
110 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
111 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
113 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
114 throw new ParameterMissingException("Template data or Template file is missing");
116 if (StringUtils.isBlank(templateData)) {
117 templateData = IOUtils.toString(
118 MergeNode.class.getClassLoader().getResourceAsStream(templateFile),
119 Charset.defaultCharset());
122 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
123 String doPrettyOutput =
124 inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
126 String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData,
127 templateType, doPrettyOutput);
128 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
131 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
132 ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
133 } catch (Exception e) {
134 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
135 ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
136 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
138 log.error("Failed in merging data to template", e);
139 throw new SvcLogicException(e.getMessage());
143 public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
144 /* TODO implement this method */