Changed to unmaintained
[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-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
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.sdnc.config.generator.merge;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.nio.charset.Charset;
29 import java.util.Map;
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;
39
40 public class MergeNode implements SvcLogicJavaPlugin {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeNode.class);
43
44     public void mergeDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
45         /* TODO implement this method */
46     }
47
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);
52         try {
53             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
54             String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
55             if (StringUtils.isBlank(jsonData)) {
56                 log.info("No JSON data to merge with template");
57             } else {
58                 String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
59                 String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
60
61                 if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
62                     throw new ParameterMissingException("Template data or Template file is missing");
63                 }
64                 if (StringUtils.isBlank(templateData)) {
65                     templateData = IOUtils.toString(
66                         MergeNode.class.getClassLoader().getResourceAsStream(templateFile), "UTF-8");
67                 }
68
69                 Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
70                 log.info("Data Maps created :" + dataMap);
71                 trySetContextAttribute(ctx, responsePrefix, templateData, dataMap);
72             }
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,
80                 e.getMessage());
81             log.error("Failed in merging data to template", e);
82             throw new SvcLogicException(e.getMessage());
83         }
84     }
85
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
92                 ctx.setAttribute(
93                     responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
94                     EscapeUtils.unescapeSql(mergedData));
95             }
96         }
97     }
98
99     public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
100         throws SvcLogicException {
101         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
102         try {
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");
107             }
108
109             String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
110             String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
111
112             if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
113                 throw new ParameterMissingException("Template data or Template file is missing");
114             }
115             if (StringUtils.isBlank(templateData)) {
116                 templateData = IOUtils.toString(
117                     MergeNode.class.getClassLoader().getResourceAsStream(templateFile),
118                     Charset.defaultCharset());
119             }
120
121             String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
122             String doPrettyOutput =
123                 inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
124
125             String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData,
126                 templateType, doPrettyOutput);
127             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
128                 mergedData);
129
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,
136                 e.getMessage());
137             log.error("Failed in merging data to template", e);
138             throw new SvcLogicException(e.getMessage());
139         }
140     }
141
142     public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
143         /* TODO implement this method */
144     }
145 }