2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.sdnc.config.generator.tool;
27 import java.io.IOException;
28 import java.io.StringWriter;
29 import java.util.Iterator;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.velocity.Template;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.Velocity;
35 import org.apache.velocity.app.VelocityEngine;
36 import org.apache.velocity.runtime.RuntimeConstants;
37 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
38 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
39 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.fasterxml.jackson.core.JsonParseException;
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class MergeTool {
50 private static final EELFLogger log = EELFManager.getInstance().getLogger(MergeTool.class);
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");
61 StringResourceRepository repo = (StringResourceRepository) ve
62 .getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
63 repo.putStringResource("TemplateResource", template);
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());
72 t.merge(context, writer);
73 return writer.toString();
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)) {
84 ObjectMapper mapper = new ObjectMapper();
85 CustomJsonNodeFactory f = new CustomJsonNodeFactory();
86 mapper.setNodeFactory(f);
88 JsonNode jsonObj = mapper.readValue(jsonData, JsonNode.class);
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));
97 StringWriter writer = new StringWriter();
98 Velocity.evaluate(context, writer, "TemplateData", template);
100 mergedData = writer.toString();
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))) {