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