48814ce6bfecbf3f1dd7c09f85f6277d3cf14d21
[appc.git] /
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 java.nio.charset.Charset;
28 import java.util.Map;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
35 import org.onap.sdnc.config.generator.tool.EscapeUtils;
36 import org.onap.sdnc.config.generator.tool.JSONTool;
37 import org.onap.sdnc.config.generator.tool.MergeTool;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
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)
46             throws SvcLogicException {
47
48     }
49
50     public void mergeJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
51             throws SvcLogicException {
52         log.info("Received mergeJsonDataOnTemplate call with params : " + inParams);
53         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
54         try {
55             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
56             String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
57             if (StringUtils.isBlank(jsonData)) {
58                 throw new Exception("JSON Data is missing");
59             }
60
61             String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
62             String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
63
64             if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
65                 throw new Exception("Template data or Template file is missing");
66             }
67             if (StringUtils.isBlank(templateData)) {
68                 String path = MergeNode.class.getClassLoader().getResource(".").toString();
69                 templateData = IOUtils.toString(
70                         MergeNode.class.getClassLoader().getResourceAsStream(templateFile));
71             }
72
73             String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
74
75             Map<String, String> dataMap = JSONTool.convertToProperties(jsonData);
76             log.info("Data Maps created :" + dataMap);
77             if (dataMap != null) {
78                 String mergedData = MergeTool.mergeMap2TemplateData(templateData, dataMap);
79                 if (mergedData != null) {
80                     // Changed for E2E defect 266908 Quote issue
81                     // ctx.setAttribute(responsePrefix +
82                     // ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,mergedData);
83                     ctx.setAttribute(
84                             responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
85                             EscapeUtils.unescapeSql(mergedData));
86                 }
87             }
88             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
89                     ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
90             log.info("Data Merge Successful :" + ctx);
91         } catch (Exception e) {
92             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
93                     ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
94             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
95                     e.getMessage());
96             log.error("Failed in merging data to template " + e.getMessage());
97             throw new SvcLogicException(e.getMessage());
98         }
99     }
100
101     public void mergeComplexJsonDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
102             throws SvcLogicException {
103         // log.info("Received mergeJsonComplexDataOnTemplate call with params : " + inParams);
104         String responsePrefix = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_RESPONSE_PRIFIX);
105         try {
106             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
107             String jsonData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_JSON_DATA);
108             if (StringUtils.isBlank(jsonData)) {
109                 throw new Exception("JSON Data is missing");
110             }
111
112             String templateData = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_DATA);
113             String templateFile = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_FILE);
114
115             if (StringUtils.isBlank(templateData) && StringUtils.isBlank(templateFile)) {
116                 throw new Exception("Template data or Template file is missing");
117             }
118             if (StringUtils.isBlank(templateData)) {
119                 // String path = MergeNode.class.getClassLoader().getResource(".").toString();
120                 templateData = IOUtils.toString(
121                         MergeNode.class.getClassLoader().getResourceAsStream(templateFile),
122                         Charset.defaultCharset());
123             }
124
125             String templateType = inParams.get(ConfigGeneratorConstant.INPUT_PARAM_TEMPLATE_TYPE);
126             String doPrettyOutput =
127                     inParams.get(ConfigGeneratorConstant.INPUT_PARAM_DO_PRETTY_OUTPUT);
128
129             String mergedData = MergeTool.mergeJson2TemplateData(templateData, jsonData,
130                     templateType, doPrettyOutput);
131             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_MERGED_DATA,
132                     mergedData);
133
134             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
135                     ConfigGeneratorConstant.OUTPUT_STATUS_SUCCESS);
136             // log.info("Data Merge Successful :" + ctx);
137         } catch (Exception e) {
138             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_STATUS,
139                     ConfigGeneratorConstant.OUTPUT_STATUS_FAILURE);
140             ctx.setAttribute(responsePrefix + ConfigGeneratorConstant.OUTPUT_PARAM_ERROR_MESSAGE,
141                     e.getMessage());
142             log.error("Failed in merging data to template " + e.getMessage());
143             throw new SvcLogicException(e.getMessage());
144         }
145     }
146
147     public void mergeYamlDataOnTemplate(Map<String, String> inParams, SvcLogicContext ctx)
148             throws SvcLogicException {
149
150     }
151
152 }