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 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;
 
  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;
 
  46 public class MergeTool {
 
  48     private 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         for (Entry<String, String> entry : dataMap.entrySet()) {
 
  68             context.put(entry.getKey(), entry.getValue());
 
  70         t.merge(context, writer);
 
  71         return writer.toString();
 
  74     public static String mergeJson2TemplateData(String template, String jsonData,
 
  75         String templateType, String doPrettyOutput) throws IOException {
 
  77         String mergedData = template;
 
  78         if (StringUtils.isNotBlank(template) && StringUtils.isNotBlank(jsonData)) {
 
  81             ObjectMapper mapper = new ObjectMapper();
 
  82             CustomJsonNodeFactory f = new CustomJsonNodeFactory();
 
  83             mapper.setNodeFactory(f);
 
  85             JsonNode jsonObj = mapper.readValue(jsonData, JsonNode.class);
 
  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));
 
  94             StringWriter writer = new StringWriter();
 
  95             Velocity.evaluate(context, writer, "TemplateData", template);
 
  97             mergedData = writer.toString();
 
  99             if (prettyPrint(templateType, doPrettyOutput)) {
 
 106     private static boolean isJsonOrXml(String templateType) {
 
 107         return ConfigGeneratorConstant.DATA_TYPE_JSON.equalsIgnoreCase(templateType)
 
 108             || ConfigGeneratorConstant.DATA_TYPE_XML.equalsIgnoreCase(templateType);
 
 111     private static boolean prettyPrint(String templateType, String doPrettyOutput) {
 
 112         return StringUtils.isNotBlank(templateType) && StringUtils.isNotBlank(doPrettyOutput)
 
 113             && ConfigGeneratorConstant.Y.equalsIgnoreCase(doPrettyOutput) && isJsonOrXml(templateType);