appc-config-generator-provider sonar fixes part 2
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / tool / MergeTool.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.tool;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import java.io.IOException;
32 import java.io.StringWriter;
33 import java.util.Iterator;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import org.apache.commons.lang3.StringUtils;
37 import org.apache.velocity.Template;
38 import org.apache.velocity.VelocityContext;
39 import org.apache.velocity.app.Velocity;
40 import org.apache.velocity.app.VelocityEngine;
41 import org.apache.velocity.runtime.RuntimeConstants;
42 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
43 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
44 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
45
46 public class MergeTool {
47
48     private MergeTool() {}
49
50     private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeTool.class);
51
52     public static String mergeMap2TemplateData(String template, Map<String, String> dataMap) {
53         log.info("MergeMap2TemplateData Template :" + template + " Maps :" + dataMap);
54         StringWriter writer = new StringWriter();
55         VelocityEngine ve = new VelocityEngine();
56         ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "string");
57         ve.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
58         ve.addProperty("string.resource.loader.repository.static", "false");
59         ve.init();
60
61         StringResourceRepository repo = (StringResourceRepository) ve
62             .getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
63         repo.putStringResource("TemplateResource", template);
64
65         Template t = ve.getTemplate("TemplateResource");
66         VelocityContext context = new VelocityContext();
67         for (Entry<String, String> entry : dataMap.entrySet()) {
68             context.put(entry.getKey(), entry.getValue());
69         }
70         t.merge(context, writer);
71         return writer.toString();
72     }
73
74     public static String mergeJson2TemplateData(String template, String jsonData,
75         String templateType, String doPrettyOutput) throws IOException {
76
77         String mergedData = template;
78         if (StringUtils.isNotBlank(template) && StringUtils.isNotBlank(jsonData)) {
79             Velocity.init();
80
81             ObjectMapper mapper = new ObjectMapper();
82             CustomJsonNodeFactory f = new CustomJsonNodeFactory();
83             mapper.setNodeFactory(f);
84
85             JsonNode jsonObj = mapper.readValue(jsonData, JsonNode.class);
86
87             VelocityContext context = new VelocityContext();
88             Iterator<String> ii = jsonObj.fieldNames();
89             while (ii.hasNext()) {
90                 String key = ii.next();
91                 context.put(key, jsonObj.get(key));
92             }
93
94             StringWriter writer = new StringWriter();
95             Velocity.evaluate(context, writer, "TemplateData", template);
96             writer.flush();
97             mergedData = writer.toString();
98
99             if (prettyPrint(templateType, doPrettyOutput)) {
100                 // Perform Prettying
101             }
102         }
103         return mergedData;
104     }
105
106     private static boolean isJsonOrXml(String templateType) {
107         return ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(templateType)
108             || ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(templateType);
109     }
110
111     private static boolean prettyPrint(String templateType, String doPrettyOutput) {
112         return StringUtils.isNotBlank(templateType) && StringUtils.isNotBlank(doPrettyOutput)
113             && ConfigGeneratorConstant.Y.equalsIgnoreCase(doPrettyOutput) && isJsonOrXml(templateType);
114     }
115 }