2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.config.generator.merge;
23 import java.nio.charset.Charset;
26 import org.apache.commons.io.IOUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
29 import org.openecomp.sdnc.config.generator.tool.JSONTool;
30 import org.openecomp.sdnc.config.generator.tool.MergeTool;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
38 public class MergeNode implements SvcLogicJavaPlugin {
40 private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeNode.class);
42 public void mergeDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
46 public void mergeJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
47 log.info("Received mergeJsonDataOnTemplate call with params : " + inParams);
48 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
50 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
51 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
52 if(StringUtils.isBlank(jsonData)){
53 throw new Exception("JSON Data is missing");
56 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
57 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
59 if(StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)){
60 throw new Exception("Template data or Template file is missing");
62 if(StringUtils.isBlank(templateData)){
63 String path = MergeNode.class.getClassLoader().getResource(".").toString();
64 templateData = IOUtils.toString(MergeNode.class.getClassLoader().getResourceAsStream(templateFile));
67 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
69 Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
70 log.info("Data Maps created :" + dataMap);
72 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
73 if(mergedData != null){
74 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
77 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
78 log.info("Data Merge Successful :" + ctx);
79 } catch (Exception e) {
80 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
81 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
82 log.error("Failed in merging data to template " + e.getMessage());
83 throw new SvcLogicException(e.getMessage());
87 public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
88 //log.info("Received mergeJsonComplexDataOnTemplate call with params : " + inParams);
89 String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
91 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix+".") : "";
92 String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
93 if(StringUtils.isBlank(jsonData)){
94 throw new Exception("JSON Data is missing");
97 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
98 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
100 if(StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)){
101 throw new Exception("Template data or Template file is missing");
103 if(StringUtils.isBlank(templateData)){
104 //String path = MergeNode.class.getClassLoader().getResource(".").toString();
105 templateData = IOUtils.toString(MergeNode.class.getClassLoader().getResourceAsStream(templateFile), Charset.defaultCharset());
108 String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
109 String doPrettyOutput = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
111 String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData, templateType, doPrettyOutput);
112 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
114 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
115 //log.info("Data Merge Successful :" + ctx);
116 } catch (Exception e) {
117 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS, ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
118 ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,e.getMessage());
119 log.error("Failed in merging data to template " + e.getMessage());
120 throw new SvcLogicException(e.getMessage());
124 public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {