1126c2adb8341bb10c322a7a776ec737aa643795
[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.tool;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.core.JsonParseException;
30 import com.fasterxml.jackson.databind.JsonMappingException;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import java.io.IOException;
34 import java.io.StringWriter;
35 import java.util.Iterator;
36 import java.util.Map;
37 import org.apache.commons.lang3.StringUtils;
38 import org.apache.velocity.Template;
39 import org.apache.velocity.VelocityContext;
40 import org.apache.velocity.app.Velocity;
41 import org.apache.velocity.app.VelocityEngine;
42 import org.apache.velocity.runtime.RuntimeConstants;
43 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
44 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
45 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
46
47
48 public class 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         Iterator<Map.Entry<String, String>> entries = dataMap.entrySet().iterator();
68         while (entries.hasNext()) {
69             Map.Entry<String, String> entry = entries.next();
70             context.put(entry.getKey(), entry.getValue());
71         }
72         t.merge(context, writer);
73         return writer.toString();
74     }
75
76
77     public static String mergeJson2TemplateData(String template, String jsonData,
78         String templateType, String doPrettyOutput)
79         throws JsonParseException, JsonMappingException, IOException {
80         String mergedData = template;
81         if (StringUtils.isNotBlank(template) && StringUtils.isNotBlank(jsonData)) {
82             Velocity.init();
83
84             ObjectMapper mapper = new ObjectMapper();
85             CustomJsonNodeFactory f = new CustomJsonNodeFactory();
86             mapper.setNodeFactory(f);
87
88             JsonNode jsonObj = mapper.readValue(jsonData, JsonNode.class);
89
90             VelocityContext context = new VelocityContext();
91             Iterator<String> ii = jsonObj.fieldNames();
92             while (ii.hasNext()) {
93                 String key = ii.next();
94                 context.put(key, jsonObj.get(key));
95             }
96
97             StringWriter writer = new StringWriter();
98             Velocity.evaluate(context, writer, "TemplateData", template);
99             writer.flush();
100             mergedData = writer.toString();
101
102             if (StringUtils.isNotBlank(templateType) && StringUtils.isNotBlank(doPrettyOutput)
103                 && ConfigGeneratorConstant.Y.equalsIgnoreCase(doPrettyOutput)
104                 && (ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(templateType)
105                 || ConfigGeneratorConstant.DATA_TYPE_XML
106                 .equalsIgnoreCase(templateType))) {
107                 // Perform Prettying
108
109             }
110         }
111         return mergedData;
112
113     }
114
115 }