Change to CCSDK and ODL Carbon
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / openecomp / sdnc / config / generator / merge / MergeNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdnc.config.generator.merge;
22
23 import java.nio.charset.Charset;
24 import java.util.Map;
25
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;
31
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;
37
38 public class MergeNode implements SvcLogicJavaPlugin {
39
40     private static final  EELFLogger log = EELFManager.getInstance().getLogger(MergeNode.class);
41
42     public void mergeDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
43
44     }
45
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);
49         try{
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");
54             }
55
56             String templateData =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
57             String templateFile =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
58
59             if(StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)){
60                 throw new Exception("Template data or Template file is missing");
61             }
62             if(StringUtils.isBlank(templateData)){
63                 String path = MergeNode.class.getClassLoader().getResource(".").toString();
64                 templateData = IOUtils.toString(MergeNode.class.getClassLoader().getResourceAsStream(templateFile));
65             }
66
67             String templateType =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
68
69             Map<String, String> dataMap  = JSONTool.convertToProperties(jsonData);
70             log.info("Data Maps created :" + dataMap);
71             if(dataMap != null){
72                 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
73                 if(mergedData != null){
74                     ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
75                 }
76             }
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());
84         }
85     }
86     
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);
90         try{
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");
95             }
96
97             String templateData =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
98             String templateFile =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
99
100             if(StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)){
101                 throw new Exception("Template data or Template file is missing");
102             }
103             if(StringUtils.isBlank(templateData)){
104                 //String path = MergeNode.class.getClassLoader().getResource(".").toString();
105                 templateData = IOUtils.toString(MergeNode.class.getClassLoader().getResourceAsStream(templateFile), Charset.defaultCharset());
106             }
107
108             String templateType =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
109             String doPrettyOutput =  inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
110             
111             String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData, templateType, doPrettyOutput);
112             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
113             
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());
121         }
122     }
123
124     public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
125
126     }
127
128 }