appc-config-generator-provider sonar fixes part 2
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / merge / MergeNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.generator.merge;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.nio.charset.Charset;
30 import java.util.Map;
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;
40
41 public class MergeNode implements SvcLogicJavaPlugin {
42
43     private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeNode.class);
44
45     public void mergeDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
46         /* TODO implement this method */
47     }
48
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);
53         try {
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");
58             }
59
60             String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
61             String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
62
63             if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
64                 throw new ParameterMissingException("Template data or Template file is missing");
65             }
66             if (StringUtils.isBlank(templateData)) {
67                 templateData = IOUtils.toString(
68                     MergeNode.class.getClassLoader().getResourceAsStream(templateFile), "UTF-8");
69             }
70
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,
81                 e.getMessage());
82             log.error("Failed in merging data to template", e);
83             throw new SvcLogicException(e.getMessage());
84         }
85     }
86
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
93                 ctx.setAttribute(
94                     responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
95                     EscapeUtils.unescapeSql(mergedData));
96             }
97         }
98     }
99
100     public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
101         throws SvcLogicException {
102         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
103         try {
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");
108             }
109
110             String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
111             String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
112
113             if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
114                 throw new ParameterMissingException("Template data or Template file is missing");
115             }
116             if (StringUtils.isBlank(templateData)) {
117                 templateData = IOUtils.toString(
118                     MergeNode.class.getClassLoader().getResourceAsStream(templateFile),
119                     Charset.defaultCharset());
120             }
121
122             String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
123             String doPrettyOutput =
124                 inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
125
126             String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData,
127                 templateType, doPrettyOutput);
128             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
129                 mergedData);
130
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,
137                 e.getMessage());
138             log.error("Failed in merging data to template", e);
139             throw new SvcLogicException(e.getMessage());
140         }
141     }
142
143     public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
144         /* TODO implement this method */
145     }
146 }