aboutsummaryrefslogtreecommitdiffstats
path: root/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java
diff options
context:
space:
mode:
authorPatrick Brady <pb071s@att.com>2017-12-13 11:19:06 -0800
committerPatrick Brady <pb071s@att.com>2017-12-13 11:19:17 -0800
commit781b1a6df324419c846c84ea983c18fc8362bfd3 (patch)
tree580008010dd50ca32db2ef6dc2e36628cf8c2b5b /appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java
parent161df8a94bb3b0c34ed16fd4fdba078bd1eeef9a (diff)
Third part of onap rename
This part of the commit changes the folder structure on all other folders of appc. Change-Id: I8acfa11cdfcdcd36be0e137245d1dd7324f1abd3 Signed-off-by: Patrick Brady <pb071s@att.com> Issue-ID: APPC-13
Diffstat (limited to 'appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java')
-rw-r--r--appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java128
1 files changed, 128 insertions, 0 deletions
diff --git a/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java b/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java
new file mode 100644
index 000000000..60fcd5ba8
--- /dev/null
+++ b/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLGenerator.java
@@ -0,0 +1,128 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APP-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.dg.loader;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.script.Invocable;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class DGXMLGenerator {
+ private final static Logger logger = LoggerFactory.getLogger(DGXMLGenerator.class);
+
+ public static String STRING_ENCODING = "utf-8";
+ public static String JS_INTERFACE_DG_CONVERTOR = "dgconverter";
+ public static String JS_METHOD_GET_NODE_TO_XML = "getNodeToXml";
+ public static String GENERATOR_TEMPLATE_FILE = "js/dg_xml2json.js";
+
+ public void generateXMLFromJSON(String jsonPath, String xmlpath, String propertyPath) throws Exception {
+ try{
+ ScriptEngineManager manager = new ScriptEngineManager();
+ ScriptEngine engine = manager.getEngineByName("JavaScript");
+ if (!(engine instanceof Invocable)) {
+ logger.error("Invoking methods is not supported.");
+ return;
+ }
+ Invocable inv = (Invocable) engine;
+ // engine.eval(new FileReader(DGXMLGenerator.class.getClassLoader().getResource(GENERATOR_TEMPLATE_FILE).getPath()));
+ String js = IOUtils.toString(DGXMLGenerator.class.getClassLoader().getResourceAsStream(GENERATOR_TEMPLATE_FILE),STRING_ENCODING);
+ engine.eval(js);
+
+ Object dgconverter = engine.get(JS_INTERFACE_DG_CONVERTOR);
+
+ List<File> files = new ArrayList<File>();
+ if(dgconverter != null){
+ File jsonPathFile = new File(jsonPath);
+ if(jsonPathFile.isDirectory()){
+ String[] extensions = new String[] { "json", "JSON" };
+ files = (List<File>) FileUtils.listFiles(jsonPathFile, extensions, true);
+ }else if(jsonPathFile.isFile()){
+ files.add(jsonPathFile);
+ }else{
+ throw new Exception("Failed to get the nature of the JSON path :"+ jsonPath);
+ }
+
+ logger.info("JSON Files identified "+ files.size());
+
+ if(files.size() > 0){
+ boolean isXmlPathDeleted = FileUtils.deleteQuietly(new File(xmlpath));
+ logger.info("Cleaning old DG XML under : " + xmlpath + ", delete status :" + isXmlPathDeleted);
+
+ for (File file : files) {
+ String dgJson = FileUtils.readFileToString(file,STRING_ENCODING);
+ logger.info("Generating XML from :" + file.getName());
+ String xmlFileName = xmlpath +"/"+file.getName().replace(".json", ".xml");
+
+ Object dgXMl = inv.invokeMethod(dgconverter, JS_METHOD_GET_NODE_TO_XML, dgJson);
+ // Write the XML File
+ if(dgXMl != null){
+ File xmlFile = new File(xmlFileName);
+ FileUtils.writeStringToFile(xmlFile, dgXMl.toString(), STRING_ENCODING);
+ logger.info("Generated XML File under :" + xmlFile.getCanonicalPath());
+ }
+ }
+
+ }else{
+ logger.info("No JSON Files to generate XML");
+ }
+ }else{
+ logger.error("Couldn't get Java Script Engine..");
+ }
+ }catch (Exception e) {
+ logger.error("Failed to generate generateXMLFromJSON :"+e.getMessage());
+ }
+ }
+
+
+ public static void main(String[] args) {
+ try {
+ DGXMLGenerator application = new DGXMLGenerator();
+ String jsonPath = null;
+ String xmlPath = null;
+ String propertyPath = null;
+ // Generate, GenerateLoad, GenerateLoadActivate
+ //args = new String[]{"src/main/resources/json","src/test/resources/xml"};
+ logger.info("DGXML Conversion Started with arguments :"+ args[0] +":"+ args[1]);
+ if(args != null && args.length >= 2){
+ jsonPath = args[0];
+ xmlPath = args[1];
+ }else{
+ throw new Exception("Sufficient inputs are missing <jsonPath> <xmlPath>");
+ }
+
+ application.generateXMLFromJSON(jsonPath, xmlPath, propertyPath);
+ logger.info("DGXML Conversion Completed...");
+ } catch (Exception e) {
+ logger.error("Failed in DG XML Generation :"+e.getMessage());
+ e.printStackTrace();
+ }
+
+ }
+
+}