First part of onap rename
[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 : APP-C
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdnc.config.generator.tool;
22
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.util.Iterator;
26 import java.util.Map;
27
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;
37
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;
44
45
46 public class MergeTool {
47
48     private static final  EELFLogger log = EELFManager.getInstance().getLogger(MergeTool.class);
49
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");
57         ve.init();
58
59         StringResourceRepository repo = (StringResourceRepository)ve.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
60         repo.putStringResource("TemplateResource", template);
61
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());
68         }
69         t.merge(context, writer);
70         return writer.toString();
71     }
72
73
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)){    
77             Velocity.init();
78
79             ObjectMapper mapper = new ObjectMapper();        
80             CustomJsonNodeFactory f = new CustomJsonNodeFactory();        
81             mapper.setNodeFactory(f);
82
83             JsonNode jsonObj = mapper.readValue(jsonData, JsonNode.class);    
84
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));
90             }            
91
92             StringWriter writer = new StringWriter();            
93             Velocity.evaluate(context, writer, "TemplateData", template);
94             writer.flush();
95             mergedData =  writer.toString();    
96
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)) ){
101                 // Perform Prettying
102
103             }
104         }
105         return mergedData;
106
107     }
108
109 }