Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / GenerateXsd.java
index 63318da..d94457e 100644 (file)
@@ -7,50 +7,70 @@
  * 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
- * 
+ *
+ *    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=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
-
 package org.onap.aai.util;
 
-import org.onap.aai.introspection.Version;
-import org.onap.aai.util.genxsd.EdgeRuleSet;
+
+import org.onap.aai.config.SpringContextAware;
+import org.onap.aai.setup.SchemaVersion;
+import org.onap.aai.setup.SchemaVersions;
 import org.onap.aai.util.genxsd.HTMLfromOXM;
+import org.onap.aai.util.genxsd.NodesYAMLfromOXM;
+
 import org.onap.aai.util.genxsd.YAMLfromOXM;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.w3c.dom.*;
+
 
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.*;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public class GenerateXsd {
        
-       private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class");
+       private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class");      
        protected static String apiVersion = null;
+       public static AnnotationConfigApplicationContext ctx = null;
        static String apiVersionFmt = null;
        static boolean useAnnotationsInXsd = false;
        static String responsesUrl = null;
        static String responsesLabel = null;
        static String jsonEdges = null;
-       static EdgeRuleSet edgeRuleSet = null;
-
        static Map<String, String> generatedJavaType;
        static Map<String, String> appliedPaths;
+       static String RELEASE = System.getProperty("aai.release", "onap");
+
+
+       static NodeList javaTypeNodes;
+       static Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions();
+    private static Map<String, String> createJavaTypeDefinitions()
+    {
+       StringBuffer aaiInternal = new StringBuffer();
+       Map<String,String> javaTypeDefinitions = new HashMap<String, String>();
+       aaiInternal.append("  aai-internal:\n");
+       aaiInternal.append("    properties:\n");
+       aaiInternal.append("      property-name:\n");
+       aaiInternal.append("        type: string\n");
+       aaiInternal.append("      property-value:\n");
+       aaiInternal.append("        type: string\n");
+//     javaTypeDefinitions.put("aai-internal", aaiInternal.toString());
+       return javaTypeDefinitions;
+    }
 
        public static final int VALUE_NONE = 0;
        public static final int VALUE_DESCRIPTION = 1;
@@ -60,26 +80,33 @@ public class GenerateXsd {
        private static final String generateTypeXSD = "xsd";
        private static final String generateTypeYAML = "yaml";
        
-       private static final String root = "../aai-schema/src/main/resources";
-       private static final String autoGenRoot = "aai-schema/src/main/resources";
-       private static final String normalStartDir = "aai-core";
-       private static final String xsd_dir = root + "/aai_schema";
-       private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml";
+       private final static String nodeDir = System.getProperty("nodes.configuration.location");
+       private final static String edgeDir = System.getProperty("edges.configuration.location");
+       private static final String baseRoot = "aai-schema/";
+       private static final String baseAutoGenRoot = "aai-schema/";
+       
+       private static final String root = baseRoot + "src/main/resources";
+       private static final String autoGenRoot = baseAutoGenRoot + "src/main/resources";
        
+       private static final String normalStartDir = "aai-core";
+       private static final String xsd_dir = root + "/" + RELEASE +"/aai_schema";
+
+       private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/" + RELEASE + "/aai_swagger_yaml";
+
        /* These three strings are for yaml auto-generation from aai-common class*/
-//     private static final String alt_yaml_dir = autoGenRoot + "/aai_swagger_yaml";
 
-       private static int annotationsStartVersion = 9; // minimum version to support annotations in xsd
-       private static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation
+       private static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation
+
        
        private static boolean validVersion(String versionToGen) {
                
                if ("ALL".equalsIgnoreCase(versionToGen)) {
                        return true;
                }
-               
-               for (Version v : Version.values()) {
-               if (v.name().equals(versionToGen)) {
+
+               SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
+               for (SchemaVersion v : schemaVersions.getVersions()) {
+               if (v.equals(versionToGen)) {
                    return true;
                }
            }
@@ -87,13 +114,6 @@ public class GenerateXsd {
            return false;
        }
        
-       private static boolean versionUsesAnnotations( String version) {
-               if (new Integer(version.substring(1)).intValue() >= annotationsStartVersion ) {
-                       return true;
-               }
-               return false;
-       }
-       
        private static boolean versionSupportsSwagger( String version) {
                if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) {
                        return true;
@@ -112,10 +132,17 @@ public class GenerateXsd {
        public static String getResponsesUrl() {
                return responsesUrl;
        }
-
        public static void main(String[] args) throws IOException {
                String versionToGen = System.getProperty("gen_version").toLowerCase();
                String fileTypeToGen = System.getProperty("gen_type").toLowerCase();
+
+               AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
+                               "org.onap.aai.config",
+                               "org.onap.aai.setup"
+               );
+
+               SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);
+
                if ( fileTypeToGen == null ) {
                        fileTypeToGen = generateTypeXSD;
                }
@@ -125,11 +152,10 @@ public class GenerateXsd {
                        System.exit(1);
                }
                
-               
                String responsesLabel = System.getProperty("yamlresponses_url");
                responsesUrl = responsesLabel;
                
-               List<Version> versionsToGen = new ArrayList<>();
+               List<SchemaVersion> versionsToGen = new ArrayList<>();
                if ( versionToGen == null ) {
                        System.err.println("Version is required, ie v<n> or ALL.");
                        System.exit(1);                 
@@ -139,11 +165,11 @@ public class GenerateXsd {
                        System.exit(1);
                }
                else if ("ALL".equalsIgnoreCase(versionToGen)) {
-                       versionsToGen = Arrays.asList(Version.values());
+                       versionsToGen = schemaVersions.getVersions();
                        Collections.sort(versionsToGen);
                        Collections.reverse(versionsToGen);
                } else {
-                       versionsToGen.add(Version.getVersion(versionToGen));
+                       versionsToGen.add(new SchemaVersion(versionToGen));
                }
                
                //process file type System property
@@ -160,40 +186,51 @@ public class GenerateXsd {
                                responsesUrl = "description: "+ "Response codes found in [response codes]("+responsesLabel+ ").\n";
                        }
                }
+               /*
+                * TODO: Oxm Path is config driveb
+                */
                String oxmPath;
                if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) {
-                       oxmPath = autoGenRoot + "/oxm/";
+                       oxmPath = baseAutoGenRoot + nodeDir;
                }
                else {
-                       oxmPath = root + "/oxm/";
+                       oxmPath = baseRoot + nodeDir;
                }
 
-               String outfileName;
+               String outfileName = null;
                File outfile;
+               String nodesfileName = null;
+               File nodesfile;
                String fileContent = null;
-               
-               for (Version v : versionsToGen) {
+               String nodesContent = null;
+
+
+               for (SchemaVersion v : versionsToGen) {
                        apiVersion = v.toString();
                        logger.debug("YAMLdir = "+yaml_dir);
                        logger.debug("Generating " + apiVersion + " " + fileTypeToGen);
-                       File oxm_file = new File(oxmPath + "aai_oxm_" + apiVersion + ".xml");
                        apiVersionFmt = "." + apiVersion + ".";
                        generatedJavaType = new HashMap<String, String>();
                        appliedPaths = new HashMap<String, String>();
                        File edgeRuleFile = null;
+                       String fileName = edgeDir + "DbEdgeRules_" + apiVersion + ".json";
                        logger.debug("user.dir = "+System.getProperty("user.dir"));
                        if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) {
-                               edgeRuleFile = new File(normalStartDir + "/src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json");
+                               fileName = baseAutoGenRoot + fileName;
+                               
                        }
                        else {
-                               edgeRuleFile = new File("src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json");
+                               fileName = baseRoot + fileName;
+                               
                        }
-                       
+                       edgeRuleFile = new File( fileName);
+//                     Document doc = ni.getSchema(translateVersion(v));
+
                        if ( fileTypeToGen.equals(generateTypeXSD) ) {
-                               useAnnotationsInXsd = versionUsesAnnotations(apiVersion);
                                outfileName = xsd_dir + "/aai_schema_" + apiVersion + "." + generateTypeXSD;
                                try {
-                                       HTMLfromOXM swagger = new HTMLfromOXM(oxm_file, v);
+                                       HTMLfromOXM swagger = ctx.getBean(HTMLfromOXM.class);
+                                       swagger.setVersion(v);
                                        fileContent = swagger.process();
                                } catch(Exception e) {
                                logger.error( "Exception creating output file " + outfileName);
@@ -202,12 +239,16 @@ public class GenerateXsd {
                                }
                        } else if ( versionSupportsSwagger(apiVersion )) {
                                outfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + generateTypeYAML;
-                               try {
-                                       YAMLfromOXM swagger = new YAMLfromOXM(oxm_file, v, edgeRuleFile);
+                               nodesfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + "nodes"+"."+generateTypeYAML;
+                               try {                                   
+                                       YAMLfromOXM swagger = (YAMLfromOXM) ctx.getBean(YAMLfromOXM.class);
+                                       swagger.setVersion(v);
                                        fileContent = swagger.process();
+                                       NodesYAMLfromOXM nodesSwagger = ctx.getBean(NodesYAMLfromOXM.class);
+                                       nodesSwagger.setVersion(v);
+                                       nodesContent = nodesSwagger.process();
                                } catch(Exception e) {
                                logger.error( "Exception creating output file " + outfileName);
-                               logger.error( e.getMessage());
                                e.printStackTrace();
                                }
                        } else {
@@ -217,7 +258,33 @@ public class GenerateXsd {
                        File parentDir = outfile.getParentFile();
                        if(! parentDir.exists()) 
                              parentDir.mkdirs();
-               
+                       if(nodesfileName != null) {
+                               BufferedWriter nodesBW = null;
+                               nodesfile = new File(nodesfileName);
+                               parentDir = nodesfile.getParentFile();
+                               if(! parentDir.exists()) 
+                                     parentDir.mkdirs();
+                           try {
+                               nodesfile.createNewFile();
+                           } catch (IOException e) {
+                               logger.error( "Exception creating output file " + nodesfileName);
+                               e.printStackTrace();
+                           }
+                       try {
+                               Charset charset = Charset.forName("UTF-8");
+                               Path path = Paths.get(nodesfileName);
+                               nodesBW = Files.newBufferedWriter(path, charset);
+                               nodesBW.write(nodesContent);
+                       } catch ( IOException e) {
+                               logger.error( "Exception writing output file " + outfileName);
+                               e.printStackTrace();
+                       } finally {
+                               if ( nodesBW != null ) {
+                                       nodesBW.close();
+                               }
+                       }
+                       }
+                                       
                    try {
                        outfile.createNewFile();
                    } catch (IOException e) {
@@ -242,4 +309,7 @@ public class GenerateXsd {
                }
                
        }
-}
\ No newline at end of file
+
+}
+
+        
\ No newline at end of file