2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdnc.config.generator.tool;
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.util.Iterator;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.velocity.Template;
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.Velocity;
32 import org.apache.velocity.app.VelocityEngine;
33 import org.apache.velocity.runtime.RuntimeConstants;
34 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
35 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
36 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import com.fasterxml.jackson.core.JsonParseException;
41 import com.fasterxml.jackson.databind.JsonMappingException;
42 import com.fasterxml.jackson.databind.JsonNode;
43 import com.fasterxml.jackson.databind.ObjectMapper;
46 public class MergeTool {
48 private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeTool.class);
50 public static String mergeMap2TemplateData(String template, Map< String, String> dataMap ){
51 log.info("MergeMap2TemplateData Template :"+ template + " Maps :"+ dataMap);
52 StringWriter writer = new StringWriter();
53 VelocityEngine ve = new VelocityEngine();
54 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "string");
55 ve.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
56 ve.addProperty("string.resource.loader.repository.static", "false");
59 StringResourceRepository repo = (StringResourceRepository)ve.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
60 repo.putStringResource("TemplateResource", template);
62 Template t = ve.getTemplate("TemplateResource");
63 VelocityContext context = new VelocityContext();
64 Iterator<Map.Entry<String, String>> entries = dataMap.entrySet().iterator();
65 while (entries.hasNext()) {
66 Map.Entry<String, String> entry = entries.next();
67 context.put(entry.getKey(), entry.getValue());
69 t.merge(context, writer);
70 return writer.toString();
74 public static String mergeJson2TemplateData(String template, String jsonData, String templateType, String doPrettyOutput) throws JsonParseException, JsonMappingException, IOException{
75 String mergedData = template;
76 if( StringUtils.isNotBlank(template) && StringUtils.isNotBlank(jsonData)){
79 ObjectMapper mapper = new ObjectMapper();
80 CustomJsonNodeFactory f = new CustomJsonNodeFactory();
81 mapper.setNodeFactory(f);
83 JsonNode jsonObj = mapper.readValue(jsonData, JsonNode.class);
85 VelocityContext context = new VelocityContext();
86 Iterator<String> ii = jsonObj.fieldNames();
87 while (ii.hasNext()) {
88 String key = ii.next();
89 context.put(key, jsonObj.get(key));
92 StringWriter writer = new StringWriter();
93 Velocity.evaluate(context, writer, "TemplateData", template);
95 mergedData = writer.toString();
97 if(StringUtils.isNotBlank(templateType) && StringUtils.isNotBlank(doPrettyOutput)
98 && ConfigGeneratorConstant.Y.equalsIgnoreCase(doPrettyOutput)
99 && ( ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(templateType)
100 || ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(templateType)) ){