3ffef14fe0fad59a0bcb29b671cd38d53a184bd7
[appc.git] /
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.dg.loader;
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.script.Invocable;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.io.IOUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 public class DGXMLGenerator {
37         private final static Logger logger = LoggerFactory.getLogger(DGXMLGenerator.class);
38
39         public static String STRING_ENCODING = "utf-8";
40         public static String JS_INTERFACE_DG_CONVERTOR = "dgconverter";
41         public static String JS_METHOD_GET_NODE_TO_XML = "getNodeToXml";
42         public static String GENERATOR_TEMPLATE_FILE = "js/dg_xml2json.js";
43
44         public void generateXMLFromJSON(String jsonPath, String xmlpath, String propertyPath) throws Exception {
45                 try{
46                         ScriptEngineManager manager = new ScriptEngineManager();
47                         ScriptEngine engine = manager.getEngineByName("JavaScript");
48                         if (!(engine instanceof Invocable)) {
49                                 logger.error("Invoking methods is not supported.");
50                                 return;
51                         }
52                         Invocable inv = (Invocable) engine;
53                         //              engine.eval(new FileReader(DGXMLGenerator.class.getClassLoader().getResource(GENERATOR_TEMPLATE_FILE).getPath()));
54                         String js = IOUtils.toString(DGXMLGenerator.class.getClassLoader().getResourceAsStream(GENERATOR_TEMPLATE_FILE),STRING_ENCODING);
55                         engine.eval(js);
56
57                         Object dgconverter = engine.get(JS_INTERFACE_DG_CONVERTOR);
58
59                         List<File> files = new ArrayList<File>();
60                         if(dgconverter != null){
61                                 File jsonPathFile = new File(jsonPath);
62                                 if(jsonPathFile.isDirectory()){
63                                         String[] extensions = new String[] { "json", "JSON" };
64                                         files = (List<File>) FileUtils.listFiles(jsonPathFile, extensions, true);
65                                 }else if(jsonPathFile.isFile()){
66                                         files.add(jsonPathFile);
67                                 }else{
68                                         throw new Exception("Failed to get the nature of the JSON path :"+ jsonPath);
69                                 }
70
71                                 logger.info("JSON Files identified "+ files.size());
72
73                                 if(files.size() > 0){
74                                         boolean isXmlPathDeleted = FileUtils.deleteQuietly(new File(xmlpath));
75                                         logger.info("Cleaning old DG XML under : " + xmlpath + ", delete status :" + isXmlPathDeleted);
76
77                                         for (File file : files) {
78                                                 String dgJson = FileUtils.readFileToString(file,STRING_ENCODING);
79                                                 logger.info("Generating XML from  :" + file.getName());
80                                                 String xmlFileName = xmlpath +"/"+file.getName().replace(".json", ".xml");
81
82                                                 Object dgXMl = inv.invokeMethod(dgconverter, JS_METHOD_GET_NODE_TO_XML, dgJson);
83                                                 // Write the XML File
84                                                 if(dgXMl != null){
85                                                         File xmlFile = new File(xmlFileName);
86                                                         FileUtils.writeStringToFile(xmlFile, dgXMl.toString(), STRING_ENCODING);
87                                                         logger.info("Generated XML File under  :" + xmlFile.getCanonicalPath());
88                                                 }
89                                         }
90
91                                 }else{
92                                         logger.info("No JSON Files to generate XML");
93                                 }
94                         }else{
95                                 logger.error("Couldn't get Java Script Engine..");
96                         }
97                 }catch (Exception e) {
98                         logger.error("Failed to generate generateXMLFromJSON :"+e.getMessage());
99                 }
100         }
101
102
103         public static void main(String[] args) {
104                 try {
105                         DGXMLGenerator application = new DGXMLGenerator();
106                         String jsonPath = null;
107                         String xmlPath = null;
108                         String propertyPath = null;
109                         // Generate, GenerateLoad, GenerateLoadActivate
110                         //args = new String[]{"src/main/resources/json","src/test/resources/xml"};
111                         logger.info("DGXML Conversion Started with arguments :"+ args[0] +":"+ args[1]);
112                         if(args != null && args.length >= 2){
113                                 jsonPath = args[0];
114                                 xmlPath = args[1];
115                         }else{
116                                 throw new Exception("Sufficient inputs are missing <jsonPath> <xmlPath>");
117                         }
118
119                         application.generateXMLFromJSON(jsonPath, xmlPath, propertyPath);
120                         logger.info("DGXML Conversion Completed...");
121                 } catch (Exception e) {
122                         logger.error("Failed in DG XML Generation :"+e.getMessage());
123                         e.printStackTrace();
124                 }
125
126         }
127
128 }