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.dg.loader;
28 import java.security.InvalidParameterException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import javax.script.Invocable;
32 import javax.script.ScriptEngine;
33 import javax.script.ScriptEngineManager;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.io.IOUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 public class DGXMLGenerator {
41 private final static Logger logger = LoggerFactory.getLogger(DGXMLGenerator.class);
43 public static final String STRING_ENCODING = "utf-8";
44 public static final String JS_INTERFACE_DG_CONVERTOR = "dgconverter";
45 public static final String JS_METHOD_GET_NODE_TO_XML = "getNodeToXml";
46 public static final String GENERATOR_TEMPLATE_FILE = "js/dg_xml2json.js";
48 public void generateXMLFromJSON(String jsonPath, String xmlpath, String propertyPath)
49 throws InvalidParameterException {
51 ScriptEngineManager manager = new ScriptEngineManager();
52 ScriptEngine engine = manager.getEngineByName("JavaScript");
53 if (!(engine instanceof Invocable)) {
54 logger.error("Invoking methods is not supported.");
57 Invocable inv = (Invocable) engine;
59 // FileReader(DGXMLGenerator.class.getClassLoader().getResource(GENERATOR_TEMPLATE_FILE).getPath()));
60 String js = IOUtils.toString(DGXMLGenerator.class.getClassLoader()
61 .getResourceAsStream(GENERATOR_TEMPLATE_FILE), STRING_ENCODING);
64 Object dgconverter = engine.get(JS_INTERFACE_DG_CONVERTOR);
66 List<File> files = new ArrayList<File>();
67 if (dgconverter != null) {
68 File jsonPathFile = new File(jsonPath);
69 if (jsonPathFile.isDirectory()) {
70 String[] extensions = new String[] {"json", "JSON"};
71 files = (List<File>) FileUtils.listFiles(jsonPathFile, extensions, true);
72 } else if (jsonPathFile.isFile()) {
73 files.add(jsonPathFile);
75 throw new InvalidParameterException("Failed to get the nature of the JSON path :" + jsonPath);
78 logger.info("JSON Files identified " + files.size());
80 if (files.size() > 0) {
81 boolean isXmlPathDeleted = FileUtils.deleteQuietly(new File(xmlpath));
82 logger.info("Cleaning old DG XML under : " + xmlpath + ", delete status :"
85 for (File file : files) {
86 String dgJson = FileUtils.readFileToString(file, STRING_ENCODING);
87 logger.info("Generating XML from :" + file.getName());
89 xmlpath + "/" + file.getName().replace(".json", ".xml");
92 inv.invokeMethod(dgconverter, JS_METHOD_GET_NODE_TO_XML, dgJson);
95 File xmlFile = new File(xmlFileName);
96 FileUtils.writeStringToFile(xmlFile, dgXMl.toString(), STRING_ENCODING);
97 logger.info("Generated XML File under :" + xmlFile.getCanonicalPath());
102 logger.info("No JSON Files to generate XML");
105 logger.error("Couldn't get Java Script Engine..");
107 } catch (Exception e) {
108 logger.error("Failed to generate generateXMLFromJSON :" + e.getMessage());
113 public static void main(String[] args) {
115 DGXMLGenerator application = new DGXMLGenerator();
116 String jsonPath = null;
117 String xmlPath = null;
118 String propertyPath = null;
119 // Generate, GenerateLoad, GenerateLoadActivate
120 // args = new String[]{"src/main/resources/json","src/test/resources/xml"};
121 logger.info("DGXML Conversion Started with arguments :" + args[0] + ":" + args[1]);
122 if (args != null && args.length >= 2) {
126 throw new InvalidParameterException("Required inputs are missing <jsonPath> <xmlPath>");
129 application.generateXMLFromJSON(jsonPath, xmlPath, propertyPath);
130 logger.info("DGXML Conversion Completed...");
131 } catch (Exception e) {
132 logger.error("Failed in DG XML Generation :" + e.getMessage());