diff options
author | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2022-11-10 11:03:00 +0000 |
---|---|---|
committer | Fiete Ostkamp <Fiete.Ostkamp@telekom.de> | 2022-11-10 11:03:00 +0000 |
commit | f8c058b2be718ab825f88e10e43ed59d5ca75de2 (patch) | |
tree | 7078532cf899cc9d618345aac2c865e233663e01 /aai-schema-gen | |
parent | a8e838713d168e7cd81c9765d5e5a56b88e48def (diff) |
Reduce the number of problems in schema-service
Issue-ID: AAI-3590
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Change-Id: I7a5d3e5a4db75efb8a8fcc6c8598d6cab80dca4a
Diffstat (limited to 'aai-schema-gen')
7 files changed, 201 insertions, 173 deletions
diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/AutoGenerateHtml.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/AutoGenerateHtml.java index ae5426f..9460175 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/AutoGenerateHtml.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/AutoGenerateHtml.java @@ -33,10 +33,14 @@ import java.util.ListIterator; import org.onap.aai.schemagen.swagger.GenerateSwagger; import org.onap.aai.setup.SchemaVersion; import org.onap.aai.setup.SchemaVersions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class AutoGenerateHtml { + private static Logger logger = LoggerFactory.getLogger(AutoGenerateHtml.class); private static final String AAI_GENERATE_VERSION = "aai.generate.version"; public static final String DEFAULT_SCHEMA_DIR = "../aai-schema"; // if the program is run from aai-common, use this directory as default" @@ -47,34 +51,37 @@ public class AutoGenerateHtml { public static void main(String[] args) throws IOException, TemplateException { String savedProperty = System.getProperty(AAI_GENERATE_VERSION); - AnnotationConfigApplicationContext ctx = - new AnnotationConfigApplicationContext("org.onap.aai.setup", "org.onap.aai.schemagen"); + try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + "org.onap.aai.setup", "org.onap.aai.schemagen")) { + SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); - SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); - - List<SchemaVersion> versionsToGen = schemaVersions.getVersions(); - Collections.sort(versionsToGen); - Collections.reverse(versionsToGen); - ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator(); - String schemaDir; - if (System.getProperty("user.dir") != null - && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) { - schemaDir = ALT_SCHEMA_DIR; - } else { - schemaDir = DEFAULT_SCHEMA_DIR; - } - String release = System.getProperty("aai.release", "onap"); - while (versionIterator.hasNext()) { - System.setProperty(AAI_GENERATE_VERSION, versionIterator.next().toString()); - String yamlFile = - schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" - + System.getProperty(AAI_GENERATE_VERSION) + ".yaml"; - File swaggerYamlFile = new File(yamlFile); - if (swaggerYamlFile.exists()) { - GenerateSwagger.schemaVersions = schemaVersions; - GenerateSwagger.main(args); + List<SchemaVersion> versionsToGen = schemaVersions.getVersions(); + Collections.sort(versionsToGen); + Collections.reverse(versionsToGen); + ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator(); + String schemaDir; + if (System.getProperty("user.dir") != null + && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) { + schemaDir = ALT_SCHEMA_DIR; + } else { + schemaDir = DEFAULT_SCHEMA_DIR; } + String release = System.getProperty("aai.release", "onap"); + while (versionIterator.hasNext()) { + System.setProperty(AAI_GENERATE_VERSION, versionIterator.next().toString()); + String yamlFile = + schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + + System.getProperty(AAI_GENERATE_VERSION) + ".yaml"; + File swaggerYamlFile = new File(yamlFile); + if (swaggerYamlFile.exists()) { + GenerateSwagger.schemaVersions = schemaVersions; + GenerateSwagger.main(args); + } + } + } catch (BeansException e) { + logger.warn("Unable to initialize AnnotationConfigApplicationContext ", e); } + System.setProperty(AAI_GENERATE_VERSION, savedProperty); } } diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/GenerateXsd.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/GenerateXsd.java index 490e290..2655e3c 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/GenerateXsd.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/GenerateXsd.java @@ -28,7 +28,11 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.onap.aai.schemagen.genxsd.HTMLfromOXM; import org.onap.aai.schemagen.genxsd.NodesYAMLfromOXM; @@ -37,12 +41,13 @@ import org.onap.aai.setup.SchemaVersion; import org.onap.aai.setup.SchemaVersions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.w3c.dom.NodeList; 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; @@ -149,170 +154,174 @@ public class GenerateXsd { fileTypeToGen = fileTypeToGen.toLowerCase(); } - AnnotationConfigApplicationContext ctx = - new AnnotationConfigApplicationContext("org.onap.aai.setup", "org.onap.aai.schemagen"); + try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + "org.onap.aai.setup", "org.onap.aai.schemagen")) { + SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); - SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); - - if (!fileTypeToGen.equals(GENERATE_TYPE_XSD) && !fileTypeToGen.equals(GENERATE_TYPE_YAML)) { - System.err.println("Invalid gen_type passed. " + fileTypeToGen); - System.exit(1); - } - - String responsesLabel = System.getProperty("yamlresponses_url"); - responsesUrl = responsesLabel; + if (!fileTypeToGen.equals(GENERATE_TYPE_XSD) + && !fileTypeToGen.equals(GENERATE_TYPE_YAML)) { + System.err.println("Invalid gen_type passed. " + fileTypeToGen); + System.exit(1); + } - List<SchemaVersion> versionsToGen = new ArrayList<>(); - if (!"ALL".equalsIgnoreCase(versionToGen) && !versionToGen.matches("v\\d+") - && !validVersion(versionToGen)) { - System.err.println("Invalid version passed. " + versionToGen); - System.exit(1); - } else if ("ALL".equalsIgnoreCase(versionToGen)) { - versionsToGen = schemaVersions.getVersions(); - Collections.sort(versionsToGen); - Collections.reverse(versionsToGen); - } else { - versionsToGen.add(new SchemaVersion(versionToGen)); - } + String responsesLabel = System.getProperty("yamlresponses_url"); + responsesUrl = responsesLabel; - // process file type System property - if (fileTypeToGen.equals(GENERATE_TYPE_YAML)) { - if (responsesUrl == null || responsesUrl.length() < 1 || responsesLabel == null - || responsesLabel.length() < 1) { - System.err.println( - "generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties"); + List<SchemaVersion> versionsToGen = new ArrayList<>(); + if (!"ALL".equalsIgnoreCase(versionToGen) && versionToGen != null + && !versionToGen.matches("v\\d+") && !validVersion(versionToGen)) { + System.err.println("Invalid version passed. " + versionToGen); System.exit(1); + } else if ("ALL".equalsIgnoreCase(versionToGen)) { + versionsToGen = schemaVersions.getVersions(); + Collections.sort(versionsToGen); + Collections.reverse(versionsToGen); } else { - responsesUrl = "description: " + "Response codes found in [response codes](" - + responsesLabel + ").\n"; + versionsToGen.add(new SchemaVersion(versionToGen)); } - } - /* - * TODO: Oxm Path is config driveb - */ - String oxmPath; - if (System.getProperty("user.dir") != null - && !System.getProperty("user.dir").contains(NORMAL_START_DIR)) { - oxmPath = BASE_AUTO_GEN_ROOT + NODE_DIR; - } else { - oxmPath = BASE_ROOT + NODE_DIR; - } - String outfileName = null; - File outfile; - String nodesfileName = null; - File nodesfile; - String fileContent = null; - String nodesContent = null; - - for (SchemaVersion v : versionsToGen) { - apiVersion = v.toString(); - logger.debug("YAMLdir = " + YAML_DIR); - logger.debug("Generating " + apiVersion + " " + fileTypeToGen); - apiVersionFmt = "." + apiVersion + "."; - generatedJavaType = new HashMap<String, String>(); - appliedPaths = new HashMap<String, String>(); - File edgeRuleFile = null; - String fileName = EDGE_DIR + "DbEdgeRules_" + apiVersion + ".json"; - logger.debug("user.dir = " + System.getProperty("user.dir")); + // process file type System property + if (fileTypeToGen.equals(GENERATE_TYPE_YAML)) { + if (responsesUrl == null || responsesUrl.length() < 1 || responsesLabel == null + || responsesLabel.length() < 1) { + System.err.println( + "generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties"); + System.exit(1); + } else { + responsesUrl = "description: " + "Response codes found in [response codes](" + + responsesLabel + ").\n"; + } + } + /* + * TODO: Oxm Path is config driven + */ + String oxmPath; if (System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(NORMAL_START_DIR)) { - fileName = BASE_AUTO_GEN_ROOT + fileName; - + oxmPath = BASE_AUTO_GEN_ROOT + NODE_DIR; } else { - fileName = BASE_ROOT + fileName; - + oxmPath = BASE_ROOT + NODE_DIR; } - edgeRuleFile = new File(fileName); - // Document doc = ni.getSchema(translateVersion(v)); - if (fileTypeToGen.equals(GENERATE_TYPE_XSD)) { - outfileName = XSD_DIR + "/aai_schema_" + apiVersion + "." + GENERATE_TYPE_XSD; - try { - HTMLfromOXM swagger = ctx.getBean(HTMLfromOXM.class); - swagger.setVersion(v); - fileContent = swagger.process(); - if (fileContent.startsWith("Schema format issue")) { - throw new Exception(fileContent); - } - } catch (Exception e) { - logger.error("Exception creating output file " + outfileName); - logger.error(e.getMessage()); - System.exit(-1); + String outfileName = null; + File outfile; + String nodesfileName = null; + File nodesfile; + String fileContent = null; + String nodesContent = null; + + for (SchemaVersion v : versionsToGen) { + apiVersion = v.toString(); + logger.debug("YAMLdir = " + YAML_DIR); + logger.debug("Generating " + apiVersion + " " + fileTypeToGen); + apiVersionFmt = "." + apiVersion + "."; + generatedJavaType = new HashMap<String, String>(); + appliedPaths = new HashMap<String, String>(); + File edgeRuleFile = null; + String fileName = EDGE_DIR + "DbEdgeRules_" + apiVersion + ".json"; + logger.debug("user.dir = " + System.getProperty("user.dir")); + if (System.getProperty("user.dir") != null + && !System.getProperty("user.dir").contains(NORMAL_START_DIR)) { + fileName = BASE_AUTO_GEN_ROOT + fileName; + + } else { + fileName = BASE_ROOT + fileName; + } - } else if (versionSupportsSwagger(apiVersion)) { - outfileName = YAML_DIR + "/aai_swagger_" + apiVersion + "." + GENERATE_TYPE_YAML; - nodesfileName = YAML_DIR + "/aai_swagger_" + apiVersion + "." + "nodes" + "." - + GENERATE_TYPE_YAML; - try { - YAMLfromOXM swagger = (YAMLfromOXM) ctx.getBean(YAMLfromOXM.class); - swagger.setVersion(v); - fileContent = swagger.process(); - Map combinedJavaTypes = swagger.getCombinedJavaTypes(); - NodesYAMLfromOXM nodesSwagger = ctx.getBean(NodesYAMLfromOXM.class); - nodesSwagger.setVersion(v); - nodesSwagger.setCombinedJavaTypes(combinedJavaTypes); - nodesContent = nodesSwagger.process(); - } catch (Exception e) { - logger.error("Exception creating output file " + outfileName, e); + edgeRuleFile = new File(fileName); + // Document doc = ni.getSchema(translateVersion(v)); + + if (fileTypeToGen.equals(GENERATE_TYPE_XSD)) { + outfileName = XSD_DIR + "/aai_schema_" + apiVersion + "." + GENERATE_TYPE_XSD; + try { + HTMLfromOXM swagger = ctx.getBean(HTMLfromOXM.class); + swagger.setVersion(v); + fileContent = swagger.process(); + if (fileContent.startsWith("Schema format issue")) { + throw new Exception(fileContent); + } + } catch (Exception e) { + logger.error("Exception creating output file " + outfileName); + logger.error(e.getMessage()); + System.exit(-1); + } + } else if (versionSupportsSwagger(apiVersion)) { + outfileName = + YAML_DIR + "/aai_swagger_" + apiVersion + "." + GENERATE_TYPE_YAML; + nodesfileName = YAML_DIR + "/aai_swagger_" + apiVersion + "." + "nodes" + "." + + GENERATE_TYPE_YAML; + try { + YAMLfromOXM swagger = (YAMLfromOXM) ctx.getBean(YAMLfromOXM.class); + swagger.setVersion(v); + fileContent = swagger.process(); + Map<String, Integer> combinedJavaTypes = swagger.getCombinedJavaTypes(); + NodesYAMLfromOXM nodesSwagger = ctx.getBean(NodesYAMLfromOXM.class); + nodesSwagger.setVersion(v); + nodesSwagger.setCombinedJavaTypes(combinedJavaTypes); + nodesContent = nodesSwagger.process(); + } catch (Exception e) { + logger.error("Exception creating output file " + outfileName, e); + } + } else { + continue; } - } else { - continue; - } - outfile = new File(outfileName); - File parentDir = outfile.getParentFile(); - if (!parentDir.exists()) { - parentDir.mkdirs(); - } - if (nodesfileName != null) { - BufferedWriter nodesBW = null; - nodesfile = new File(nodesfileName); - parentDir = nodesfile.getParentFile(); + outfile = new File(outfileName); + 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 { + if (!nodesfile.createNewFile()) { + logger.error("File {} already exist", nodesfileName); + } + } catch (IOException e) { + logger.error("Exception creating output file " + nodesfileName, e); + } + try { + Charset charset = StandardCharsets.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); + } finally { + if (nodesBW != null) { + nodesBW.close(); + } + } + } + try { - if (!nodesfile.createNewFile()) { - logger.error("File {} already exist", nodesfileName); + if (!outfile.createNewFile()) { + logger.error("File {} already exist", outfileName); } } catch (IOException e) { - logger.error("Exception creating output file " + nodesfileName, e); + logger.error("Exception creating output file " + outfileName, e); } + BufferedWriter bw = null; try { Charset charset = StandardCharsets.UTF_8; - Path path = Paths.get(nodesfileName); - nodesBW = Files.newBufferedWriter(path, charset); - nodesBW.write(nodesContent); + Path path = Paths.get(outfileName); + bw = Files.newBufferedWriter(path, charset); + bw.write(fileContent); } catch (IOException e) { logger.error("Exception writing output file " + outfileName, e); } finally { - if (nodesBW != null) { - nodesBW.close(); + if (bw != null) { + bw.close(); } } + logger.debug("GeneratedXSD successful, saved in " + outfileName); } - - try { - if (!outfile.createNewFile()) { - logger.error("File {} already exist", outfileName); - } - } catch (IOException e) { - logger.error("Exception creating output file " + outfileName, e); - } - BufferedWriter bw = null; - try { - Charset charset = StandardCharsets.UTF_8; - Path path = Paths.get(outfileName); - bw = Files.newBufferedWriter(path, charset); - bw.write(fileContent); - } catch (IOException e) { - logger.error("Exception writing output file " + outfileName, e); - } finally { - if (bw != null) { - bw.close(); - } - } - logger.debug("GeneratedXSD successful, saved in " + outfileName); + } catch (BeansException e) { + logger.warn("Unable to initialize AnnotationConfigApplicationContext ", e); } } diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/HTMLfromOXM.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/HTMLfromOXM.java index 980a3f3..c1cf5ac 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/HTMLfromOXM.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/HTMLfromOXM.java @@ -106,7 +106,7 @@ public class HTMLfromOXM extends OxmFileProcessor { StringBuilder sbInventory = new StringBuilder(); Element elem; String javaTypeName; - combinedJavaTypes = new HashMap(); + combinedJavaTypes = new HashMap<>(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { elem = (Element) javaTypeNodes.item(i); javaTypeName = elem.getAttribute("name"); diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java index 8bceb5c..77c76cb 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java @@ -461,7 +461,7 @@ public class NodesYAMLfromOXM extends OxmFileProcessor { } pathParams.append(sbParameters); } - if (indexedProps.isEmpty() && containerProps.isEmpty()) { + if (indexedProps != null && indexedProps.isEmpty() && containerProps.isEmpty()) { NodeGetOperation get = new NodeGetOperation(useOpId, xmlRootElementName, tag, path, null); String operation = get.toString(); diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java index fde0877..150bab7 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java @@ -24,7 +24,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringReader; -import java.util.*; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -72,7 +71,7 @@ public abstract class OxmFileProcessor { protected Document doc = null; protected String apiVersion = null; protected SchemaVersions schemaVersions; - protected Map combinedJavaTypes; + protected Map<String, Integer> combinedJavaTypes; protected String apiVersionFmt = null; protected List<String> topLevelPaths = new ArrayList<String>(); protected HashMap<String, String> generatedJavaType = new HashMap<String, String>(); @@ -299,11 +298,11 @@ public abstract class OxmFileProcessor { return null; } - public Map getCombinedJavaTypes() { + public Map<String, Integer> getCombinedJavaTypes() { return combinedJavaTypes; } - public void setCombinedJavaTypes(Map combinedJavaTypes) { + public void setCombinedJavaTypes(Map<String, Integer> combinedJavaTypes) { this.combinedJavaTypes = combinedJavaTypes; } @@ -479,7 +478,7 @@ public abstract class OxmFileProcessor { int useElement = -1; if (combinedJavaTypes.containsKey(javaTypeName)) { - return combineElementList.get((int) combinedJavaTypes.get(javaTypeName)); + return combineElementList.get(combinedJavaTypes.get(javaTypeName)); } for (int i = 0; i < combineElementList.size(); ++i) { javaTypeElement = combineElementList.get(i); diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java index ff6f7dd..3b8fa4d 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java @@ -20,7 +20,6 @@ package org.onap.aai.schemagen.genxsd; -import com.google.common.base.CharMatcher; import com.google.common.collect.Multimap; import java.io.BufferedWriter; @@ -153,7 +152,7 @@ public class YAMLfromOXM extends OxmFileProcessor { StringBuilder definitionsSb = new StringBuilder(); Element elem; String javaTypeName; - combinedJavaTypes = new HashMap(); + combinedJavaTypes = new HashMap<>(); for (int i = 0; i < javaTypeNodes.getLength(); ++i) { elem = (Element) javaTypeNodes.item(i); javaTypeName = elem.getAttribute("name"); diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/GenerateSwagger.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/GenerateSwagger.java index 39b1e83..b467e33 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/GenerateSwagger.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/GenerateSwagger.java @@ -27,8 +27,22 @@ import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; -import java.io.*; -import java.util.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; import java.util.stream.Collectors; import org.onap.aai.setup.SchemaVersions; |