diff options
author | Kajur, Harish (vk250x) <vk250x@att.com> | 2018-12-13 08:37:13 -0500 |
---|---|---|
committer | Kajur, Harish (vk250x) <vk250x@att.com> | 2019-01-11 16:05:35 -0500 |
commit | d8393a8b7a51ed28e95cddac2350d6a20f31f953 (patch) | |
tree | ed0f594c75216030ab194340e710e11067d6119e /aai-schema-gen/src/main/java/org | |
parent | d64d789ad789e950c3b5e5443fb418a722b917c4 (diff) |
Move the aai-schema, annotations and
schema generation code from the aai-common
repo to the schema service repo as aai-schema
should be in the schema service and all code
related to it such as generation of the schema
code should also be moved to the service
Issue-ID: AAI-2002
Change-Id: Ia792d5ae8822642b1816a0e9664bb21fa511e24f
Signed-off-by: Kajur, Harish (vk250x) <vk250x@att.com>
Diffstat (limited to 'aai-schema-gen/src/main/java/org')
23 files changed, 5422 insertions, 0 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 new file mode 100644 index 0000000..85ecfad --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/AutoGenerateHtml.java @@ -0,0 +1,79 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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.onap.aai.schemagen; + +import freemarker.template.TemplateException; +import org.onap.aai.schemagen.swagger.GenerateSwagger; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; + +public class AutoGenerateHtml { + + 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" + public static final String ALT_SCHEMA_DIR = "aai-schema"; + //used to check to see if program is run from aai-schema-gen + public static final String DEFAULT_RUN_DIR = "aai-schema-gen"; + + 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" + ); + + + 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); + } + } + 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 new file mode 100644 index 0000000..227df63 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/GenerateXsd.java @@ -0,0 +1,318 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen; + + +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.schemagen.genxsd.HTMLfromOXM; +import org.onap.aai.schemagen.genxsd.NodesYAMLfromOXM; +import org.onap.aai.schemagen.genxsd.YAMLfromOXM; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.w3c.dom.NodeList; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class GenerateXsd { + + 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 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; + public static final int VALUE_INDEXED_PROPS = 2; + public static final int VALUE_CONTAINER = 3; + + private static final String generateTypeXSD = "xsd"; + private static final String generateTypeYAML = "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-schema-gen"; + 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 int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation + + + private static boolean validVersion(String versionToGen) { + + if ("ALL".equalsIgnoreCase(versionToGen)) { + return true; + } + + SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + for (SchemaVersion v : schemaVersions.getVersions()) { + if (v.equals(versionToGen)) { + return true; + } + } + + return false; + } + + private static boolean versionSupportsSwagger( String version) { + if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) { + return true; + } + return false; + } + + public static String getAPIVersion() { + return apiVersion; + } + + public static String getYamlDir() { + return yaml_dir; + } + + 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.setup", + "org.onap.aai.schemagen" + ); + + SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); + + if ( fileTypeToGen == null ) { + fileTypeToGen = generateTypeXSD; + } + + if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) { + System.err.println("Invalid gen_type passed. " + fileTypeToGen); + System.exit(1); + } + + String responsesLabel = System.getProperty("yamlresponses_url"); + responsesUrl = responsesLabel; + + List<SchemaVersion> versionsToGen = new ArrayList<>(); + if ( versionToGen == null ) { + System.err.println("Version is required, ie v<n> or ALL."); + System.exit(1); + } + else 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)); + } + + //process file type System property + fileTypeToGen = (fileTypeToGen == null ? generateTypeXSD : fileTypeToGen.toLowerCase()); + if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) { + System.err.println("Invalid gen_type passed. " + fileTypeToGen); + System.exit(1); + } else if ( fileTypeToGen.equals(generateTypeYAML) ) { + 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 driveb + */ + String oxmPath; + if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) { + oxmPath = baseAutoGenRoot + nodeDir; + } + else { + oxmPath = baseRoot + nodeDir; + } + + 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 = edgeDir + "DbEdgeRules_" + apiVersion + ".json"; + logger.debug("user.dir = "+System.getProperty("user.dir")); + if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) { + fileName = baseAutoGenRoot + fileName; + + } + else { + fileName = baseRoot + fileName; + + } + edgeRuleFile = new File( fileName); +// Document doc = ni.getSchema(translateVersion(v)); + + if ( fileTypeToGen.equals(generateTypeXSD) ) { + outfileName = xsd_dir + "/aai_schema_" + apiVersion + "." + generateTypeXSD; + 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()); + e.printStackTrace(); + System.exit(-1); + } + } else if ( versionSupportsSwagger(apiVersion )) { + outfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + generateTypeYAML; + nodesfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + "nodes"+"."+generateTypeYAML; + 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.printStackTrace(); + } + } 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(); + 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) { + logger.error( "Exception creating output file " + outfileName); + e.printStackTrace(); + } + BufferedWriter bw = null; + try { + Charset charset = Charset.forName("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.printStackTrace(); + } finally { + if ( bw != null ) { + bw.close(); + } + } + logger.debug( "GeneratedXSD successful, saved in " + outfileName); + } + + } + +} + diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SchemaConfiguration.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SchemaConfiguration.java new file mode 100644 index 0000000..26ff4b6 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SchemaConfiguration.java @@ -0,0 +1,50 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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.onap.aai.schemagen; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.AAIConfigTranslator; +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.SchemaVersions; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SchemaConfiguration { + + @Bean + public EdgeIngestor edgeIngestor(SchemaLocationsBean schemaLocationsBean, SchemaVersions schemaVersions){ + return new EdgeIngestor(configTranslator(schemaLocationsBean, schemaVersions), schemaVersions); + } + + @Bean(name = "nodeIngestor") + public NodeIngestor nodeIngestor(ConfigTranslator configTranslator) { + return new NodeIngestor(configTranslator); + } + + @Bean(name = "configTranslator") + public ConfigTranslator configTranslator(SchemaLocationsBean schemaLocationsBean, SchemaVersions schemaVersions) { + return new AAIConfigTranslator(schemaLocationsBean, schemaVersions); + } +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SpringContextAware.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SpringContextAware.java new file mode 100644 index 0000000..48cd46d --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SpringContextAware.java @@ -0,0 +1,64 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen; + + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class SpringContextAware implements ApplicationContextAware { + + private static ApplicationContext context = null; + + public static ApplicationContext getApplicationContext() { + return context; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + + + public static <T> T getBean(String beanName, Class<T> requiredType) { + if(context != null){ + return context.getBean(beanName, requiredType); + } + return null; + } + + public static <T> T getBean(Class<T> clazz){ + if(context != null){ + return context.getBean(clazz); + } + return null; + } + + public static Object getBean(String bean){ + if(context != null){ + return context.getBean(bean); + } + return null; + } + +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SwaggerGenerationConfiguration.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SwaggerGenerationConfiguration.java new file mode 100644 index 0000000..e479328 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/SwaggerGenerationConfiguration.java @@ -0,0 +1,64 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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.onap.aai.schemagen; + + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.schemagen.genxsd.HTMLfromOXM; +import org.onap.aai.schemagen.genxsd.NodesYAMLfromOXM; +import org.onap.aai.schemagen.genxsd.YAMLfromOXM; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +public class SwaggerGenerationConfiguration { + + @Value("${schema.uri.base.path}") + private String basePath; + + @Value("${schema.xsd.maxoccurs:5000}") + private String maxOccurs; + + @Bean + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public NodesYAMLfromOXM nodesYamlFromOXM(SchemaVersions schemaVersions, NodeIngestor nodeIngestor, EdgeIngestor edgeIngestor) { + return new NodesYAMLfromOXM(basePath, schemaVersions, nodeIngestor, edgeIngestor); + } + + @Bean + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public HTMLfromOXM htmlFromOXM(SchemaVersions schemaVersions, NodeIngestor nodeIngestor, EdgeIngestor edgeIngestor) { + return new HTMLfromOXM(maxOccurs, schemaVersions, nodeIngestor, edgeIngestor); + } + + @Bean + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public YAMLfromOXM yamlFromOXM(SchemaVersions schemaVersions, NodeIngestor nodeIngestor, EdgeIngestor edgeIngestor) { + return new YAMLfromOXM(basePath, schemaVersions, nodeIngestor, edgeIngestor); + } + +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/ConfigTranslatorForDocs.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/ConfigTranslatorForDocs.java new file mode 100644 index 0000000..217aa4e --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/ConfigTranslatorForDocs.java @@ -0,0 +1,81 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.*; + +public class ConfigTranslatorForDocs extends ConfigTranslator { + + public ConfigTranslatorForDocs(SchemaLocationsBean bean, SchemaVersions schemaVersions) { + super(bean, schemaVersions); + } + + @Override + public Map<SchemaVersion, List<String>> getNodeFiles() { + List<SchemaVersion> versionsToGen = new ArrayList<>(); + versionsToGen = schemaVersions.getVersions(); + Collections.sort(versionsToGen); + Collections.reverse(versionsToGen); + Map<SchemaVersion, List<String>> mp = new TreeMap<>(); + for (SchemaVersion v : versionsToGen) { + File dir = new File(bean.getNodeDirectory() + File.separator + v.toString().toLowerCase()); + File [] fileSet = dir.listFiles(); + List<String> files = new ArrayList<>(); + for(File f: fileSet ) { + files.add(f.getAbsolutePath()); + } + + mp.put(v, files); + } + return mp; + } + + @Override + public Map<SchemaVersion, List<String>> getEdgeFiles() { + List<SchemaVersion> versionsToGen = new ArrayList<>(); + versionsToGen = schemaVersions.getVersions(); + Collections.sort(versionsToGen); + Collections.reverse(versionsToGen); + Map<SchemaVersion, List<String>> mp = new TreeMap<>(); + for (SchemaVersion v : versionsToGen) { + File dir = new File(bean.getEdgeDirectory()); + File [] fileSet = dir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.contains("_"+v.toString().toLowerCase()); + } + }); + List<String> files = new ArrayList<>(); + for(File f: fileSet ) { + files.add(f.getAbsolutePath()); + } + mp.put(v, files); + } + return mp; + } +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/DeleteFootnoteSet.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/DeleteFootnoteSet.java new file mode 100644 index 0000000..7efaed9 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/DeleteFootnoteSet.java @@ -0,0 +1,57 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import java.util.Set; +import java.util.TreeSet; + +public class DeleteFootnoteSet { + protected Set<String> footnotes = new TreeSet<>(); + protected String targetNode = "<NodeType>"; + public DeleteFootnoteSet(String targetNode) { + super(); + this.targetNode = targetNode == null ? "" : targetNode; + } + + public void add(String s ) { + String fullnote=null; + if("(1)".equals(s)) { + fullnote = s+" IF this "+targetNode.toUpperCase()+" node is deleted, this FROM node is DELETED also"; + } else if("(2)".equals(s)) { + fullnote = s+" IF this "+targetNode.toUpperCase()+" node is deleted, this TO node is DELETED also"; + } else if("(3)".equals(s)) { + fullnote = s+" IF this FROM node is deleted, this "+targetNode.toUpperCase()+" is DELETED also"; + } else if("(4)".equals(s)) { + fullnote = s+" IF this TO node is deleted, this "+targetNode.toUpperCase()+" is DELETED also"; + } else if(s.contains(targetNode.toUpperCase())) { + fullnote = s; + } else { + return; + } + footnotes.add(fullnote); + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + if(footnotes.size() > 0) sb.append("\n -"); + sb.append(String.join("\n -", footnotes)+"\n"); + return sb.toString(); + } +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/DeleteOperation.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/DeleteOperation.java new file mode 100644 index 0000000..e1d993f --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/DeleteOperation.java @@ -0,0 +1,106 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.schemagen.GenerateXsd; + +import java.util.HashMap; +import java.util.StringTokenizer; + +public class DeleteOperation { + private String useOpId; + private String xmlRootElementName; + private String tag; + private String path; + private String pathParams; + + public static HashMap<String, String> deletePaths = new HashMap<String, String>(); + public DeleteOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) { + super(); + this.useOpId = useOpId; + this.xmlRootElementName = xmlRootElementName; + this.tag = tag; + this.path = path; + this.pathParams = pathParams; + } + @Override + public String toString() { + StringTokenizer st; + st = new StringTokenizer(path, "/"); + //a valid tag is necessary + if ( StringUtils.isEmpty(tag) ) { + return ""; + } + if ( path.contains("/relationship/") ) { // filter paths with relationship-list + return ""; + } + if ( path.endsWith("/relationship-list")) { + return ""; + } + if ( path.startsWith("/search")) { + return ""; + } + //All Delete operation paths end with "relationship" + //or there is a parameter at the end of the path + //and there is a parameter in the path + + if ( !path.endsWith("/relationship") && !path.endsWith("}") ) { + return ""; + } + StringBuffer pathSb = new StringBuffer(); + pathSb.append(" delete:\n"); + pathSb.append(" tags:\n"); + pathSb.append(" - " + tag + "\n"); + pathSb.append(" summary: delete an existing " + xmlRootElementName + "\n"); + + pathSb.append(" description: delete an existing " + xmlRootElementName + "\n"); + + pathSb.append(" operationId: delete" + useOpId + "\n"); + pathSb.append(" consumes:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + pathSb.append(" produces:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + pathSb.append(" responses:\n"); + pathSb.append(" \"default\":\n"); + pathSb.append(" " + GenerateXsd.getResponsesUrl()); + pathSb.append(" parameters:\n"); + + pathSb.append(pathParams); // for nesting + if ( !path.endsWith("/relationship") ) { + pathSb.append(" - name: resource-version\n"); + + pathSb.append(" in: query\n"); + pathSb.append(" description: resource-version for concurrency\n"); + pathSb.append(" required: true\n"); + pathSb.append(" type: string\n"); + } + this.objectPathMapEntry(); + return pathSb.toString(); + } + public String objectPathMapEntry() { + if (! path.endsWith("/relationship") ) { + deletePaths.put(path, xmlRootElementName); + } + return (xmlRootElementName+":"+path); + } + } diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/EdgeDescription.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/EdgeDescription.java new file mode 100644 index 0000000..f472896 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/EdgeDescription.java @@ -0,0 +1,196 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.enums.AAIDirection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EdgeDescription { + + private static final Logger logger = LoggerFactory.getLogger("EdgeDescription.class"); + EdgeRule ed; + public static enum LineageType { + PARENT, CHILD, UNRELATED; + } + private String ruleKey; +// private String to; +// private String from; + private LineageType lineageType = LineageType.UNRELATED; +// private String direction; +// private String multiplicity; +// private String preventDelete; +// private String deleteOtherV; +// private String label; +// private String description; + + public EdgeDescription(EdgeRule ed) { + super(); + if ( ed.getDirection().toString().equals(ed.getContains()) && + AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + this.lineageType= LineageType.PARENT; + } else if ( AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getContains())) && + ed.getDirection().toString().equals(ed.getContains())) { + this.lineageType= LineageType.CHILD; + } else if ( AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getContains())) && + AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getDirection()))) { + this.lineageType= LineageType.PARENT; + } else if ( AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getContains())) && + AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + this.lineageType= LineageType.PARENT; + } else { + this.lineageType= LineageType.UNRELATED; + } + this.ruleKey = ed.getFrom()+"|"+ed.getTo(); + this.ed=ed; + } + /** + * @return the deleteOtherV + */ + public String getDeleteOtherV() { + return ed.getDeleteOtherV(); + } + /** + * @return the preventDelete + */ + public String getPreventDelete() { + return ed.getPreventDelete(); + } + public String getAlsoDeleteFootnote(String targetNode) { + String returnVal = ""; + if(ed.getDeleteOtherV().equals("IN") && ed.getTo().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); + logger.debug("IF this "+targetNode+" node is deleted, this FROM node is DELETED also"); + returnVal = "(1)"; + } + if(ed.getDeleteOtherV().equals("OUT") && ed.getFrom().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); + logger.debug("IF this "+targetNode+" is deleted, this TO node is DELETED also"); + returnVal = "(2)"; + } + if(ed.getDeleteOtherV().equals("OUT") && ed.getTo().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); + logger.debug("IF this FROM node is deleted, this "+targetNode+" is DELETED also"); + returnVal = "(3)"; + } + if(ed.getDeleteOtherV().equals("IN") && ed.getFrom().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); + logger.debug("IF this TO node is deleted, this "+targetNode+" node is DELETED also"); + returnVal = "(4)"; + } + return returnVal; + } + /** + * @return the to + */ + public String getTo() { + return ed.getTo(); + } + /** + * @return the from + */ + public String getFrom() { + return ed.getFrom(); + } + public String getRuleKey() { + return ruleKey; + } + public String getMultiplicity() { + return ed.getMultiplicityRule().toString(); + } + public AAIDirection getDirection() { + return AAIDirection.getValue(ed.getDirection()); + } + public String getDescription() { + return ed.getDescription(); + } + public String getRelationshipDescription(String fromTo, String otherNodeName) { + + String result = ""; + + if ("FROM".equals(fromTo)) { + if (AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + if (LineageType.PARENT == lineageType) { + result = " (PARENT of "+otherNodeName; + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } + } + else { + if (LineageType.CHILD == lineageType) { + result = " (CHILD of "+otherNodeName; + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } + else if (LineageType.PARENT == lineageType) { + result = " (PARENT of "+otherNodeName; + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } + } + if (result.length() == 0) result = String.join(" ", "(", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } else { + //if ("TO".equals(fromTo) + if (AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + if (LineageType.PARENT == lineageType) { + result = " (PARENT of "+otherNodeName; + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } + } else { + if (LineageType.PARENT == lineageType) { + result = " (PARENT of "+otherNodeName; + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } + } + if (result.length() == 0) result = String.join(" ", "(", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); + } + + if (result.length() > 0) result = result + ")"; + + if (ed.getDescription() != null && ed.getDescription().length() > 0) result = result + "\n "+ ed.getDescription(); // 6 spaces is important for yaml + + return result; + } + + /** + * @return the hasDelTarget + */ + + public boolean hasDelTarget() { + return StringUtils.isNotEmpty(ed.getDeleteOtherV()) && (! "NONE".equalsIgnoreCase(ed.getDeleteOtherV())); + } + + /** + * @return the type + */ + public LineageType getType() { + + return lineageType; + } + /** + * @return the label + */ + public String getLabel() { + return ed.getLabel(); + } + public String getShortLabel() { + String[] pieces = this.getLabel().split("[.]"); + return pieces[pieces.length-1]; + } +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/GetOperation.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/GetOperation.java new file mode 100644 index 0000000..f2f0d63 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/GetOperation.java @@ -0,0 +1,121 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.schemagen.GenerateXsd; + +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Vector; + +public class GetOperation { + static Map<String, Vector<String>> containers = new HashMap<String, Vector<String>>(); + public static void addContainerProps(String container, Vector<String> containerProps) { + containers.put(container, containerProps);; + } + private String useOpId; + private String xmlRootElementName; + private String tag; + private String path; + private String pathParams; + private String queryParams; + + public GetOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) { + super(); + this.useOpId = useOpId; + this.xmlRootElementName = xmlRootElementName; + this.tag = tag; + this.path = path; + this.pathParams = pathParams; +// StringBuilder p = new StringBuilder(); + + if(containers.get(xmlRootElementName) == null) { + this.queryParams = ""; + } else { + this.queryParams= String.join("", containers.get(xmlRootElementName)); +// for(String param : containers.get(xmlRootElementName)) { +// p.append(param); +// } +// this.queryParams = p.toString(); + } + } + @Override + public String toString() { + StringTokenizer st; + st = new StringTokenizer(path, "/"); + //Path has to be longer than one element + /* + if ( st.countTokens() <= 1) { + return ""; + } + */ + //a valid tag is necessary + if ( StringUtils.isEmpty(tag) ) { + return ""; + } + if ( path.endsWith("/relationship") ) { + return ""; + } + if ( path.contains("/relationship/") ) { // filter paths with relationship-list + return ""; + } + if ( path.endsWith("/relationship-list")) { + return ""; + } + if ( path.startsWith("/search")) { + return ""; + } + StringBuffer pathSb = new StringBuffer(); + pathSb.append(" " + path + ":\n" ); + pathSb.append(" get:\n"); + pathSb.append(" tags:\n"); + pathSb.append(" - " + tag + "\n"); + pathSb.append(" summary: returns " + xmlRootElementName + "\n"); + + pathSb.append(" description: returns " + xmlRootElementName + "\n"); + pathSb.append(" operationId: get" + useOpId + "\n"); + pathSb.append(" produces:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + + pathSb.append(" responses:\n"); + pathSb.append(" \"200\":\n"); + pathSb.append(" description: successful operation\n"); + pathSb.append(" schema:\n"); + pathSb.append(" $ref: \"#/getDefinitions/" + xmlRootElementName + "\"\n"); + pathSb.append(" \"default\":\n"); + pathSb.append(" " + GenerateXsd.getResponsesUrl()); + if ( StringUtils.isNotEmpty(pathParams) || StringUtils.isNotEmpty(queryParams)) { + pathSb.append(" parameters:\n"); + } + if ( StringUtils.isNotEmpty(pathParams)) { + pathSb.append(pathParams); + } +// if ( StringUtils.isNotEmpty(pathParams) && StringUtils.isNotEmpty(queryParams)) { +// pathSb.append("\n"); +// } + if ( StringUtils.isNotEmpty(queryParams)) { + pathSb.append(queryParams); + } + return pathSb.toString(); + } + } 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 new file mode 100644 index 0000000..2af0662 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/HTMLfromOXM.java @@ -0,0 +1,283 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Attr; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class HTMLfromOXM extends OxmFileProcessor { + + private static final Logger logger = LoggerFactory.getLogger("HTMLfromOXM.class"); + + private String maxOccurs; + + public HTMLfromOXM(String maxOccurs, SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei ){ + super(schemaVersions, ni,ei); + this.maxOccurs = maxOccurs; + } + public void setOxmVersion(File oxmFile, SchemaVersion v) { + super.setOxmVersion(oxmFile, v); + this.v = v; + } + public void setXmlVersion(String xml, SchemaVersion v) { + super.setXmlVersion(xml, v); + this.v = v; + } + public void setVersion(SchemaVersion v) { + super.setVersion(v); + this.v = v; + } + + + @Override + public String getDocumentHeader() { + StringBuffer sb = new StringBuffer(); + logger.trace("processing starts"); + sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + LINE_SEPARATOR); + String namespace = "org.onap"; + if (v.compareTo(getSchemaVersions().getNamespaceChangeVersion()) < 0 ) { + namespace = "org.openecomp"; + } + if ( versionUsesAnnotations(v.toString()) ) { + sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" + + v.toString() + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + v.toString() + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + + LINE_SEPARATOR + + "xmlns:jaxb=\"http://java.sun.com/xml/ns/jaxb\"" + LINE_SEPARATOR + + " jaxb:version=\"2.1\"" + LINE_SEPARATOR + + " xmlns:annox=\"http://annox.dev.java.net\"" + LINE_SEPARATOR + + " jaxb:extensionBindingPrefixes=\"annox\">" + DOUBLE_LINE_SEPARATOR); + } else { + sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" + + v.toString() + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + v.toString() + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" + DOUBLE_LINE_SEPARATOR); + } + return sb.toString(); + } + + @Override + public String process() throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, EdgeRuleNotFoundException { + StringBuilder sb = new StringBuilder(); + + try { + init(); + } catch(Exception e) { + logger.error( "Error initializing " + this.getClass()); + throw e; + } + sb.append(getDocumentHeader()); + StringBuilder sbInventory = new StringBuilder(); + Element elem; + String javaTypeName; + combinedJavaTypes = new HashMap(); + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + elem = (Element)javaTypeNodes.item(i); + javaTypeName = elem.getAttribute("name"); + if ( !"Inventory".equals(javaTypeName ) ) { + if ( generatedJavaType.containsKey(javaTypeName) ) { + continue; + } + // will combine all matching java-types + elem = getJavaTypeElement(javaTypeName,false ); + } + XSDElement javaTypeElement = new XSDElement(elem, maxOccurs); + //javaTypeName = javaTypeElement.name(); + if ( javaTypeName == null ) { + String msg = "Invalid OXM file: <java-type> has no name attribute in " + oxmFile; + logger.error(msg); + throw new SAXException(msg); + } + if ("Nodes".equals(javaTypeName)) { + logger.debug("skipping Nodes entry (temporary feature)"); + continue; + } + logger.debug(getXmlRootElementName(javaTypeName)+" vs "+ javaTypeName+":"+generatedJavaType.containsKey(getXmlRootElementName(javaTypeName))); + + if ( !"Inventory".equals(javaTypeName)) { + generatedJavaType.put(javaTypeName, null); + } + sb.append(processJavaTypeElement( javaTypeName, javaTypeElement, sbInventory )); + } + sb.append(sbInventory); + sb.append(" </xs:sequence>" + LINE_SEPARATOR); + sb.append(" </xs:complexType>" + LINE_SEPARATOR); + sb.append(" </xs:element>" + LINE_SEPARATOR); + sb.append("</xs:schema>" + LINE_SEPARATOR); + return sb.toString(); + } + + protected boolean isValidName( String name ) { + if ( name == null || name.length() == 0 ) { + return false; + } + String pattern = "^[a-z0-9-]*$"; + return name.matches(pattern); + } + + protected boolean skipCheck( String javaAttribute ) { + if ( javaAttribute.equals("model") + || javaAttribute.equals("eventHeader") ) { + return true; + } + return false; + } + + public String processJavaTypeElement( String javaTypeName, Element javaType_Element, StringBuilder sbInventory) { + String xmlRootElementName = getXMLRootElementName(javaType_Element); + + NodeList parentNodes = javaType_Element.getElementsByTagName("java-attributes"); + StringBuffer sb = new StringBuffer(); + if ( parentNodes.getLength() == 0 ) { + logger.trace( "no java-attributes for java-type " + javaTypeName); + return ""; + } + + Element parentElement = (Element)parentNodes.item(0); + NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + // support for multiple inventory elements across oxm files + boolean processingInventory = false; + boolean hasPreviousInventory = false; + if ( "inventory".equals(xmlRootElementName) && sbInventory != null ) { + processingInventory = true; + if ( sbInventory.toString().contains("xs:complexType") ) { + hasPreviousInventory = true; + } + } + + StringBuffer sb1 = new StringBuffer(); + if ( xmlElementNodes.getLength() > 0 ) { + + if ( !processingInventory || !hasPreviousInventory ) { + sb1.append(" <xs:element name=\"" + xmlRootElementName + "\">" + LINE_SEPARATOR); + sb1.append(" <xs:complexType>" + LINE_SEPARATOR); + + XSDElement javaTypeElement = new XSDElement(javaType_Element, maxOccurs); + logger.debug("XSDElement name: "+javaTypeElement.name()); + if(versionUsesAnnotations(v.toString())) { + sb1.append(javaTypeElement.getHTMLAnnotation("class", " ")); + } + sb1.append(" <xs:sequence>" + LINE_SEPARATOR); + } + Element javatypeElement; + for ( int i = 0; i < xmlElementNodes.getLength(); ++i ) { + + XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(i), maxOccurs); + +// String elementName = xmlElementElement.getAttribute("name"); + String elementType = xmlElementElement.getAttribute("type"); + //No simple types; only AAI custom types + String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; + if ( elementType.contains("." + v.toString() + ".") && !generatedJavaType.containsKey(addType) ) { + generatedJavaType.put(addType, elementType); + javatypeElement = getJavaTypeElement(addType, processingInventory); + sb.append(processJavaTypeElement( addType, javatypeElement, null )); + } + if ("Nodes".equals(addType)) { + logger.trace("Skipping nodes, temporary testing"); + continue; + } + //assembles the basic <element> + sb1.append(xmlElementElement.getHTMLElement(v, versionUsesAnnotations(v.toString()), this)); + } + if ( !processingInventory ) { + sb1.append(" </xs:sequence>" + LINE_SEPARATOR); + sb1.append(" </xs:complexType>" + LINE_SEPARATOR); + sb1.append(" </xs:element>" + LINE_SEPARATOR); + } + } + + if ( xmlElementNodes.getLength() < 1 ) { + sb.append(" <xs:element name=\"" + xmlRootElementName + "\">" + LINE_SEPARATOR); + sb.append(" <xs:complexType>" + LINE_SEPARATOR); + sb.append(" <xs:sequence/>" + LINE_SEPARATOR); + sb.append(" </xs:complexType>" + LINE_SEPARATOR); + sb.append(" </xs:element>" + LINE_SEPARATOR); + generatedJavaType.put(javaTypeName, null); + return sb.toString(); + } + if ( processingInventory && sbInventory != null ) { + sbInventory.append(sb1); + } else { + sb.append( sb1 ); + } + return sb.toString(); + } + + private Element getJavaTypeElement( String javaTypeName, boolean processingInventory ) + { + String attrName, attrValue; + Attr attr; + Element javaTypeElement; + + List<Element> combineElementList = new ArrayList<Element>(); + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + javaTypeElement = (Element) javaTypeNodes.item(i); + NamedNodeMap attributes = javaTypeElement.getAttributes(); + for ( int j = 0; j < attributes.getLength(); ++j ) { + attr = (Attr) attributes.item(j); + attrName = attr.getNodeName(); + attrValue = attr.getNodeValue(); + if ( attrName.equals("name") && attrValue.equals(javaTypeName)) { + if ( processingInventory ) { + return javaTypeElement; + } else { + combineElementList.add(javaTypeElement); + } + } + } + } + if ( combineElementList.size() == 0 ) { + logger.error( "oxm file format error, missing java-type " + javaTypeName); + return (Element) null; + } else if ( combineElementList.size() > 1 ) { + // need to combine java-attributes + return combineElements( javaTypeName, combineElementList); + } + return combineElementList.get(0); + + } + + private boolean versionUsesAnnotations( String version) { + int ver = new Integer(version.substring(1)).intValue(); + if ( ver >= HTMLfromOXM.annotationsStartVersion ) { + return true; + } + if ( ver <= HTMLfromOXM.annotationsMinVersion ) { + return true; + } + return false; + } +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodeGetOperation.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodeGetOperation.java new file mode 100644 index 0000000..b0961c3 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodeGetOperation.java @@ -0,0 +1,164 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.schemagen.GenerateXsd; + +import java.util.*; + +public class NodeGetOperation { + static Map<String, Vector<String>> containers = new HashMap<String, Vector<String>>(); + static ArrayList<String> checklist = createChecklist(); + private static ArrayList<String> createChecklist() + { + ArrayList<String> list = new ArrayList<String>(); + return list; + } + public static void addContainerProps(String container, Vector<String> containerProps) { + containers.put(container, containerProps); + } + public static void resetContainers() { + containers = new HashMap<String, Vector<String>>(); + checklist = createChecklist(); + } + private String useOpId; + private String xmlRootElementName; + private String tag; + private String path; + private String CRUDpath; + private String pathParams; + private String queryParams; + + public NodeGetOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) { + super(); + this.useOpId = useOpId; + this.xmlRootElementName = xmlRootElementName; + this.tag = tag; + this.CRUDpath = path; + this.path = nodePath(); + this.pathParams = pathParams; + StringBuilder p = new StringBuilder(); + + if(containers.get(xmlRootElementName) == null) { + this.queryParams = ""; + } else { + this.queryParams= String.join("", containers.get(xmlRootElementName)); + for(String param : containers.get(xmlRootElementName)) { + p.append(param); + } + this.queryParams = p.toString(); + } + } + String nodePath() { + String path = null; + int loc = CRUDpath.indexOf(xmlRootElementName); + if(loc > 0) { + path = "/nodes/"+CRUDpath.substring(loc); + } + return path; + } + @Override + public String toString() { + StringTokenizer st; + st = new StringTokenizer(CRUDpath, "/"); + //Path has to be longer than one element + /* + if ( st.countTokens() <= 1) { + return ""; + } + */ + //a valid tag is necessary + if ( StringUtils.isEmpty(tag) ) { + return ""; + } + if ( CRUDpath.endsWith("/relationship") ) { + return ""; + } + if ( CRUDpath.contains("/relationship/") ) { // filter paths with relationship-list + return ""; + } + if ( CRUDpath.endsWith("/relationship-list")) { + return ""; + } + if ( CRUDpath.startsWith("/search")) { + return ""; + } + if ( CRUDpath.startsWith("/actions")) { + return ""; + } + if ( CRUDpath.startsWith("/nodes")) { + return ""; + } + if (checklist.contains(xmlRootElementName)) { + return ""; + } + StringBuffer pathSb = new StringBuffer(); + //Drop out the operations with multiple path parameters + if(CRUDpath.lastIndexOf('{') > CRUDpath.indexOf('{') && StringUtils.isNotEmpty(pathParams)) { + return ""; + } + if(path.lastIndexOf('{') > path.indexOf('{') ) { + return ""; + } + //trim leading path elements before the current node type +// int loc = path.indexOf(xmlRootElementName); +// if(loc > 0) { +// path = "/nodes/"+path.substring(loc); +// } + //append generic parameter syntax to all plural queries + if(path.indexOf('{') == -1) { + path += "?parameter=value[¶meter2=value2]"; + } + pathSb.append(" " + path + ":\n" ); + pathSb.append(" get:\n"); + pathSb.append(" tags:\n"); + pathSb.append(" - Operations" + "\n"); + pathSb.append(" summary: returns " + xmlRootElementName + "\n"); + + pathSb.append(" description: returns " + xmlRootElementName + "\n"); + pathSb.append(" operationId: get" + useOpId + "\n"); + pathSb.append(" produces:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + + pathSb.append(" responses:\n"); + pathSb.append(" \"200\":\n"); + pathSb.append(" description: successful operation\n"); + pathSb.append(" schema:\n"); + pathSb.append(" $ref: \"#/definitions/" + xmlRootElementName + "\"\n"); + pathSb.append(" \"default\":\n"); + pathSb.append(" " + GenerateXsd.getResponsesUrl()); + if ( StringUtils.isNotEmpty(pathParams) || StringUtils.isNotEmpty(queryParams)) { + pathSb.append("\n parameters:\n"); + } + if ( StringUtils.isNotEmpty(pathParams)) { + pathSb.append(pathParams); + } + if ( StringUtils.isNotEmpty(pathParams) && StringUtils.isNotEmpty(queryParams)) { + pathSb.append("\n"); + } + if ( StringUtils.isNotEmpty(queryParams)) { + pathSb.append(queryParams); + } + checklist.add(xmlRootElementName); + return pathSb.toString(); + } + } 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 new file mode 100644 index 0000000..b9efa2f --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java @@ -0,0 +1,527 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import com.google.common.collect.Multimap; +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class NodesYAMLfromOXM extends OxmFileProcessor { + private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class"); + private static final String root = "../aai-schema/src/main/resources"; + private static final String autoGenRoot = "aai-schema/src/main/resources"; + private static final String generateTypeYAML = "yaml"; + private static final String normalStartDir = "aai-schema-gen"; + private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml"; + private StringBuilder inventoryDefSb = null; + private Map<String,String> operationDefinitions = new HashMap<>(); + + private String basePath; + + public NodesYAMLfromOXM(String basePath, SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){ + super(schemaVersions, ni,ei); + this.basePath = basePath; + } + public void setOxmVersion(File oxmFile, SchemaVersion v) { + super.setOxmVersion(oxmFile, v); + } + public void setXmlVersion(String xml, SchemaVersion v){ + super.setXmlVersion(xml, v); + } + + public void setVersion(SchemaVersion v) { + super.setVersion(v); + } + + @Override + public String getDocumentHeader() { + StringBuffer sb = new StringBuffer(); + sb.append("swagger: \"2.0\"\ninfo:\n "); + sb.append("description: |"); + if ( versionSupportsSwaggerDiff(v.toString())) { + sb.append("\n\n [Differences versus the previous schema version]("+"apidocs/aai_swagger_" + v.toString() + ".diff)"); + } + sb.append("\n\n Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.\n\n Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except in compliance with the License.\n\n You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)\n\n 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.\n\n This document is best viewed with Firefox or Chrome. Nodes can be found by appending /#/definitions/node-type-to-find to the path to this document. Edge definitions can be found with the node definitions.\n version: \"" + v.toString() +"\"\n"); + sb.append(" title: Active and Available Inventory REST API\n"); + sb.append(" license:\n name: Apache 2.0\n url: http://www.apache.org/licenses/LICENSE-2.0.html\n"); + sb.append(" contact:\n name:\n url:\n email:\n"); + sb.append("host:\nbasePath: " + basePath + "/" + v.toString() + "\n"); + sb.append("schemes:\n - https\npaths:\n"); + return sb.toString(); + } + + protected void init() throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, EdgeRuleNotFoundException { + super.init(); + } + + @Override + public String process() throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, EdgeRuleNotFoundException { + StringBuffer sb = new StringBuffer(); + StringBuffer pathSb = new StringBuffer(); + NodeGetOperation.resetContainers(); + try { + init(); + } catch(Exception e) { + logger.error( "Error initializing " + this.getClass()); + throw e; + } + pathSb.append(getDocumentHeader()); + StringBuffer definitionsSb = new StringBuffer(); + Element elem; + String javaTypeName; + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + elem = (Element)javaTypeNodes.item(i); + javaTypeName = elem.getAttribute("name"); + if ( !"Inventory".equals(javaTypeName ) ) { + if ( generatedJavaType.containsKey(javaTypeName) ) { + continue; + } + // will combine all matching java-types + elem = getJavaTypeElementSwagger(javaTypeName ); + } + + XSDElement javaTypeElement = new XSDElement(elem); + + logger.debug("External: "+javaTypeElement.getAttribute("name")+"/"+getXmlRootElementName(javaTypeName)); + if ( javaTypeName == null ) { + String msg = "Invalid OXM file: <java-type> has no name attribute in " + oxmFile; + logger.error(msg); + throw new SAXException(msg); + } + namespaceFilter.add(getXmlRootElementName(javaTypeName)); + processJavaTypeElementSwagger( javaTypeName, javaTypeElement, pathSb, + definitionsSb, null, null, null, null, null, null); + } + sb.append(pathSb); +// sb.append(getDocumentHeader()); +// sb.append(totalPathSbAccumulator); + sb.append(appendOperations()); + sb.append(appendDefinitions()); + PutRelationPathSet prp = new PutRelationPathSet(v); + prp.generateRelations(ei); + return sb.toString(); + } + + public String appendDefinitions() { + return appendDefinitions(null); + } + + public String appendDefinitions(Set<String> namespaceFilter) { + if ( inventoryDefSb != null ) { + javaTypeDefinitions.put("inventory", inventoryDefSb.toString()); + } + StringBuffer sb = new StringBuffer("definitions:\n"); + Map<String, String> sortedJavaTypeDefinitions = new TreeMap<>(javaTypeDefinitions); + + for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } + sb.append(entry.getValue()); + } + return sb.toString(); + } + + private String processJavaTypeElementSwagger( String javaTypeName, Element javaTypeElement, + StringBuffer pathSb, StringBuffer definitionsSb, String path, String tag, String opId, + String getItemName, StringBuffer pathParams, String validEdges) { + + String xmlRootElementName = getXMLRootElementName(javaTypeElement); + StringBuilder definitionsLocalSb = new StringBuilder(256); + + String useTag = null; + String useOpId = null; + logger.debug("tag="+tag); + + if(tag != null && (! validTag(tag))) { + logger.debug("tag="+tag+"; javaTypeName="+javaTypeName); + return null; + } + if ( !javaTypeName.equals("Inventory") ) { + if ( javaTypeName.equals("AaiInternal")) + return null; + if ( opId == null ) + useOpId = javaTypeName; + else + useOpId = opId + javaTypeName; + if ( tag == null ) + useTag = javaTypeName; + } + + path = xmlRootElementName.equals("inventory") ? "" : (path == null) ? "/" + xmlRootElementName : path + "/" + xmlRootElementName; + XSDJavaType javaType = new XSDJavaType(javaTypeElement); + if ( getItemName != null) { + if ( getItemName.equals("array") ) + return javaType.getArrayType(); + else + return javaType.getItemName(); + } + + NodeList parentNodes = javaTypeElement.getElementsByTagName("java-attributes"); + if ( parentNodes.getLength() == 0 ) { + logger.debug( "no java-attributes for java-type " + javaTypeName); + return ""; + } + + String pathDescriptionProperty = javaType.getPathDescriptionProperty(); + String container = javaType.getContainerProperty(); + Vector<String> indexedProps = javaType.getIndexedProps(); + Vector<String> containerProps = new Vector<String>(); + if(container != null) { + logger.debug("javaTypeName " + javaTypeName + " container:" + container +" indexedProps:"+indexedProps); + } + + Element parentElement = (Element)parentNodes.item(0); + NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + + StringBuffer sbParameters = new StringBuffer(); + StringBuffer sbRequired = new StringBuffer(); + int requiredCnt = 0; + int propertyCnt = 0; + StringBuffer sbProperties = new StringBuffer(); + + if ( appliedPaths.containsKey(path)) + return null; + + StringTokenizer st = new StringTokenizer(path, "/"); + logger.debug("path: " + path + " st? " + st.toString()); + if ( st.countTokens() > 1 && getItemName == null ) { + logger.debug("appliedPaths: " + appliedPaths + " containsKey? " + appliedPaths.containsKey(path)); + appliedPaths.put(path, xmlRootElementName); + } + Vector<String> addTypeV = null; + for ( int i = 0; i < xmlElementNodes.getLength(); ++i ) { + XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(i)); + if ( !xmlElementElement.getParentNode().isSameNode(parentElement)) + continue; + String elementDescription=xmlElementElement.getPathDescriptionProperty(); + if(getItemName == null) { + addTypeV = xmlElementElement.getAddTypes(v.toString()); + } + if ( "true".equals(xmlElementElement.getAttribute("xml-key"))) { + path += "/{" + xmlElementElement.getAttribute("name") + "}"; + } + logger.debug("path: " + path); + logger.debug( "xmlElementElement.getAttribute(required):"+xmlElementElement.getAttribute("required") ); + + if ( ("true").equals(xmlElementElement.getAttribute("required"))) { + if ( requiredCnt == 0 ) + sbRequired.append(" required:\n"); + ++requiredCnt; + if ( addTypeV == null || addTypeV.isEmpty()) { + sbRequired.append(" - " + xmlElementElement.getAttribute("name") + "\n"); + } else { + for ( int k = 0; k < addTypeV.size(); ++k ) { + sbRequired.append(" - " + getXmlRootElementName(addTypeV.elementAt(k)) + ":\n"); + } + } + } + + if ( "true".equals(xmlElementElement.getAttribute("xml-key")) ) { + sbParameters.append(xmlElementElement.getPathParamYAML(elementDescription)); + } + if ( indexedProps != null + && indexedProps.contains(xmlElementElement.getAttribute("name") ) ) { + containerProps.add(xmlElementElement.getQueryParamYAML()); + NodeGetOperation.addContainerProps(container, containerProps); + } + if ( xmlElementElement.isStandardType()) { + sbProperties.append(xmlElementElement.getTypePropertyYAML()); + ++propertyCnt; + } + +// StringBuffer newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); //cp8128 don't append the pathParams to sbParameters so that child nodes don't contain the parameters from parent + StringBuffer newPathParams = new StringBuffer(sbParameters.toString()); + for ( int k = 0; addTypeV != null && k < addTypeV.size(); ++k ) { + String addType = addTypeV.elementAt(k); + namespaceFilter.add(getXmlRootElementName(addType)); + if ( opId == null || !opId.contains(addType)) { + processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, null, + newPathParams, validEdges); + } + // need item name of array + String itemName = processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, + "array", null, null); + + if ( itemName != null ) { + if ( addType.equals("AaiInternal") ) { + logger.debug( "addType AaiInternal, skip properties"); + + } else if ( getItemName == null) { + ++propertyCnt; + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: array\n items:\n"); + sbProperties.append(" $ref: \"#/definitions/" + (itemName == "" ? "aai-internal" : itemName) + "\"\n"); + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + } + } else { + if ( ("java.util.ArrayList").equals(xmlElementElement.getAttribute("container-type"))) { + // need properties for getXmlRootElementName(addType) + namespaceFilter.add(getXmlRootElementName(addType)); + if(getXmlRootElementName(addType).equals("service-capabilities")) + { + logger.info("arrays: "+ getXmlRootElementName(addType)); + } +// newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); //cp8128 - change this to not append pathParameters. Just use sbParameters + newPathParams = new StringBuffer(sbParameters.toString()); + processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, + null, newPathParams, validEdges); + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: array\n items: \n"); + sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + + } else { + //Make sure certain types added to the filter don't appear + if (nodeFilter.contains(getXmlRootElementName(addType))) { + ; + } else { + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: object\n"); + sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); + } + } + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + ++propertyCnt; + } + } + } + + if ( sbParameters.toString().length() > 0 ) { + if ( pathParams == null ) + pathParams = new StringBuffer(); + pathParams.append(sbParameters); + } + if (indexedProps.isEmpty() && containerProps.isEmpty()){ + NodeGetOperation get = new NodeGetOperation(useOpId, xmlRootElementName, tag, path, null); + String operation = get.toString(); + if(StringUtils.isNotEmpty(operation)) { + operationDefinitions.put(xmlRootElementName, operation); + } + } else { + NodeGetOperation get = new NodeGetOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString()); + String operation = get.toString(); + if(StringUtils.isNotEmpty(operation)) { + operationDefinitions.put(xmlRootElementName, operation); + } + } + logger.debug("opId vs useOpId:"+opId+" vs "+useOpId+" PathParams="+pathParams); + // add PUT + if ( generatedJavaType.containsKey(xmlRootElementName) ) { + logger.debug("xmlRootElementName(1)="+xmlRootElementName); + return null; + } + boolean processingInventoryDef = false; + if ( xmlRootElementName.equals("inventory")) { + // inventory properties for each oxm to be concatenated + processingInventoryDef = true; + if ( inventoryDefSb == null ) { + inventoryDefSb = new StringBuilder(); + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" properties:\n"); + } + + } else { + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + } + DeleteFootnoteSet footnotes = new DeleteFootnoteSet(xmlRootElementName); + StringBuffer sbEdge = new StringBuffer(); + LinkedHashSet<String> preventDelete = new LinkedHashSet<String>(); + String prevent=null; + String nodeCaption = new String(" ###### Related Nodes\n"); + try { + EdgeRuleQuery q = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).fromOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q); + SortedSet<String> ss=new TreeSet<>(results.keySet()); + sbEdge.append(nodeCaption); + nodeCaption=""; + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - TO ").append(i.getTo()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - TO "+i.getTo()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("TO", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("OUT")))).forEach((i) ->{ preventDelete.add(i.getTo().toUpperCase());} ); + } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + } + try { + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).toOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - FROM "+i.getFrom()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("FROM", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - FROM ").append(i.getFrom()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("IN")))).forEach((i) ->{ preventDelete.add(i.getFrom().toUpperCase());} ); + } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + } + if(preventDelete.size() > 0) { + prevent = xmlRootElementName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); + logger.debug(prevent); + } + + if(StringUtils.isNotEmpty(prevent)) { + footnotes.add(prevent); + } + if(footnotes.footnotes.size() > 0) { + sbEdge.append(footnotes.toString()); + } + validEdges = sbEdge.toString(); + + // Handle description property. Might have a description OR valid edges OR both OR neither. + // Only put a description: tag if there is at least one. + if (StringUtils.isNotEmpty(pathDescriptionProperty) || StringUtils.isNotEmpty(validEdges) ) { + definitionsSb.append(" description: |\n"); + definitionsLocalSb.append(" description: |\n"); + + if ( pathDescriptionProperty != null ) { + definitionsSb.append(" " + pathDescriptionProperty + "\n" ); + definitionsLocalSb.append(" " + pathDescriptionProperty + "\n" ); + } + definitionsSb.append(validEdges); + definitionsLocalSb.append(validEdges); + } + + if ( requiredCnt > 0 ) { + definitionsSb.append(sbRequired); + definitionsLocalSb.append(sbRequired); + } + + if ( propertyCnt > 0 ) { + definitionsSb.append(" properties:\n"); + definitionsSb.append(sbProperties); + if ( !processingInventoryDef) { + definitionsLocalSb.append(" properties:\n"); + } + definitionsLocalSb.append(sbProperties); + } + try { + namespaceFilter.add(xmlRootElementName); + if ( xmlRootElementName.equals("inventory") ) { + //will add to javaTypeDefinitions at end + inventoryDefSb.append(definitionsLocalSb.toString()); + } else { + javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + if ( xmlRootElementName.equals("inventory") ) { + logger.trace("skip xmlRootElementName(2)="+xmlRootElementName); + return null; + } + generatedJavaType.put(xmlRootElementName, null); + //Write operations by Namespace(tagName) +/* + if( validTag(javaTypeName) && javaTypeName == useTag && tag == null) { + writeYAMLfile("nodes_"+javaTypeName, getDocumentHeader()+pathSb.toString()+appendDefinitions(namespaceFilter)); + totalPathSbAccumulator.append(pathSb); + pathSb.delete(0, pathSb.length()); + namespaceFilter.clear(); + } +*/ + logger.debug("xmlRootElementName(2)="+xmlRootElementName); + return null; + } + + private void writeYAMLfile(String outfileName, String fileContent) { + outfileName = (StringUtils.isEmpty(outfileName)) ? "aai_swagger" : outfileName; + outfileName = (outfileName.lastIndexOf(File.separator) == -1) ? yaml_dir + File.separator +outfileName+"_" + v.toString() + "." + generateTypeYAML : outfileName; + File outfile = new File(outfileName); + File parentDir = outfile.getParentFile(); + if(parentDir != null && ! parentDir.exists()) + parentDir.mkdirs(); + try { + outfile.createNewFile(); + } catch (IOException e) { + logger.error( "Exception creating output file " + outfileName); + e.printStackTrace(); + } + Path path = Paths.get(outfileName); + Charset charset = Charset.forName("UTF-8"); + try(BufferedWriter bw = Files.newBufferedWriter(path, charset);) { + bw.write(fileContent); + if ( bw != null ) { + bw.close(); + } + } catch ( IOException e) { + logger.error( "Exception writing output file " + outfileName); + e.printStackTrace(); + } + } + + public boolean validTag(String tag) { + if(tag != null) { + switch ( tag ) { + case "Network": + case "Search": + case "Actions": + case "ServiceDesignAndCreation": + case "Business": + case "LicenseManagement": + case "CloudInfrastructure": + return true; + } + } + return false; + } + + public String appendOperations() { + //append definitions + StringBuffer sb = new StringBuffer(); + Map<String, String> sortedOperationDefinitions = new TreeMap<String, String>(operationDefinitions); + for (Map.Entry<String, String> entry : sortedOperationDefinitions.entrySet()) { + sb.append(entry.getValue()); + } + return sb.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 new file mode 100644 index 0000000..dfa702b --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java @@ -0,0 +1,481 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.w3c.dom.*; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.StringReader; +import java.util.*; + +public abstract class OxmFileProcessor { + + public static final String LINE_SEPARATOR = System.getProperty("line.separator"); + public static final String DOUBLE_LINE_SEPARATOR = System.getProperty("line.separator") + System.getProperty("line.separator"); + + EdgeIngestor ei; + NodeIngestor ni; + protected Set<String> namespaceFilter; + protected File oxmFile; + protected String xml; + protected SchemaVersion v; + protected Document doc = null; + protected String apiVersion = null; + protected SchemaVersions schemaVersions; + + + protected static int annotationsStartVersion = 9; // minimum version to support annotations in xsd + protected static int annotationsMinVersion = 6; // lower versions support annotations in xsd + protected static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation + protected static int swaggerDiffStartVersion = 1; // minimum version to support difference + protected static int swaggerMinBasepath = 6; // minimum version to support difference + + protected Map combinedJavaTypes; + + + protected String apiVersionFmt = null; + protected HashMap<String, String> generatedJavaType = new HashMap<String, String>(); + protected HashMap<String, String> appliedPaths = new HashMap<String, String>(); + protected NodeList javaTypeNodes = null; + + protected Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions(); + private Map<String, String> createJavaTypeDefinitions() + { + StringBuffer aaiInternal = new StringBuffer(); + StringBuffer nodes = 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()); + nodes.append(" nodes:\n"); + nodes.append(" properties:\n"); + nodes.append(" inventory-item-data:\n"); + nodes.append(" type: array\n"); + nodes.append(" items:\n"); + nodes.append(" $ref: \"#/definitions/inventory-item-data\"\n"); + javaTypeDefinitions.put("nodes", nodes.toString()); + return javaTypeDefinitions; + } + static List<String> nodeFilter = createNodeFilter(); + private static List<String> createNodeFilter() + { + List<String> list = Arrays.asList("search", "actions", "aai-internal", "nodes"); + return list; + } + + public OxmFileProcessor(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){ + this.schemaVersions = schemaVersions; + this.ni = ni; + this.ei = ei; + } + + + + public void setOxmVersion(File oxmFile, SchemaVersion v) { + this.oxmFile = oxmFile; + this.v = v; + } + + public void setXmlVersion(String xml, SchemaVersion v) { + this.xml = xml; + this.v = v; + } + + public void setVersion(SchemaVersion v) { + this.oxmFile = null; + this.v = v; + } + + public void setNodeIngestor(NodeIngestor ni) { + this.ni = ni; + } + + public void setEdgeIngestor(EdgeIngestor ei) { + this.ei = ei; + } + + public SchemaVersions getSchemaVersions() { + return schemaVersions; + } + + public void setSchemaVersions(SchemaVersions schemaVersions) { + this.schemaVersions = schemaVersions; + } + + protected void init() throws ParserConfigurationException, SAXException, IOException, EdgeRuleNotFoundException { + if(this.xml != null || this.oxmFile != null ) { + createDocument(); + } + if(this.doc == null) { + this.doc = ni.getSchema(v); + } + namespaceFilter = new HashSet<>(); + + NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings"); + Element bindingElement; + NodeList javaTypesNodes; + Element javaTypesElement; + + if ( bindingsNodes == null || bindingsNodes.getLength() == 0 ) { + throw new SAXException("OXM file error: missing <binding-nodes> in " + oxmFile); + } + + bindingElement = (Element) bindingsNodes.item(0); + javaTypesNodes = bindingElement.getElementsByTagName("java-types"); + if ( javaTypesNodes.getLength() < 1 ) { + throw new SAXException("OXM file error: missing <binding-nodes><java-types> in " + oxmFile); + } + javaTypesElement = (Element) javaTypesNodes.item(0); + + javaTypeNodes = javaTypesElement.getElementsByTagName("java-type"); + if ( javaTypeNodes.getLength() < 1 ) { + throw new SAXException("OXM file error: missing <binding-nodes><java-types><java-type> in " + oxmFile ); + } + } + + private void createDocument() throws ParserConfigurationException, SAXException, IOException { + DocumentBuilder dBuilder = null; + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + dBuilder = dbFactory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw e; + } + try { + if ( xml == null ) { + doc = dBuilder.parse(oxmFile); + } else { + InputSource isInput = new InputSource(new StringReader(xml)); + doc = dBuilder.parse(isInput); + } + } catch (SAXException e) { + throw e; + } catch (IOException e) { + throw e; + } + return; + } + public abstract String getDocumentHeader(); + public abstract String process() throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, EdgeRuleNotFoundException ; + + public String getXMLRootElementName(Element javaTypeElement) { + String xmlRootElementName=null; + NamedNodeMap attributes; + + NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element"); + Element valElement = (Element) valNodes.item(0); + attributes = valElement.getAttributes(); + for ( int i = 0; i < attributes.getLength(); ++i ) { + Attr attr = (Attr) attributes.item(i); + String attrName = attr.getNodeName(); + + String attrValue = attr.getNodeValue(); + if ( attrName.equals("name")) + xmlRootElementName = attrValue; + } + return xmlRootElementName; + } + + public String getXmlRootElementName( String javaTypeName ) + { + String attrName, attrValue; + Attr attr; + Element javaTypeElement; + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + javaTypeElement = (Element) javaTypeNodes.item(i); + NamedNodeMap attributes = javaTypeElement.getAttributes(); + for ( int j = 0; j < attributes.getLength(); ++j ) { + attr = (Attr) attributes.item(j); + attrName = attr.getNodeName(); + attrValue = attr.getNodeValue(); + if ( attrName.equals("name") && attrValue.equals(javaTypeName)) { + NodeList valNodes = javaTypeElement.getElementsByTagName("xml-root-element"); + Element valElement = (Element) valNodes.item(0); + attributes = valElement.getAttributes(); + for ( int k = 0; k < attributes.getLength(); ++k ) { + attr = (Attr) attributes.item(k); + attrName = attr.getNodeName(); + + attrValue = attr.getNodeValue(); + if ( attrName.equals("name")) + return (attrValue); + } + } + } + } + return null; + } + + public Map getCombinedJavaTypes() { + return combinedJavaTypes; + } + + public void setCombinedJavaTypes(Map combinedJavaTypes) { + this.combinedJavaTypes = combinedJavaTypes; + } + + public Element getJavaTypeElementSwagger( String javaTypeName ) + { + + String attrName, attrValue; + Attr attr; + Element javaTypeElement; + + List<Element> combineElementList = new ArrayList<Element>(); + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + javaTypeElement = (Element) javaTypeNodes.item(i); + NamedNodeMap attributes = javaTypeElement.getAttributes(); + for ( int j = 0; j < attributes.getLength(); ++j ) { + attr = (Attr) attributes.item(j); + attrName = attr.getNodeName(); + attrValue = attr.getNodeValue(); + if ( attrName.equals("name") && attrValue.equals(javaTypeName)) { + combineElementList.add(javaTypeElement); + } + } + } + if ( combineElementList.size() == 0 ) { + return (Element) null; + } else if ( combineElementList.size() > 1 ) { + return combineElements( javaTypeName, combineElementList); + } + return combineElementList.get(0); + } + + public boolean versionSupportsSwaggerDiff( String version) { + int ver = new Integer(version.substring(1)).intValue(); + if ( ver >= HTMLfromOXM.swaggerDiffStartVersion ) { + return true; + } + return false; + } + + public boolean versionSupportsBasePathProperty( String version) { + int ver = new Integer(version.substring(1)).intValue(); + if ( ver <= HTMLfromOXM.swaggerMinBasepath ) { + return true; + } + return false; + } + + protected void updateParentXmlElements(Element parentElement, NodeList moreXmlElementNodes) { + Element xmlElement; + NodeList childNodes; + Node childNode; + + Node refChild = null; + // find childNode with attributes and no children, insert children before that node + childNodes = parentElement.getChildNodes(); + if ( childNodes == null || childNodes.getLength() == 0 ) { + // should not happen since the base parent was chosen if it had children + return; + } + + for ( int i = 0; i < childNodes.getLength(); ++i ) { + refChild = childNodes.item(i); + if ( refChild.hasAttributes() && !refChild.hasChildNodes()) { + break; + } + + } + + for ( int i = 0; i < moreXmlElementNodes.getLength(); ++i ) { + xmlElement = (Element)moreXmlElementNodes.item(i); + childNode = xmlElement.cloneNode(true); + parentElement.insertBefore(childNode, refChild); + } + } + + protected Node getXmlPropertiesNode(Element javaTypeElement ) { + NodeList nl = javaTypeElement.getChildNodes(); + Node child; + for ( int i = 0; i < nl.getLength(); ++i ) { + child = nl.item(i); + if ( "xml-properties".equals(child.getNodeName())) { + return child; + } + } + return null; + } + + protected Node merge( NodeList nl, Node mergeNode ) { + NamedNodeMap nnm = mergeNode.getAttributes(); + Node childNode; + NamedNodeMap childNnm; + + String mergeName = nnm.getNamedItem("name").getNodeValue(); + String mergeValue = nnm.getNamedItem("value").getNodeValue(); + String childName; + String childValue; + for ( int j = 0; j < nl.getLength(); ++j ) { + childNode = nl.item(j); + if ( "xml-property".equals(childNode.getNodeName())) { + childNnm = childNode.getAttributes(); + childName = childNnm.getNamedItem("name").getNodeValue(); + childValue = childNnm.getNamedItem("value").getNodeValue(); + if ( childName.equals(mergeName)) { + // attribute exists + // keep, replace or update + if ( childValue.contains(mergeValue) ) { + return null; + } + if ( mergeValue.contains(childValue) ) { + childNnm.getNamedItem("value").setTextContent(mergeValue); + return null; + } + childNnm.getNamedItem("value").setTextContent(mergeValue + "," + childValue); + return null; + } + } + } + childNode = mergeNode.cloneNode(true); + return childNode; + } + + protected void mergeXmlProperties(Node useChildProperties, NodeList propertiesToMerge ) { + NodeList nl = useChildProperties.getChildNodes(); + Node childNode; + Node newNode; + for ( int i = 0; i < propertiesToMerge.getLength(); ++i ) { + childNode = propertiesToMerge.item(i); + if ( "xml-property".equals(childNode.getNodeName()) ) { + newNode = merge(nl, childNode); + if ( newNode != null ) { + useChildProperties.appendChild(newNode); + } + } + + } + } + + protected void combineXmlProperties(int useElement, List<Element> combineElementList) { + // add or update xml-properties to the referenced element from the combined list + Element javaTypeElement = combineElementList.get(useElement); + NodeList nl = javaTypeElement.getChildNodes(); + Node useChildProperties = getXmlPropertiesNode( javaTypeElement); + int cloneChild = -1; + Node childProperties; + if ( useChildProperties == null ) { + // find xml-properties to clone + for ( int i = 0; i < combineElementList.size(); ++i ) { + if ( i == useElement ) { + continue; + } + childProperties = getXmlPropertiesNode(combineElementList.get(i)); + if ( childProperties != null ) { + useChildProperties = childProperties.cloneNode(true); + javaTypeElement.appendChild(useChildProperties); + cloneChild = i; + } + } + } + NodeList cnl; + // find other xml-properties + for ( int i = 0; i < combineElementList.size(); ++i ) { + if ( i == useElement|| ( cloneChild >= 0 && i <= cloneChild )) { + continue; + } + childProperties = getXmlPropertiesNode(combineElementList.get(i)); + if ( childProperties == null ) { + continue; + } + cnl = childProperties.getChildNodes(); + mergeXmlProperties( useChildProperties, cnl); + } + + } + + protected Element combineElements( String javaTypeName, List<Element> combineElementList ) { + Element javaTypeElement; + NodeList parentNodes; + Element parentElement = null; + NodeList xmlElementNodes; + + int useElement = -1; + if ( combinedJavaTypes.containsKey( javaTypeName) ) { + return combineElementList.get((int)combinedJavaTypes.get(javaTypeName)); + } + for ( int i = 0; i < combineElementList.size(); ++i ) { + javaTypeElement = combineElementList.get(i); + parentNodes = javaTypeElement.getElementsByTagName("java-attributes"); + if ( parentNodes.getLength() == 0 ) { + continue; + } + parentElement = (Element)parentNodes.item(0); + xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + if ( xmlElementNodes.getLength() <= 0 ) { + continue; + } + useElement = i; + break; + } + boolean doCombineElements = true; + if ( useElement < 0 ) { + useElement = 0; + doCombineElements = false; + } else if ( useElement == combineElementList.size() - 1) { + doCombineElements = false; + } + if ( doCombineElements ) { + // get xml-element from other javaTypeElements + Element otherParentElement = null; + for ( int i = 0; i < combineElementList.size(); ++i ) { + if ( i == useElement ) { + continue; + } + javaTypeElement = combineElementList.get(i); + parentNodes = javaTypeElement.getElementsByTagName("java-attributes"); + if ( parentNodes.getLength() == 0 ) { + continue; + } + otherParentElement = (Element)parentNodes.item(0); + xmlElementNodes = otherParentElement.getElementsByTagName("xml-element"); + if ( xmlElementNodes.getLength() <= 0 ) { + continue; + } + // xml-element that are not present + updateParentXmlElements( parentElement, xmlElementNodes); + + } + } + // need to combine xml-properties + combineXmlProperties(useElement, combineElementList ); + combinedJavaTypes.put( javaTypeName, useElement); + return combineElementList.get(useElement); + } +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PatchOperation.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PatchOperation.java new file mode 100644 index 0000000..27882b7 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PatchOperation.java @@ -0,0 +1,113 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.schemagen.GenerateXsd; + +import java.util.StringTokenizer; + +public class PatchOperation { + private String useOpId; + private String xmlRootElementName; + private String tag; + private String path; + private String pathParams; + + public PatchOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) { + super(); + this.useOpId = useOpId; + this.xmlRootElementName = xmlRootElementName; + this.tag = tag; + this.path = path; + this.pathParams = pathParams; + } + + public String toString() { + StringTokenizer st; + st = new StringTokenizer(path, "/"); + //a valid tag is necessary + if ( StringUtils.isEmpty(tag) ) { + return ""; + } + if ( path.contains("/relationship/") ) { // filter paths with relationship-list + return ""; + } + if ( path.endsWith("/relationship-list")) { + return ""; + } + if ( path.startsWith("/search")) { + return ""; + } + //No Patch operation paths end with "relationship" + + if (path.endsWith("/relationship") ) { + return ""; + } + if (!path.endsWith("}")) { + return ""; + } + + StringBuffer pathSb = new StringBuffer(); + StringBuffer relationshipExamplesSb = new StringBuffer(); + if ( path.endsWith("/relationship") ) { + pathSb.append(" " + path + ":\n" ); + } + pathSb.append(" patch:\n"); + pathSb.append(" tags:\n"); + pathSb.append(" - " + tag + "\n"); + + if ( path.endsWith("/relationship") ) { + pathSb.append(" summary: see node definition for valid relationships\n"); + relationshipExamplesSb.append("[See Examples](apidocs/relations/"+GenerateXsd.getAPIVersion()+"/"+useOpId+".json)"); + } else { + pathSb.append(" summary: update an existing " + xmlRootElementName + "\n"); + pathSb.append(" description: |\n"); + pathSb.append(" Update an existing " + xmlRootElementName + "\n"); + pathSb.append(" #\n"); + pathSb.append(" Note: Endpoints that are not devoted to object relationships support both PUT and PATCH operations.\n"); + pathSb.append(" The PUT operation will entirely replace an existing object.\n"); + pathSb.append(" The PATCH operation sends a \"description of changes\" for an existing object. The entire set of changes must be applied. An error result means no change occurs.\n"); + pathSb.append(" #\n"); + pathSb.append(" Other differences between PUT and PATCH are:\n"); + pathSb.append(" #\n"); + pathSb.append(" - For PATCH, you can send any of the values shown in sample REQUEST body. There are no required values.\n"); + pathSb.append(" - For PATCH, resource-id which is a required REQUEST body element for PUT, must not be sent.\n"); + pathSb.append(" - PATCH cannot be used to update relationship elements; there are dedicated PUT operations for this.\n"); + } + pathSb.append(" operationId: Update" + useOpId + "\n"); + pathSb.append(" consumes:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" produces:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" responses:\n"); + pathSb.append(" \"default\":\n"); + pathSb.append(" " + GenerateXsd.getResponsesUrl()); + pathSb.append(" parameters:\n"); + pathSb.append(pathParams); // for nesting + pathSb.append(" - name: body\n"); + pathSb.append(" in: body\n"); + pathSb.append(" description: " + xmlRootElementName + " object that needs to be updated."+relationshipExamplesSb.toString()+"\n"); + pathSb.append(" required: true\n"); + pathSb.append(" schema:\n"); + pathSb.append(" $ref: \"#/patchDefinitions/" + xmlRootElementName + "\"\n"); + return pathSb.toString(); + } + } diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PutOperation.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PutOperation.java new file mode 100644 index 0000000..3e69565 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PutOperation.java @@ -0,0 +1,118 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Modifications Copyright © 2018 IBM. + * ================================================================================ + * 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.onap.aai.schemagen.genxsd; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.schemagen.GenerateXsd; + +public class PutOperation { + public static final String RELATIONSHIP = "relationship"; + private String useOpId; + private String xmlRootElementName; + private String tag; + private String path; + private String pathParams; + private SchemaVersion version; + + public PutOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams, SchemaVersion v) { + super(); + this.useOpId = useOpId; + this.xmlRootElementName = xmlRootElementName; + this.tag = tag; + this.path = path; + this.pathParams = pathParams; + this.version = v; + } + + @Override + public String toString() { + //a valid tag is necessary + if ( StringUtils.isEmpty(tag) ) { + return ""; + } + //All Put operation paths end with "relationship" + //or there is a parameter at the end of the path + //and there is a parameter in the path + if ( path.contains("/"+RELATIONSHIP+"/") ) { // filter paths with relationship-list + return ""; + } + if ( path.endsWith("/"+RELATIONSHIP+"-list")) { + return ""; + } + if ( !path.endsWith("/"+RELATIONSHIP) && !path.endsWith("}") ) { + return ""; + } + if ( path.startsWith("/search")) { + return ""; + } + StringBuffer pathSb = new StringBuffer(); + StringBuffer relationshipExamplesSb = new StringBuffer(); + if ( path.endsWith("/"+RELATIONSHIP) ) { + pathSb.append(" " + path + ":\n" ); + } + pathSb.append(" put:\n"); + pathSb.append(" tags:\n"); + pathSb.append(" - " + tag + "\n"); + + if ( path.endsWith("/"+RELATIONSHIP) ) { + pathSb.append(" summary: see node definition for valid relationships\n"); + } else { + pathSb.append(" summary: create or update an existing " + xmlRootElementName + "\n"); + pathSb.append(" description: |\n Create or update an existing " + xmlRootElementName + ".\n #\n Note! This PUT method has a corresponding PATCH method that can be used to update just a few of the fields of an existing object, rather than a full object replacement. An example can be found in the [PATCH section] below\n"); + } + relationshipExamplesSb.append("[Valid relationship examples shown here](apidocs/relations/"+version.toString()+"/"+useOpId.replace("RelationshipListRelationship", "")+".json)"); + pathSb.append(" operationId: createOrUpdate" + useOpId + "\n"); + pathSb.append(" consumes:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + pathSb.append(" produces:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + pathSb.append(" responses:\n"); + pathSb.append(" \"default\":\n"); + pathSb.append(" " + GenerateXsd.getResponsesUrl()); + + pathSb.append(" parameters:\n"); + pathSb.append(pathParams); // for nesting + pathSb.append(" - name: body\n"); + pathSb.append(" in: body\n"); + pathSb.append(" description: " + xmlRootElementName + " object that needs to be created or updated. "+relationshipExamplesSb.toString()+"\n"); + pathSb.append(" required: true\n"); + pathSb.append(" schema:\n"); + String useElement = xmlRootElementName; + if ( xmlRootElementName.equals("relationship")) { + useElement += "-dict"; + } + pathSb.append(" $ref: \"#/definitions/" + useElement + "\"\n"); + this.tagRelationshipPathMapEntry(); + return pathSb.toString(); + } + + public String tagRelationshipPathMapEntry() { + if ( path.endsWith("/"+RELATIONSHIP) ) { + PutRelationPathSet.add(useOpId, path); + } + return ""; + } + + } diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PutRelationPathSet.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PutRelationPathSet.java new file mode 100644 index 0000000..c666d9f --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/PutRelationPathSet.java @@ -0,0 +1,218 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import com.google.common.collect.Multimap; +import org.apache.commons.text.similarity.LevenshteinDistance; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.schemagen.GenerateXsd; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.*; + +public class PutRelationPathSet { + EdgeIngestor ei; + private static final Logger logger = LoggerFactory.getLogger("PutRelationPathSet.class"); + protected static HashMap<String, String> putRelationPaths = new HashMap<String, String>(); + public static void add(String useOpId, String path) { + putRelationPaths.put(useOpId, path); + } + + String apiPath; + String opId; + SchemaVersion version; + protected ArrayList<String> relations = new ArrayList<String>(); + String objectName = ""; + + public PutRelationPathSet(SchemaVersion v) { + this.version = v; + } + + public PutRelationPathSet(String opId, String path, SchemaVersion v) { + this.apiPath = path.replace("/relationship-list/relationship", ""); + this.opId = opId; + this.version = v; + objectName = DeleteOperation.deletePaths.get(apiPath); + logger.debug("II-apiPath: "+apiPath+"\nPath: "+path+"\nopId="+opId+"\nobjectName="+objectName); + } + private void process(EdgeIngestor edgeIngestor) { + this.ei = edgeIngestor; + this.toRelations(); + this.fromRelations(); + this.writeRelationsFile(); + + } + private void toRelations() { + logger.debug("{“comment”: “Valid TO Relations that can be added”},"); + logger.debug("apiPath: "+apiPath+"\nopId="+opId+"\nobjectName="+objectName); + try { + + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder("ToOnly",objectName).version(version).build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); + relations.add("{\"comment\": \"Valid TO Relations that can be added\"}\n"); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + for(String key : ss) { + results.get(key).stream().filter((i) -> (! i.isPrivateEdge())).forEach((i) ->{ String rel = selectedRelation(i); relations.add(rel); logger.debug("Relation added: "+rel); } ); + } + } catch(Exception e) { + logger.debug("objectName: "+objectName+"\n"+e); + } + } + private String selectedRelation(EdgeRule rule) { + String selectedRelation = ""; + EdgeDescription ed = new EdgeDescription(rule); + logger.debug(ed.getRuleKey()+"Type="+ed.getType()); + String obj = ed.getRuleKey().replace(objectName,"").replace("|",""); + + if(ed.getType() == EdgeDescription.LineageType.UNRELATED) { + String selectObj = getUnrelatedObjectPaths(obj, apiPath); + logger.debug("SelectedObj"+selectObj); + selectedRelation = formatObjectRelationSet(obj,selectObj); + logger.trace("ObjectRelationSet"+selectedRelation); + } else { + String selectObj = getKinObjectPath(obj, apiPath); + logger.debug("SelectedObj"+selectObj); + selectedRelation = formatObjectRelation(obj,selectObj); + logger.trace("ObjectRelationSet"+selectedRelation); + } + return selectedRelation; + } + + private void fromRelations() { + logger.debug("“comment”: “Valid FROM Relations that can be added”"); + try { + + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(objectName,"FromOnly").version(version).build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); + relations.add("{\"comment\": \"Valid FROM Relations that can be added\"}\n"); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + for(String key : ss) { + results.get(key).stream().filter((i) -> (! i.isPrivateEdge())).forEach((i) ->{ String rel = selectedRelation(i); relations.add(rel); logger.debug("Relation added: "+rel); } ); + } + } catch(Exception e) { + logger.debug("objectName: "+objectName+"\n"+e); + } + } + private void writeRelationsFile() { + File examplefilePath = new File(GenerateXsd.getYamlDir() + "/relations/" + version.toString()+"/"+opId.replace("RelationshipListRelationship", "") + ".json"); + + logger.debug(String.join("exampleFilePath: ", examplefilePath.toString())); + FileOutputStream fop = null; + try { + if (!examplefilePath.exists()) { + examplefilePath.getParentFile().mkdirs(); + examplefilePath.createNewFile(); + } + fop = new FileOutputStream(examplefilePath); + } catch(Exception e) { + e.printStackTrace(); + return; + } + try { + if(relations.size() > 0) {fop.write("[\n".getBytes());} + fop.write(String.join(",\n", relations).getBytes()); + if(relations.size() > 0) {fop.write("\n]\n".getBytes());} + fop.flush(); + fop.close(); + } catch (Exception e) { + e.printStackTrace(); + return; + } + finally{ + try { + fop.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + logger.debug(String.join(",\n", relations)); + return; + } + + private static String formatObjectRelationSet(String obj, String selectObj) { + StringBuffer pathSb = new StringBuffer(); + String[] paths = selectObj.split("[|]"); + for (String s: paths) { + logger.trace("SelectOBJ"+s); + pathSb.append(formatObjectRelation(obj, s)+",\n"); + } + pathSb.deleteCharAt(pathSb.length()-2); + return pathSb.toString(); + } + + private static String formatObjectRelation(String obj, String selectObj) { + StringBuffer pathSb = new StringBuffer(); + pathSb.append("{\n"); + pathSb.append("\"related-to\" : \""+obj+"\",\n"); + pathSb.append("\"related-link\" : \""+selectObj+"\"\n"); + pathSb.append("}"); + return pathSb.toString(); + } + + private static String getKinObjectPath(String obj, String apiPath) { + LevenshteinDistance proximity = new LevenshteinDistance(); + String targetPath = ""; + int targetScore = Integer.MAX_VALUE; + int targetMaxScore = 0; + for (Map.Entry<String, String> p : DeleteOperation.deletePaths.entrySet()) { + if(p.getValue().equals(obj)) { + targetScore = (targetScore >= proximity.apply(apiPath, p.getKey())) ? proximity.apply(apiPath, p.getKey()) : targetScore; + targetPath = (targetScore >= proximity.apply(apiPath, p.getKey())) ? p.getKey() : targetPath; + targetMaxScore = (targetMaxScore <= proximity.apply(apiPath, p.getKey())) ? proximity.apply(apiPath, p.getKey()) : targetScore; + logger.trace(proximity.apply(apiPath, p.getKey())+":"+p.getKey()); + logger.trace(proximity.apply(apiPath, p.getKey())+":"+apiPath); + } + } + return targetPath; + } + + private static String getUnrelatedObjectPaths(String obj, String apiPath) { + String targetPath = ""; + logger.trace("Obj:"+obj +"\n" + apiPath); + for (Map.Entry<String, String> p : DeleteOperation.deletePaths.entrySet()) { + if(p.getValue().equals(obj)) { + logger.trace("p.getvalue:"+p.getValue()+"p.getkey:"+p.getKey()); + targetPath += ((targetPath.length() == 0 ? "" : "|") + p.getKey()); + logger.trace("Match:"+apiPath +"\n" + targetPath); + } + } + return targetPath; + } + + public void generateRelations(EdgeIngestor edgeIngestor) { + putRelationPaths.forEach((k,v)->{ + logger.trace("k="+k+"\n"+"v="+v+v.equals("/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/allotted-resources/allotted-resource/{id}/relationship-list/relationship")); + logger.debug("apiPath(Operation): "+v); + logger.debug("Target object: "+v.replace("/relationship-list/relationship", "")); + logger.debug("Relations: "); + PutRelationPathSet prp = new PutRelationPathSet(k, v, this.version); + prp.process(edgeIngestor); + }); + } + +} + diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/XSDElement.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/XSDElement.java new file mode 100644 index 0000000..3ee9495 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/XSDElement.java @@ -0,0 +1,725 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import com.google.common.base.Joiner; +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.setup.SchemaVersion; +import org.w3c.dom.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.StringTokenizer; +import java.util.Vector; + +public class XSDElement implements Element { + Element xmlElementElement; + String maxOccurs; + private static final int VALUE_NONE = 0; + private static final int VALUE_DESCRIPTION = 1; + private static final int VALUE_INDEXED_PROPS = 2; + private static final int VALUE_CONTAINER = 3; + private static final int VALUE_REQUIRES = 4; + + public XSDElement(Element xmlElementElement, String maxOccurs) { + super(); + this.xmlElementElement = xmlElementElement; + this.maxOccurs = maxOccurs; + } + + public XSDElement(Element xmlElementElement) { + super(); + this.xmlElementElement = xmlElementElement; + this.maxOccurs = null; + } + + public String name() { + return this.getAttribute("name"); + } + + public Vector<String> getAddTypes(String version) { + String apiVersionFmt = "." + version + "."; + NamedNodeMap attributes = this.getAttributes(); + Vector<String> addTypeV = new Vector<String>(); // vector of 1 + String addType = null; + + for ( int j = 0; j < attributes.getLength(); ++j ) { + Attr attr = (Attr) attributes.item(j); + String attrName = attr.getNodeName(); + + String attrValue = attr.getNodeValue(); + if ( attrName.equals("type")) { + if ( attrValue.contains(apiVersionFmt) ) { + addType = attrValue.substring(attrValue.lastIndexOf('.')+1); + if ( addTypeV == null ) + addTypeV = new Vector<String>(); + addTypeV.add(addType); + } + + } + } + return addTypeV; + } + + public String getRequiresProperty() { + String elementAlsoRequiresProperty = null; + NodeList xmlPropNodes = this.getElementsByTagName("xml-properties"); + + for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) { + Element xmlPropElement = (Element)xmlPropNodes.item(i); + if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){ + continue; + } + NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property"); + + for ( int j = 0; j < childNodes.getLength(); ++j ) { + Element childElement = (Element)childNodes.item(j); + // get name + int useValue = VALUE_NONE; + NamedNodeMap attributes = childElement.getAttributes(); + for ( int k = 0; k < attributes.getLength(); ++k ) { + Attr attr = (Attr) attributes.item(k); + String attrName = attr.getNodeName(); + String attrValue = attr.getNodeValue(); + if ( attrName == null || attrValue == null ) + continue; + if ( attrName.equals("name") && attrValue.equals("requires")) { + useValue = VALUE_REQUIRES; + } + if ( useValue == VALUE_REQUIRES && attrName.equals("value")) { + elementAlsoRequiresProperty = attrValue; + } + } + } + } + return elementAlsoRequiresProperty; + } + + public String getPathDescriptionProperty() { + String pathDescriptionProperty = null; + NodeList xmlPropNodes = this.getElementsByTagName("xml-properties"); + + for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) { + Element xmlPropElement = (Element)xmlPropNodes.item(i); + if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){ + continue; + } +// This stopped working, replaced with above - should figure out why... +// if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement)) +// continue; + NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property"); + + for ( int j = 0; j < childNodes.getLength(); ++j ) { + Element childElement = (Element)childNodes.item(j); + // get name + int useValue = VALUE_NONE; + NamedNodeMap attributes = childElement.getAttributes(); + for ( int k = 0; k < attributes.getLength(); ++k ) { + Attr attr = (Attr) attributes.item(k); + String attrName = attr.getNodeName(); + String attrValue = attr.getNodeValue(); + if ( attrName == null || attrValue == null ) + continue; + if ( attrName.equals("name") && attrValue.equals("description")) { + useValue = VALUE_DESCRIPTION; + } + if ( useValue == VALUE_DESCRIPTION && attrName.equals("value")) { + pathDescriptionProperty = attrValue; + } + } + } + } + return pathDescriptionProperty; + } + public Vector<String> getIndexedProps() { + Vector<String> indexedProps = new Vector<String>(); + NodeList xmlPropNodes = this.getElementsByTagName("xml-properties"); + + for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) { + Element xmlPropElement = (Element)xmlPropNodes.item(i); + if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement)) + continue; + NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property"); + for ( int j = 0; j < childNodes.getLength(); ++j ) { + Element childElement = (Element)childNodes.item(j); + // get name + int useValue = VALUE_NONE; + NamedNodeMap attributes = childElement.getAttributes(); + for ( int k = 0; k < attributes.getLength(); ++k ) { + Attr attr = (Attr) attributes.item(k); + String attrName = attr.getNodeName(); + String attrValue = attr.getNodeValue(); + if ( attrName == null || attrValue == null ) + continue; + if ( attrValue.equals("indexedProps")) { + useValue = VALUE_INDEXED_PROPS; + } + if ( useValue == VALUE_INDEXED_PROPS && attrName.equals("value")) { + indexedProps = getIndexedProps( attrValue ); + } + } + } + } + return indexedProps; + } + + private static Vector<String> getIndexedProps( String attrValue ) + { + if ( attrValue == null ) + return null; + StringTokenizer st = new StringTokenizer( attrValue, ","); + if ( st.countTokens() == 0 ) + return null; + Vector<String> result = new Vector<String>(); + while ( st.hasMoreTokens()) { + result.add(st.nextToken()); + } + return result; + } + + public String getContainerProperty() { + NodeList xmlPropNodes = this.getElementsByTagName("xml-properties"); + String container = null; + for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) { + Element xmlPropElement = (Element)xmlPropNodes.item(i); + if ( !xmlPropElement.getParentNode().isSameNode(this.xmlElementElement)) + continue; + NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property"); + for ( int j = 0; j < childNodes.getLength(); ++j ) { + Element childElement = (Element)childNodes.item(j); + // get name + int useValue = VALUE_NONE; + NamedNodeMap attributes = childElement.getAttributes(); + for ( int k = 0; k < attributes.getLength(); ++k ) { + Attr attr = (Attr) attributes.item(k); + String attrName = attr.getNodeName(); + String attrValue = attr.getNodeValue(); + if ( attrName == null || attrValue == null ) + continue; + if ( useValue == VALUE_CONTAINER && attrName.equals("value")) { + container = attrValue; + } + if ( attrValue.equals("container")) { + useValue = VALUE_CONTAINER; + } + } + } + } + return container; + } + + public String getQueryParamYAML() { + StringBuffer sbParameter = new StringBuffer(); + sbParameter.append((" - name: " + this.getAttribute("name") + "\n")); + sbParameter.append((" in: query\n")); + if ( this.getAttribute("description") != null && this.getAttribute("description").length() > 0 ) + sbParameter.append((" description: " + this.getAttribute("description") + "\n")); + else + sbParameter.append((" description:\n")); + sbParameter.append((" required: false\n")); + if ( ("java.lang.String").equals(this.getAttribute("type"))) + sbParameter.append(" type: string\n"); + if ( ("java.lang.Long").equals(this.getAttribute("type"))) { + sbParameter.append(" type: integer\n"); + sbParameter.append(" format: int64\n"); + } + if ( ("java.lang.Integer").equals(this.getAttribute("type"))) { + sbParameter.append(" type: integer\n"); + sbParameter.append(" format: int32\n"); + } + if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) { + sbParameter.append(" type: boolean\n"); + } + return sbParameter.toString(); + } + + public String getPathParamYAML(String elementDescription) { + StringBuffer sbParameter = new StringBuffer(); + sbParameter.append((" - name: " + this.getAttribute("name") + "\n")); + sbParameter.append((" in: path\n")); + if ( elementDescription != null && elementDescription.length() > 0 ) + sbParameter.append((" description: " + elementDescription + "\n")); + sbParameter.append((" required: true\n")); + if ( ("java.lang.String").equals(this.getAttribute("type"))) + sbParameter.append(" type: string\n"); + if ( ("java.lang.Long").equals(this.getAttribute("type"))) { + sbParameter.append(" type: integer\n"); + sbParameter.append(" format: int64\n"); + } + if ( ("java.lang.Integer").equals(this.getAttribute("type"))) { + sbParameter.append(" type: integer\n"); + sbParameter.append(" format: int32\n"); + } + if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) { + sbParameter.append(" type: boolean\n"); + } + if(StringUtils.isNotBlank(this.getAttribute("name"))) { + sbParameter.append(" example: "+"__"+this.getAttribute("name").toUpperCase()+"__"+"\n"); + } + return sbParameter.toString(); + } + + public String getHTMLElement(SchemaVersion v, boolean useAnnotation, HTMLfromOXM driver) { + StringBuffer sbElement = new StringBuffer(); + String elementName = this.getAttribute("name"); + String elementType = this.getAttribute("type"); + String elementContainerType = this.getAttribute("container-type"); + String elementIsRequired = this.getAttribute("required"); + String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; + + if ( addType != null ) { + sbElement.append(" <xs:element ref=\"tns:" + driver.getXmlRootElementName(addType)+"\""); + } else { + sbElement.append(" <xs:element name=\"" + elementName +"\""); + } + if ( elementType.equals("java.lang.String")) + sbElement.append(" type=\"xs:string\""); + if ( elementType.equals("java.lang.Long")) + sbElement.append(" type=\"xs:unsignedInt\""); + if ( elementType.equals("java.lang.Integer")) + sbElement.append(" type=\"xs:int\""); + if ( elementType.equals("java.lang.Boolean")) + sbElement.append(" type=\"xs:boolean\""); + if ( addType != null || elementType.startsWith("java.lang.") ) { + sbElement.append(" minOccurs=\"0\""); + } + if ( elementContainerType != null && elementContainerType.equals("java.util.ArrayList")) { + sbElement.append(" maxOccurs=\"" + maxOccurs + "\""); + } + if(useAnnotation) { + String annotation = new XSDElement(xmlElementElement, maxOccurs).getHTMLAnnotation("field", " "); + sbElement.append(StringUtils.isNotEmpty(annotation) ? ">" + OxmFileProcessor.LINE_SEPARATOR : ""); + sbElement.append(annotation); + sbElement.append(StringUtils.isNotEmpty(annotation) ? " </xs:element>" + OxmFileProcessor.LINE_SEPARATOR : "/>" + OxmFileProcessor.LINE_SEPARATOR ); + } else { + sbElement.append("/>" + OxmFileProcessor.LINE_SEPARATOR); + } + return this.getHTMLElementWrapper(sbElement.toString(), v, useAnnotation); +// return sbElement.toString(); + } + + public String getHTMLElementWrapper(String unwrappedElement, SchemaVersion v, boolean useAnnotation) { + + NodeList childNodes = this.getElementsByTagName("xml-element-wrapper"); + + String xmlElementWrapper = null; + if ( childNodes.getLength() > 0 ) { + Element childElement = (Element)childNodes.item(0); + // get name + xmlElementWrapper = childElement == null ? null : childElement.getAttribute("name"); + } + if(xmlElementWrapper == null) + return unwrappedElement; + + StringBuffer sbElement = new StringBuffer(); + sbElement.append(" <xs:element name=\"" + xmlElementWrapper +"\""); + String elementType = xmlElementElement.getAttribute("type"); + String elementIsRequired = this.getAttribute("required"); + String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; + + if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) { + sbElement.append(" minOccurs=\"0\""); + } + sbElement.append(">" + OxmFileProcessor.LINE_SEPARATOR); + sbElement.append(" <xs:complexType>" + OxmFileProcessor.LINE_SEPARATOR); + if(useAnnotation) { + XSDElement javaTypeElement = new XSDElement((Element)this.getParentNode(), maxOccurs); + sbElement.append(javaTypeElement.getHTMLAnnotation("class", " ")); + } + sbElement.append(" <xs:sequence>" + OxmFileProcessor.LINE_SEPARATOR); + sbElement.append(" "); + sbElement.append(unwrappedElement); + sbElement.append(" </xs:sequence>" + OxmFileProcessor.LINE_SEPARATOR); + sbElement.append(" </xs:complexType>" + OxmFileProcessor.LINE_SEPARATOR); + sbElement.append(" </xs:element>" + OxmFileProcessor.LINE_SEPARATOR); + return sbElement.toString(); + } + + public String getHTMLAnnotation(String target, String indentation) { + StringBuffer sb = new StringBuffer(); + List<String> metadata = new ArrayList<>(); + if("true".equals(this.getAttribute("xml-key")) ) { + metadata.add("isKey=true"); + } + + NodeList xmlPropTags = this.getElementsByTagName("xml-properties"); + Element xmlPropElement = null; + for ( int i = 0; i < xmlPropTags.getLength(); ++i ) { + xmlPropElement = (Element)xmlPropTags.item(i); + if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))) + continue; + else + break; + } + if(xmlPropElement != null) { + NodeList xmlProperties = xmlPropElement.getElementsByTagName("xml-property"); + for (int i = 0; i < xmlProperties.getLength(); i++) { + Element item = (Element)xmlProperties.item(i); + String name = item.getAttribute("name"); + String value = item.getAttribute("value"); + if (name.equals("abstract")) { + name = "isAbstract"; + } else if (name.equals("extends")) { + name = "extendsFrom"; + } + metadata.add(name + "=\"" + value.replaceAll("&", "&") + "\""); + } + } + if(metadata.size() == 0) { + return ""; + } + sb.append(indentation +"<xs:annotation>" + OxmFileProcessor.LINE_SEPARATOR); + sb.append( + indentation + " <xs:appinfo>" + OxmFileProcessor.LINE_SEPARATOR + + indentation + " <annox:annotate target=\""+target+"\">@org.onap.aai.annotations.Metadata(" + Joiner.on(",").join(metadata) + ")</annox:annotate>" + OxmFileProcessor.LINE_SEPARATOR + + indentation + " </xs:appinfo>" + OxmFileProcessor.LINE_SEPARATOR); + sb.append(indentation +"</xs:annotation>" + OxmFileProcessor.LINE_SEPARATOR); + return sb.toString(); + } + + public String getTypePropertyYAML() { + StringBuffer sbProperties = new StringBuffer(); + sbProperties.append(" " + this.getAttribute("name") + ":\n"); + sbProperties.append(" type: "); + + if ( ("java.lang.String").equals(this.getAttribute("type"))) + sbProperties.append("string\n"); + else if ( ("java.lang.Long").equals(this.getAttribute("type"))) { + sbProperties.append("integer\n"); + sbProperties.append(" format: int64\n"); + } + else if ( ("java.lang.Integer").equals(this.getAttribute("type"))){ + sbProperties.append("integer\n"); + sbProperties.append(" format: int32\n"); + } + else if ( ("java.lang.Boolean").equals(this.getAttribute("type"))) + sbProperties.append("boolean\n"); + String attrDescription = this.getPathDescriptionProperty(); + if ( attrDescription != null && attrDescription.length() > 0 ) + sbProperties.append(" description: " + attrDescription + "\n"); + String elementAlsoRequiresProperty=this.getRequiresProperty(); + if ( StringUtils.isNotEmpty(elementAlsoRequiresProperty) ) + sbProperties.append(" also requires: " + elementAlsoRequiresProperty + "\n"); + return sbProperties.toString(); + } + + public boolean isStandardType() + { + switch ( this.getAttribute("type") ) { + case "java.lang.String": + case "java.lang.Long": + case "java.lang.Integer": + case"java.lang.Boolean": + return true; + } + return false; + } + + @Override + public String getNodeName() { + return xmlElementElement.getNodeName(); + } + + @Override + public String getNodeValue() throws DOMException { + return xmlElementElement.getNodeValue(); + } + + @Override + public void setNodeValue(String nodeValue) throws DOMException { + xmlElementElement.setNodeValue(nodeValue); + } + + @Override + public short getNodeType() { + return xmlElementElement.getNodeType(); + } + + @Override + public Node getParentNode() { + return xmlElementElement.getParentNode(); + } + + @Override + public NodeList getChildNodes() { + return xmlElementElement.getChildNodes(); + } + + @Override + public Node getFirstChild() { + return xmlElementElement.getFirstChild(); + } + + @Override + public Node getLastChild() { + return xmlElementElement.getLastChild(); + } + + @Override + public Node getPreviousSibling() { + return xmlElementElement.getPreviousSibling(); + } + + @Override + public Node getNextSibling() { + return xmlElementElement.getNextSibling(); + } + + @Override + public NamedNodeMap getAttributes() { + return xmlElementElement.getAttributes(); + } + + @Override + public Document getOwnerDocument() { + return xmlElementElement.getOwnerDocument(); + } + + @Override + public Node insertBefore(Node newChild, Node refChild) throws DOMException { + return xmlElementElement.insertBefore(newChild, refChild); + } + + @Override + public Node replaceChild(Node newChild, Node oldChild) throws DOMException { + return xmlElementElement.replaceChild(newChild, oldChild); + } + + @Override + public Node removeChild(Node oldChild) throws DOMException { + return xmlElementElement.removeChild(oldChild); + } + + @Override + public Node appendChild(Node newChild) throws DOMException { + return xmlElementElement.appendChild(newChild); + } + + @Override + public boolean hasChildNodes() { + return xmlElementElement.hasChildNodes(); + } + + @Override + public Node cloneNode(boolean deep) { + return xmlElementElement.cloneNode(deep); + } + + @Override + public void normalize() { + xmlElementElement.normalize(); + } + + @Override + public boolean isSupported(String feature, String version) { + return xmlElementElement.isSupported(feature, version); + } + + @Override + public String getNamespaceURI() { + return xmlElementElement.getNamespaceURI(); + } + + @Override + public String getPrefix() { + return xmlElementElement.getPrefix(); + } + + @Override + public void setPrefix(String prefix) throws DOMException { + xmlElementElement.setPrefix(prefix); + } + + @Override + public String getLocalName() { + + return xmlElementElement.getLocalName(); + } + + @Override + public boolean hasAttributes() { + return xmlElementElement.hasAttributes(); + } + + @Override + public String getBaseURI() { + return xmlElementElement.getBaseURI(); + } + + @Override + public short compareDocumentPosition(Node other) throws DOMException { + return xmlElementElement.compareDocumentPosition(other); + } + + @Override + public String getTextContent() throws DOMException { + return xmlElementElement.getTextContent(); + } + + @Override + public void setTextContent(String textContent) throws DOMException { + xmlElementElement.setTextContent(textContent); + } + + @Override + public boolean isSameNode(Node other) { + return xmlElementElement.isSameNode(other); + } + + @Override + public String lookupPrefix(String namespaceURI) { + return xmlElementElement.lookupPrefix(namespaceURI); + } + + @Override + public boolean isDefaultNamespace(String namespaceURI) { + return xmlElementElement.isDefaultNamespace(namespaceURI); + } + + @Override + public String lookupNamespaceURI(String prefix) { + return xmlElementElement.lookupNamespaceURI(prefix); + } + + @Override + public boolean isEqualNode(Node arg) { + return xmlElementElement.isEqualNode(arg); + } + + @Override + public Object getFeature(String feature, String version) { + return xmlElementElement.getFeature(feature, version); + } + + @Override + public Object setUserData(String key, Object data, UserDataHandler handler) { + return xmlElementElement.setUserData(key, data, handler); + } + + @Override + public Object getUserData(String key) { + return xmlElementElement.getUserData(key); + } + + @Override + public String getTagName() { + return xmlElementElement.getTagName(); + } + + @Override + public String getAttribute(String name) { + return xmlElementElement.getAttribute(name); + } + + @Override + public void setAttribute(String name, String value) throws DOMException { + xmlElementElement.setAttribute(name, value); + } + + @Override + public void removeAttribute(String name) throws DOMException { + xmlElementElement.removeAttribute(name); + } + + @Override + public Attr getAttributeNode(String name) { + return xmlElementElement.getAttributeNode(name); + } + + @Override + public Attr setAttributeNode(Attr newAttr) throws DOMException { + return xmlElementElement.setAttributeNode(newAttr); + } + + @Override + public Attr removeAttributeNode(Attr oldAttr) throws DOMException { + return xmlElementElement.removeAttributeNode(oldAttr); + } + + @Override + public NodeList getElementsByTagName(String name) { + return xmlElementElement.getElementsByTagName(name); + } + + @Override + public String getAttributeNS(String namespaceURI, String localName) throws DOMException { + return xmlElementElement.getAttributeNS(namespaceURI, localName); + } + + @Override + public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException { + xmlElementElement.setAttributeNS(namespaceURI, qualifiedName, value); + return; + } + + @Override + public void removeAttributeNS(String namespaceURI, String localName) throws DOMException { + xmlElementElement.removeAttributeNS(namespaceURI, localName); + } + + @Override + public Attr getAttributeNodeNS(String namespaceURI, String localName) throws DOMException { + return xmlElementElement.getAttributeNodeNS(namespaceURI, localName); + } + + @Override + public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { + return xmlElementElement.setAttributeNodeNS(newAttr); + } + + @Override + public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException { + return xmlElementElement.getElementsByTagNameNS(namespaceURI, localName); + } + + @Override + public boolean hasAttribute(String name) { + return xmlElementElement.hasAttribute(name); + } + + @Override + public boolean hasAttributeNS(String namespaceURI, String localName) throws DOMException { + return xmlElementElement.hasAttributeNS(namespaceURI, localName); + } + + @Override + public TypeInfo getSchemaTypeInfo() { + return xmlElementElement.getSchemaTypeInfo(); + } + + @Override + public void setIdAttribute(String name, boolean isId) throws DOMException { + xmlElementElement.setIdAttribute(name, isId); + + } + + @Override + public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException { + xmlElementElement.setIdAttributeNS(namespaceURI, localName, isId); + } + + @Override + public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException { + xmlElementElement.setIdAttributeNode(idAttr, isId); + return; + } + + +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/XSDJavaType.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/XSDJavaType.java new file mode 100644 index 0000000..80ab79b --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/XSDJavaType.java @@ -0,0 +1,64 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +public class XSDJavaType extends XSDElement { + StringBuffer pathSb; + StringBuffer definitionsSb; + StringBuffer pathParams; + + public XSDJavaType(Element javaTypeElement) { + super(javaTypeElement); + } +/* + public XSDJavaType(XSDElement javaTypeElement, StringBuffer pathSb, StringBuffer definitionsSb, + StringBuffer pathParams) { + super(javaTypeElement); + this.pathSb = pathSb; + this.definitionsSb = definitionsSb; + this.pathParams = pathParams; + } +*/ + public String getItemName() { + NodeList parentNodes = this.getElementsByTagName("java-attributes"); + if(parentNodes.getLength() == 0) + return null; + Element parentElement = (Element)parentNodes.item(0); + NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(0)); + return xmlElementElement.getAttribute("name"); + } + + public String getArrayType() { + NodeList parentNodes = this.getElementsByTagName("java-attributes"); + if(parentNodes.getLength() == 0) + return null; + Element parentElement = (Element)parentNodes.item(0); + NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(0)); + if ( xmlElementElement.hasAttribute("container-type") && xmlElementElement.getAttribute("container-type").equals("java.util.ArrayList")) { + return xmlElementElement.getAttribute("name"); + } + return null; + } +} 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 new file mode 100644 index 0000000..6e2af57 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java @@ -0,0 +1,590 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.genxsd; + +import com.google.common.collect.Multimap; +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + + +public class YAMLfromOXM extends OxmFileProcessor { + private static final Logger logger = LoggerFactory.getLogger("YAMLfromOXM.class"); +// private static StringBuffer totalPathSbAccumulator = new StringBuffer(); + private static final String root = "../aai-schema/src/main/resources"; + private static final String autoGenRoot = "aai-schema/src/main/resources"; + private static final String generateTypeYAML = "yaml"; + private static final String normalStartDir = "aai-schema-gen"; + private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml"; + private StringBuilder inventoryDefSb = null; + + + private String basePath; + + public YAMLfromOXM(String basePath, SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){ + super(schemaVersions, ni,ei); + this.basePath = basePath; + } + public void setOxmVersion(File oxmFile, SchemaVersion v) { + super.setOxmVersion(oxmFile, v); + } + public void setXmlVersion(String xml, SchemaVersion v){ + super.setXmlVersion(xml, v); + } + + public void setVersion(SchemaVersion v) { + super.setVersion(v); + } + + @Override + public String getDocumentHeader() { + StringBuffer sb = new StringBuffer(); + sb.append("swagger: \"2.0\"\ninfo:\n "); + sb.append("description: |"); + if ( versionSupportsSwaggerDiff(v.toString())) { + sb.append("\n\n [Differences versus the previous schema version](" + "apidocs/aai_swagger_" + v.toString() + ".diff)"); + } + sb.append("\n\n Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.\n\n Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except in compliance with the License.\n\n You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)\n\n 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.\n\n This document is best viewed with Firefox or Chrome. Nodes can be found by appending /#/definitions/node-type-to-find to the path to this document. Edge definitions can be found with the node definitions.\n version: \"" + v.toString() +"\"\n"); + sb.append(" title: Active and Available Inventory REST API\n"); + sb.append(" license:\n name: Apache 2.0\n url: http://www.apache.org/licenses/LICENSE-2.0.html\n"); + sb.append(" contact:\n name:\n url:\n email:\n"); + sb.append("host:\nbasePath: " + basePath + "/" + v.toString() + "\n"); + sb.append("schemes:\n - https\npaths:\n"); + return sb.toString(); + } + + protected void init() throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, EdgeRuleNotFoundException { + super.init(); + } + + @Override + public String process() throws ParserConfigurationException, SAXException, IOException, FileNotFoundException, EdgeRuleNotFoundException { + StringBuffer sb = new StringBuffer(); + StringBuffer pathSb = new StringBuffer(); + try { + init(); + } catch(Exception e) { + logger.error( "Error initializing " + this.getClass(),e); + throw e; + } + pathSb.append(getDocumentHeader()); + StringBuffer definitionsSb = new StringBuffer(); + Element elem; + String javaTypeName; + combinedJavaTypes = new HashMap(); + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + elem = (Element)javaTypeNodes.item(i); + javaTypeName = elem.getAttribute("name"); + if ( !"Inventory".equals(javaTypeName ) ) { + if ( generatedJavaType.containsKey(getXmlRootElementName(javaTypeName) ) ) { + continue; + } + // will combine all matching java-types + elem = getJavaTypeElementSwagger(javaTypeName ); + } + + XSDElement javaTypeElement = new XSDElement(elem); + if ( javaTypeName == null ) { + String msg = "Invalid OXM file: <java-type> has no name attribute in " + oxmFile; + logger.error(msg); + throw new SAXException(msg); + } + namespaceFilter.add(getXmlRootElementName(javaTypeName)); + processJavaTypeElementSwagger( javaTypeName, javaTypeElement, pathSb, + definitionsSb, null, null, null, null, null, null); + } + sb.append(pathSb); + + sb.append(appendDefinitions()); + PutRelationPathSet prp = new PutRelationPathSet(v); + prp.generateRelations(ei); + return sb.toString(); + } + + public String appendDefinitions() { + return appendDefinitions(null); + } + + public String appendDefinitions(Set<String> namespaceFilter) { + //append definitions + if ( inventoryDefSb != null ) { + javaTypeDefinitions.put("inventory", inventoryDefSb.toString()); + } + StringBuffer sb = new StringBuffer("definitions:\n"); + Map<String, String> sortedJavaTypeDefinitions = new TreeMap<String, String>(javaTypeDefinitions); + for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { +// logger.info("Key: "+entry.getKey()+"Value: "+ entry.getValue()); + if(namespaceFilter != null && entry.getKey().matches("service-capabilities")) { + for(String tally : namespaceFilter) { logger.debug("Marker: "+tally);} + } + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } + logger.debug("Key: "+entry.getKey()+"Test: "+ (entry.getKey() == "relationship-dict")); + if(entry.getKey().matches("relationship-dict")) { + String jb=entry.getValue(); + logger.debug("Value: "+jb); + int ndx=jb.indexOf("related-to-property:"); + if(ndx > 0) { + jb=jb.substring(0, ndx); + jb=jb.replaceAll(" +$", ""); + } + logger.debug("Value-after: "+jb); + sb.append(jb); + continue; + } + sb.append(entry.getValue()); + } + + sb.append("patchDefinitions:\n"); + for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } + String jb=entry.getValue().replaceAll("/definitions/", "/patchDefinitions/"); + int ndx=jb.indexOf("relationship-list:"); + if(ndx > 0) { + jb=jb.substring(0, ndx); + jb=jb.replaceAll(" +$", ""); + } + int ndx1=jb.indexOf("resource-version:"); + logger.debug("Key: "+entry.getKey()+" index: " + ndx1); + logger.debug("Value: "+jb); + if(ndx1 > 0) { + jb=jb.substring(0, ndx1); + jb=jb.replaceAll(" +$", ""); + } + logger.debug("Value-after: "+jb); + sb.append(jb); + } + + sb.append("getDefinitions:\n"); + for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } + String jb=entry.getValue().replaceAll("/definitions/", "/getDefinitions/"); + sb.append(jb); + } + return sb.toString(); + } + + private String getDictionary(String resource ) { + StringBuffer dictSb = new StringBuffer(); + dictSb.append(" " + resource + ":\n"); + dictSb.append(" description: |\n"); + dictSb.append(" dictionary of " + resource + "\n" ); + dictSb.append(" type: object\n"); + dictSb.append(" properties:\n"); + dictSb.append(" " + resource + ":\n"); + dictSb.append(" type: array\n"); + dictSb.append(" items:\n"); + dictSb.append(" $ref: \"#/definitions/" + resource + "-dict\"\n"); + return dictSb.toString(); + } + + private String processJavaTypeElementSwagger( String javaTypeName, Element javaTypeElement, + StringBuffer pathSb, StringBuffer definitionsSb, String path, String tag, String opId, + String getItemName, StringBuffer pathParams, String validEdges) { + + String xmlRootElementName = getXMLRootElementName(javaTypeElement); + StringBuilder definitionsLocalSb = new StringBuilder(256); + + String useTag = null; + String useOpId = null; + logger.debug("tag="+tag); + if ( tag != null ) { + switch ( tag ) { + case "Network": + case "ServiceDesignAndCreation": + case "Business": + case "LicenseManagement": + case "CloudInfrastructure": + case "Common": + break; + default: + logger.debug("javaTypeName="+javaTypeName); + return null; + } + } + + if ( !javaTypeName.equals("Inventory") ) { + if ( javaTypeName.equals("AaiInternal")) + return null; + if ( opId == null ) + useOpId = javaTypeName; + else + useOpId = opId + javaTypeName; + if ( tag == null ) + useTag = javaTypeName; + } + path = xmlRootElementName.equals("inventory") ? "" : (path == null) ? "/" + xmlRootElementName : path + "/" + xmlRootElementName; + XSDJavaType javaType = new XSDJavaType(javaTypeElement); + if ( getItemName != null) { + if ( getItemName.equals("array") ) + return javaType.getArrayType(); + else + return javaType.getItemName(); + } + + NodeList parentNodes = javaTypeElement.getElementsByTagName("java-attributes"); + if ( parentNodes.getLength() == 0 ) { + logger.debug( "no java-attributes for java-type " + javaTypeName); + return ""; + } + + String pathDescriptionProperty = javaType.getPathDescriptionProperty(); + String container = javaType.getContainerProperty(); + Vector<String> indexedProps = javaType.getIndexedProps(); + Vector<String> containerProps = new Vector<String>(); + if(container != null) { + logger.debug("javaTypeName " + javaTypeName + " container:" + container +" indexedProps:"+indexedProps); + } + + Element parentElement = (Element)parentNodes.item(0); + NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + + StringBuffer sbParameters = new StringBuffer(); + StringBuffer sbRequired = new StringBuffer(); + int requiredCnt = 0; + int propertyCnt = 0; + StringBuffer sbProperties = new StringBuffer(); + + if ( appliedPaths.containsKey(path)) + return null; + + StringTokenizer st = new StringTokenizer(path, "/"); + logger.debug("path: " + path + " st? " + st.toString()); + if ( st.countTokens() > 1 && getItemName == null ) { + logger.debug("appliedPaths: " + appliedPaths + " containsKey? " + appliedPaths.containsKey(path)); + appliedPaths.put(path, xmlRootElementName); + } + + Vector<String> addTypeV = null; + for ( int i = 0; i < xmlElementNodes.getLength(); ++i ) { + XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(i)); + if ( !xmlElementElement.getParentNode().isSameNode(parentElement)) + continue; + String elementDescription=xmlElementElement.getPathDescriptionProperty(); + if(getItemName == null) { + addTypeV = xmlElementElement.getAddTypes(v.toString()); + } + if ( "true".equals(xmlElementElement.getAttribute("xml-key"))) { + path += "/{" + xmlElementElement.getAttribute("name") + "}"; + } + logger.debug("path: " + path); + logger.debug( "xmlElementElement.getAttribute(required):"+xmlElementElement.getAttribute("required") ); + + if ( ("true").equals(xmlElementElement.getAttribute("required"))) { + if ( requiredCnt == 0 ) + sbRequired.append(" required:\n"); + ++requiredCnt; + if ( addTypeV == null || addTypeV.isEmpty()) { + sbRequired.append(" - " + xmlElementElement.getAttribute("name") + "\n"); + } else { + for ( int k = 0; k < addTypeV.size(); ++k ) { + sbRequired.append(" - " + getXmlRootElementName(addTypeV.elementAt(k)) + ":\n"); + } + } + } + + if ( "true".equals(xmlElementElement.getAttribute("xml-key")) ) { + sbParameters.append(xmlElementElement.getPathParamYAML(elementDescription)); + } + if ( indexedProps != null + && indexedProps.contains(xmlElementElement.getAttribute("name") ) ) { + containerProps.add(xmlElementElement.getQueryParamYAML()); + GetOperation.addContainerProps(container, containerProps); + } + if ( xmlElementElement.isStandardType()) { + sbProperties.append(xmlElementElement.getTypePropertyYAML()); + ++propertyCnt; + } + + StringBuffer newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); + for ( int k = 0; addTypeV != null && k < addTypeV.size(); ++k ) { + String addType = addTypeV.elementAt(k); + namespaceFilter.add(getXmlRootElementName(addType)); + logger.debug("addType: "+ addType); + + if ( opId == null || !opId.contains(addType)) { + processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, null, + newPathParams, validEdges); + } + // need item name of array + String itemName = processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, + "array", null, null ); + + if ( itemName != null ) { + if ( addType.equals("AaiInternal") ) { + logger.debug( "addType AaiInternal, skip properties"); + + } else if ( getItemName == null) { + ++propertyCnt; + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: array\n items:\n"); + sbProperties.append(" $ref: \"#/definitions/" + (itemName == "" ? "inventory-item-data" : itemName) + "\"\n"); + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + } + } else { + if ( ("java.util.ArrayList").equals(xmlElementElement.getAttribute("container-type"))) { + // need properties for getXmlRootElementName(addType) + namespaceFilter.add(getXmlRootElementName(addType)); + newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); + processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, + null, newPathParams, validEdges ); + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: array\n items: \n"); + sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + + } else { + //Make sure certain types added to the filter don't appear + if(nodeFilter.contains(getXmlRootElementName(addType))) { + ; + } else { + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: object\n"); + sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); + } + } + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + ++propertyCnt; + } + } + } + + if ( sbParameters.toString().length() > 0 ) { + if ( pathParams == null ) + pathParams = new StringBuffer(); + pathParams.append(sbParameters); + } + GetOperation get = new GetOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString()); + pathSb.append(get.toString()); + logger.debug("opId vs useOpId:"+opId+" vs "+useOpId+" PathParams="+pathParams); + // add PUT + PutOperation put = new PutOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString(), this.v); + pathSb.append(put.toString()); + // add PATCH + PatchOperation patch = new PatchOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString()); + pathSb.append(patch.toString()); + // add DELETE + DeleteOperation del = new DeleteOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString()); + pathSb.append(del.toString()); + if ( generatedJavaType.containsKey(xmlRootElementName) ) { + logger.debug("xmlRootElementName(1)="+xmlRootElementName); + return null; + } + + boolean processingInventoryDef = false; + String dict = null; + if ( xmlRootElementName.equals("inventory")) { + // inventory properties for each oxm to be concatenated + processingInventoryDef = true; + if ( inventoryDefSb == null ) { + inventoryDefSb = new StringBuilder(); + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" properties:\n"); + } + } else if ( xmlRootElementName.equals("relationship")) { + definitionsSb.append(" " + "relationship-dict" + ":\n"); + definitionsLocalSb.append(" " + "relationship-dict" + ":\n"); + dict = getDictionary(xmlRootElementName); + } else { + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + } +// Collection<EdgeDescription> edges = edgeRuleSet.getEdgeRules(xmlRootElementName ); + DeleteFootnoteSet footnotes = new DeleteFootnoteSet(xmlRootElementName); + StringBuffer sbEdge = new StringBuffer(); + LinkedHashSet<String> preventDelete = new LinkedHashSet<String>(); + String prevent=null; + String nodeCaption = new String(" ###### Related Nodes\n"); + try { + EdgeRuleQuery q = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).fromOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + nodeCaption=""; + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - TO ").append(i.getTo()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - TO "+i.getTo()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("TO", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("OUT")))).forEach((i) ->{ preventDelete.add(i.getTo().toUpperCase());} ); + } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+" from edge exception\n", e); + } + try { + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).toOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - FROM "+i.getFrom()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("FROM", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - FROM ").append(i.getFrom()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("IN")))).forEach((i) ->{ preventDelete.add(i.getFrom().toUpperCase());} ); + } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+" to edge exception\n", e); + } + if(preventDelete.size() > 0) { + prevent = xmlRootElementName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); + logger.debug(prevent); + } + + if(StringUtils.isNotEmpty(prevent)) { + footnotes.add(prevent); + } + if(footnotes.footnotes.size() > 0) { + sbEdge.append(footnotes.toString()); + } + validEdges = sbEdge.toString(); + + // Handle description property. Might have a description OR valid edges OR both OR neither. + // Only put a description: tag if there is at least one. + if (StringUtils.isNotEmpty(pathDescriptionProperty) || StringUtils.isNotEmpty(validEdges) ) { + definitionsSb.append(" description: |\n"); + definitionsLocalSb.append(" description: |\n"); + + if ( pathDescriptionProperty != null ) { + definitionsSb.append(" " + pathDescriptionProperty + "\n" ); + definitionsLocalSb.append(" " + pathDescriptionProperty + "\n" ); + } + definitionsSb.append(validEdges); + definitionsLocalSb.append(validEdges); + } + + if ( requiredCnt > 0 ) { + definitionsSb.append(sbRequired); + definitionsLocalSb.append(sbRequired); + } + + if ( propertyCnt > 0 ) { + definitionsSb.append(" properties:\n"); + definitionsSb.append(sbProperties); + if ( !processingInventoryDef) { + definitionsLocalSb.append(" properties:\n"); + } + definitionsLocalSb.append(sbProperties); + } + try { + namespaceFilter.add(xmlRootElementName); + if ( xmlRootElementName.equals("inventory") ) { + //will add to javaTypeDefinitions at end + inventoryDefSb.append(definitionsLocalSb.toString()); + } else if ( xmlRootElementName.equals("relationship") ){ + javaTypeDefinitions.put(xmlRootElementName, dict); + javaTypeDefinitions.put(xmlRootElementName+ "-dict", definitionsLocalSb.toString()); + } else { + javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + logger.error("Exception adding in javaTypeDefinitions",e); + } + if ( xmlRootElementName.equals("inventory") ) { + logger.trace("skip xmlRootElementName(2)="+xmlRootElementName); + return null; + } + generatedJavaType.put(xmlRootElementName, null); +/* + if( validTag(javaTypeName) && javaTypeName == useTag && tag == null) { + String nameSpaceResult = getDocumentHeader()+pathSb.toString()+appendDefinitions(namespaceFilter); + writeYAMLfile(javaTypeName, nameSpaceResult); + totalPathSbAccumulator.append(pathSb); + pathSb.delete(0, pathSb.length()); + namespaceFilter.clear(); + } +*/ + logger.trace("xmlRootElementName(2)="+xmlRootElementName); + return null; + } + + private void writeYAMLfile(String outfileName, String fileContent) { + outfileName = (StringUtils.isEmpty(outfileName)) ? "aai_swagger" : outfileName; + outfileName = (outfileName.lastIndexOf(File.separator) == -1) ? yaml_dir + File.separator +outfileName+"_" + v.toString() + "." + generateTypeYAML : outfileName; + File outfile = new File(outfileName); + File parentDir = outfile.getParentFile(); + if(parentDir != null && ! parentDir.exists()) + parentDir.mkdirs(); + try { + outfile.createNewFile(); + } catch (IOException e) { + logger.error( "Exception creating output file " + outfileName); + e.printStackTrace(); + } + try { + Charset charset = Charset.forName("UTF-8"); + Path path = Paths.get(outfileName); + try(BufferedWriter bw = Files.newBufferedWriter(path, charset)){ + bw.write(fileContent); + } + } catch ( IOException e) { + logger.error( "Exception writing output file " + outfileName); + e.printStackTrace(); + } + } + + public boolean validTag(String tag) { + if(tag != null) { + switch ( tag ) { + case "Network": +// case "Search": +// case "Actions": + case "ServiceDesignAndCreation": + case "Business": + case "LicenseManagement": + case "CloudInfrastructure": + case "Common": + return true; + } + } + return false; + } + +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/Api.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/Api.java new file mode 100644 index 0000000..717b710 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/Api.java @@ -0,0 +1,318 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.swagger; + +import java.util.List; +import java.util.Map; + +public class Api { + + private String path; + + private List<HttpVerb> httpMethods; + + private String tag; + + public List<HttpVerb> getHttpMethods() { + return httpMethods; + } + + public void setHttpMethods(List<HttpVerb> httpMethods) { + this.httpMethods = httpMethods; + } + + public String getTag(){ + + if(this.tag != null){ + return this.tag; + } + + if(this.httpMethods != null){ + if(this.httpMethods.size() != 0){ + if(this.httpMethods.get(0).getTags() != null){ + if(this.httpMethods.get(0).getTags().size() != 0){ + this.tag = this.httpMethods.get(0).getTags().get(0); + } + } + } + } + + if(this.tag == null){ + this.tag = ""; + } + + return this.tag; + } + + @Override + public String toString() { + return "Api{" + + "path='" + path + '\'' + + ", httpMethods=" + httpMethods + + '}'; + } + + public void setPath(String path) { + this.path = path; + } + + public String getPath() { + return this.path; + } + + public String getOperation(){ + + if(this.path != null){ + return this.path.replaceAll("[^a-zA-Z0-9\\-]", "-") + "-"; + } + + return ""; + } + + public static class HttpVerb { + + private List<String> tags; + + private String type; + + private String summary; + + private String operationId; + + private List<String> consumes; + + private boolean consumerEnabled; + + private List<String> produces; + + private List<Response> responses; + + private List<Map<String, Object>> parameters; + + private Map<String, Object> bodyParameters; + + private boolean bodyParametersEnabled; + + private boolean parametersEnabled; + + private String schemaLink; + + private String schemaType; + + private boolean hasReturnSchema; + + private String returnSchemaLink; + + private String returnSchemaObject; + + public void setConsumerEnabled(boolean consumerEnabled){ + this.consumerEnabled = consumerEnabled; + } + + public boolean isConsumerEnabled() { + return consumerEnabled; + } + + + public List<String> getTags() { + return tags; + } + + public void setTags(List<String> tags) { + this.tags = tags; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getSummary() { + return summary; + } + + public void setSummary(String summary) { + this.summary = summary; + } + + public String getOperationId() { + return operationId; + } + + public void setOperationId(String operationId) { + this.operationId = operationId; + } + + public List<String> getConsumes() { + return consumes; + } + + public void setConsumes(List<String> consumes) { + this.consumes = consumes; + } + + public List<String> getProduces() { + return produces; + } + + public void setProduces(List<String> produces) { + this.produces = produces; + } + + public List<Response> getResponses() { + return responses; + } + + public void setResponses(List<Response> responses) { + this.responses = responses; + } + + public List<Map<String, Object>> getParameters() { + return parameters; + } + + public void setParameters(List<Map<String, Object>> parameters) { + this.parameters = parameters; + } + + @Override + public String toString() { + return "HttpVerb{" + + "tags=" + tags + + ", type='" + type + '\'' + + ", summary='" + summary + '\'' + + ", operationId='" + operationId + '\'' + + ", consumes=" + consumes + + ", produces=" + produces + + ", responses=" + responses + + ", parameters=" + parameters + + '}'; + } + + public void setParametersEnabled(boolean b) { + this.parametersEnabled = b; + } + + public boolean isParametersEnabled() { + return parametersEnabled; + } + + public boolean isBodyParametersEnabled() { + return bodyParametersEnabled; + } + public boolean isOpNotPatch() { + return type.equalsIgnoreCase("patch") ? false : true; + } + + public void setBodyParametersEnabled(boolean bodyParametersEnabled) { + this.bodyParametersEnabled = bodyParametersEnabled; + } + + public Map<String, Object> getBodyParameters() { + return bodyParameters; + } + + public void setBodyParameters(Map<String, Object> bodyParameters) { + this.bodyParameters = bodyParameters; + } + + public String getSchemaLink() { + return schemaLink; + } + + public void setSchemaLink(String schemaLink) { + this.schemaLink = schemaLink; + } + + public String getSchemaType() { + return schemaType; + } + + public void setSchemaType(String schemaType) { + this.schemaType = schemaType; + } + + public boolean isHasReturnSchema() { + return hasReturnSchema; + } + + public void setHasReturnSchema(boolean hasReturnSchema) { + this.hasReturnSchema = hasReturnSchema; + } + + public String getReturnSchemaLink() { + return returnSchemaLink; + } + + public void setReturnSchemaLink(String returnSchemaLink) { + this.returnSchemaLink = returnSchemaLink; + } + + public String getReturnSchemaObject() { + return returnSchemaObject; + } + + public void setReturnSchemaObject(String returnSchemaObject) { + this.returnSchemaObject = returnSchemaObject; + } + + public static class Response { + + private String responseCode; + + private String description; + + private String version; + + public String getResponseCode() { + return responseCode; + } + + public void setResponseCode(String responseCode) { + this.responseCode = responseCode; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "Response{" + + "responseCode='" + responseCode + '\'' + + ", description='" + description + '\'' + + '}'; + } + + public void setVersion(String version) { + this.version = version; + } + } + + } + +} diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/Definition.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/Definition.java new file mode 100644 index 0000000..4a52020 --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/Definition.java @@ -0,0 +1,198 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.swagger; + +import java.util.List; + +public class Definition { + + private String definitionName; + + private String definitionDescription; + + private List<Property> propertyList; + + private List<Property> schemaPropertyList; + + private List<Property> regularPropertyList; + + private boolean hasDescription; + + public String getDefinitionName() { + return definitionName; + } + + public void setDefinitionName(String definitionName) { + this.definitionName = definitionName; + } + + public List<Property> getPropertyList() { + return propertyList; + } + + public void setPropertyList(List<Property> propertyList) { + this.propertyList = propertyList; + } + + public String getDefinitionDescription() { + return definitionDescription; + } + + public void setDefinitionDescription(String definitionDescription) { + this.definitionDescription = definitionDescription; + } + + @Override + public String toString() { + return "Definition{" + + "definitionName='" + definitionName + '\'' + + ", definitionDescription='" + definitionDescription + '\'' + + ", propertyList=" + propertyList + + '}'; + } + + public boolean isHasDescription() { + return hasDescription; + } + + public void setHasDescription(boolean hasDescription) { + this.hasDescription = hasDescription; + } + + public List<Property> getSchemaPropertyList() { + return schemaPropertyList; + } + + public void setSchemaPropertyList(List<Property> schemaPropertyList) { + this.schemaPropertyList = schemaPropertyList; + } + + public List<Property> getRegularPropertyList() { + return regularPropertyList; + } + + public void setRegularPropertyList(List<Property> regularPropertyList) { + this.regularPropertyList = regularPropertyList; + } + + public static class Property { + + private String propertyName; + + private String propertyDescription; + + private boolean hasPropertyDescription; + + private String propertyType; + + private boolean hasType; + + private String propertyReference; + + private String propertyReferenceObjectName; + + private boolean isRequired; + + private boolean hasPropertyReference; + + public Property(){} + + public String getPropertyName() { + return propertyName; + } + + public void setPropertyName(String propertyName) { + this.propertyName = propertyName; + } + + public String getPropertyType() { + return propertyType; + } + + public void setPropertyType(String propertyType) { + this.propertyType = propertyType; + } + + public String getPropertyReference() { + return propertyReference; + } + + public void setPropertyReference(String propertyReference) { + this.propertyReference = propertyReference; + } + + @Override + public String toString() { + return "Property{" + + "propertyName='" + propertyName + '\'' + + ", propertyType='" + propertyType + '\'' + + ", propertyReference='" + propertyReference + '\'' + + '}'; + } + + public boolean isHasType() { + return hasType; + } + + public void setHasType(boolean hasType) { + this.hasType = hasType; + } + + public boolean isRequired() { + return isRequired; + } + + public void setRequired(boolean required) { + isRequired = required; + } + + public boolean isHasPropertyReference() { + return hasPropertyReference; + } + + public void setHasPropertyReference(boolean hasPropertyReference) { + this.hasPropertyReference = hasPropertyReference; + } + + public String getPropertyReferenceObjectName() { + return propertyReferenceObjectName; + } + + public void setPropertyReferenceObjectName(String propertyReferenceObjectName) { + this.propertyReferenceObjectName = propertyReferenceObjectName; + } + + public String getPropertyDescription() { + return propertyDescription; + } + + public void setPropertyDescription(String propertyDescription) { + this.propertyDescription = propertyDescription; + } + + public boolean isHasPropertyDescription() { + return hasPropertyDescription; + } + + public void setHasPropertyDescription(boolean hasPropertyDescription) { + this.hasPropertyDescription = hasPropertyDescription; + } + } +} 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 new file mode 100644 index 0000000..41c88ec --- /dev/null +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/swagger/GenerateSwagger.java @@ -0,0 +1,487 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 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.onap.aai.schemagen.swagger; + +import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; +import com.fasterxml.jackson.dataformat.yaml.snakeyaml.constructor.SafeConstructor; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import org.onap.aai.setup.SchemaVersions; + +import java.io.*; +import java.util.*; +import java.util.stream.Collectors; + +public class GenerateSwagger { + + public static final String LINE_SEPARATOR = System.getProperty("line.separator"); + public static final String DEFAULT_WIKI = ""; + + public static final String DEFAULT_SCHEMA_DIR = "../aai-schema"; + //if the program is run from aai-common, use this directory as default" + public static final String ALT_SCHEMA_DIR = "aai-schema"; + //used to check to see if program is run from aai-schema-gen + public static final String DEFAULT_RUN_DIR = "aai-schema-gen"; + + public static SchemaVersions schemaVersions; + + public SchemaVersions getSchemaVersions() { + return schemaVersions; + } + + + + public static void main(String[] args) throws IOException, TemplateException { + + + + + // SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + String CURRENT_VERSION = schemaVersions.getDefaultVersion().toString(); + String schemaDir = System.getProperty("aai.schema.dir"); + String versionToGenerate = System.getProperty("aai.generate.version"); + String wikiLink = System.getProperty("aai.wiki.link"); + String release = System.getProperty("aai.release", "onap"); + + if(schemaDir == null){ + if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) { + System.out.println("Warning: Schema directory is not set so using default schema dir: " + ALT_SCHEMA_DIR); + schemaDir = ALT_SCHEMA_DIR; + } + else { + System.out.println("Warning: Schema directory is not set so using default schema dir: " + DEFAULT_SCHEMA_DIR); + schemaDir = DEFAULT_SCHEMA_DIR; + } + } + + if(versionToGenerate == null){ + System.out.println("Warning: Version to generate is not set so using default versionToGenerate " + CURRENT_VERSION); + versionToGenerate = CURRENT_VERSION; + } + + if(wikiLink == null){ + System.out.println("Warning: aai.wiki.link property is not set so using default"); + wikiLink = DEFAULT_WIKI; + } + + String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml"; + File swaggerYamlFile = new File(yamlFile); + + if(!swaggerYamlFile.exists()){ + System.err.println("Unable to find the swagger yaml file: " + swaggerYamlFile); + System.exit(1); + } + + Yaml yaml = new Yaml(new SafeConstructor()); + Map<String, Object> swaggerMap = null; + + try (BufferedReader reader = new BufferedReader(new FileReader(swaggerYamlFile))){ + swaggerMap = (Map<String, Object>) yaml.load(reader); + } catch(Exception ex){ + ex.printStackTrace(); + } + + if(null == swaggerMap) { + throw new IOException(); + } + + Map<String, Object> map = (Map<String, Object>) swaggerMap.get("paths"); + Map<String, Object> schemaDefinitionmap = (Map<String, Object>) swaggerMap.get("definitions"); + Map<String, Object> infoMap = (Map<String, Object>) swaggerMap.get("info"); + Map<String, List<Api>> tagMap = new LinkedHashMap<>(); + + List<Api> apis = convertToApi(map); + apis.forEach((api) -> { + if(!tagMap.containsKey(api.getTag())){ + List<Api> newApis = new ArrayList<>(); + newApis.add(api); + tagMap.put(api.getTag(), newApis); + } else { + tagMap.get(api.getTag()).add(api); + } + }); + + Map<String, List<Api>> sortedTagMap = new TreeMap<>(tagMap); + sortedTagMap.forEach((key, value) -> { + value.sort(Comparator.comparing(Api::getPath)); + }); + + Map<String, Object> resultMap = new HashMap<>(); + + List<Definition> definitionList = convertToDefinition(schemaDefinitionmap); + + definitionList = definitionList + .stream().sorted(Comparator.comparing(Definition::getDefinitionName)).collect(Collectors.toList()); + + resultMap.put("aaiApis", tagMap); + resultMap.put("sortedAaiApis", sortedTagMap); + resultMap.put("wikiLink", wikiLink); + resultMap.put("definitions", definitionList); + resultMap.put("version", versionToGenerate); + if (infoMap.containsKey("description")) { + String infoDescription = infoMap.get("description").toString(); + + infoDescription = Arrays.stream(infoDescription.split("\n")) + .map(line -> { + line = line.trim(); + String hyperLink = ""; + if(line.trim().contains("Differences versus")) { + return String.format(""); + } + if(line.trim().contains("https://")){ + int startIndex = line.indexOf("https://"); + int endIndex = line.lastIndexOf("/"); + hyperLink = line.substring(startIndex, endIndex); + return String.format("<a href=\"%s\">%s</a><br/>", hyperLink, line); + } + return String.format("%s<br/>", line); + }) + + .collect(Collectors.joining(LINE_SEPARATOR)); + + resultMap.put("description", infoDescription); + } + + Configuration configuration = new Configuration(); + configuration.setClassForTemplateLoading(Api.class, "/"); + String resourcePath = "src/main/resources"; + if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) { + configuration.setDirectoryForTemplateLoading(new File(DEFAULT_RUN_DIR + "/" + resourcePath)); + } + else { + configuration.setDirectoryForTemplateLoading(new File(resourcePath)); + } + Template template = configuration.getTemplate("swagger.html.ftl"); + + String outputDirStr = schemaDir + "/src/main/resources/" + release + "/aai_swagger_html"; + + File outputDir = new File(outputDirStr); + + if(!outputDir.exists()){ + boolean resp = outputDir.mkdir(); + if(!resp){ + System.err.println("Unable to create the directory: " + outputDirStr); + System.exit(1); + } + } else if(outputDir.isFile()){ + System.err.println("Unable to create the directory: " + outputDirStr + " since a filename with that string exists"); + System.exit(1); + } + + Writer file = new FileWriter(new File(outputDirStr + "/aai_swagger_" + versionToGenerate + ".html")); + template.process(resultMap, file); + } + + public static List<Api> convertToApi(Map<String, Object> pathMap){ + + if(pathMap == null) + throw new IllegalArgumentException(); + + List<Api> apis = new ArrayList<>(); + + pathMap.forEach( (pathKey, pathValue) -> { + + Api api = new Api(); + Map<String, Object> httpVerbMap = (Map<String, Object>) pathValue; + List<Api.HttpVerb> httpVerbs = new ArrayList<>(); + + api.setPath(pathKey); + + httpVerbMap.forEach((httpVerbKey, httpVerbValue) -> { + + Api.HttpVerb httpVerb = new Api.HttpVerb(); + + Map<String, Object> httpVerbValueMap = (Map<String,Object>)httpVerbValue; + + httpVerb.setType(httpVerbKey); + + if(httpVerbValueMap.containsKey("tags")){ + httpVerb.setTags((List<String>)httpVerbValueMap.get("tags")); + } + + if(httpVerbValueMap.containsKey("summary")){ + httpVerb.setSummary((String)httpVerbValueMap.get("summary")); + } + + if(httpVerbValueMap.containsKey("operationId")){ + httpVerb.setOperationId((String)httpVerbValueMap.get("operationId")); + } + + if(httpVerbValueMap.containsKey("consumes")){ + httpVerb.setConsumes((List<String>)httpVerbValueMap.get("consumes")); + if(httpVerb.getConsumes() != null){ + httpVerb.setConsumerEnabled(true); + } + } + + if(httpVerbValueMap.containsKey("produces")){ + httpVerb.setProduces((List<String>)httpVerbValueMap.get("produces")); + } + + if(httpVerbValueMap.containsKey("parameters")){ + List<Map<String, Object>> parameters = (List<Map<String, Object>>) httpVerbValueMap.get("parameters"); + List<Map<String, Object>> requestParameters = parameters + .stream() + .filter((parameter) -> !parameter.get("name").equals("body")) + .collect(Collectors.toList()); + httpVerb.setParameters(requestParameters); + if(httpVerb.getParameters() != null){ + httpVerb.setParametersEnabled(true); + } + + List<Map<String, Object>> requestBodyList = parameters + .stream() + .filter((parameter) -> parameter.get("name").equals("body")) + .collect(Collectors.toList()); + + Map<String, Object> requestBody = null; + + if(requestBodyList != null && requestBodyList.size() == 1){ + requestBody = requestBodyList.get(0); + for(String key : requestBody.keySet()) { + //Filter out all the relationship links that appear in the YAML + if(key.equals("description")) { + String reqBody=(String)requestBody.get(key); + if(reqBody.replaceAll("\\[.*.json\\)", "") != reqBody) { + requestBody.put(key, reqBody.replaceAll("\\[.*.json\\)", "")); + } + } + //Filter out all the patchDefinition links that appear in the YAML + if(key.equals("schema")) { + LinkedHashMap<String,String> reqBody = (LinkedHashMap<String,String>)requestBody.get(key); + String schema=reqBody.get("$ref"); + String schemaNopatch = schema.replace("patchDefinitions", "definitions"); + + if(! schema.equals(schemaNopatch)) { + reqBody.put("$ref", schemaNopatch); + requestBody.put(key, reqBody); + } + } + } + httpVerb.setBodyParametersEnabled(true); + httpVerb.setBodyParameters(requestBody); + + if(requestBody != null && requestBody.containsKey("schema")){ + Map<String, Object> schemaMap = (Map<String, Object>)requestBody.get("schema"); + if(schemaMap != null && schemaMap.containsKey("$ref")){ + String schemaLink = schemaMap.get("$ref").toString(); + httpVerb.setSchemaLink(schemaLink); + int retCode = schemaLink.lastIndexOf('/'); + if(retCode != -1 && retCode != schemaLink.length()){ + httpVerb.setSchemaType(schemaLink.substring(retCode)); + } + } + } + } + } + + if(httpVerbValueMap.containsKey("responses")){ + + List<Api.HttpVerb.Response> responses = new ArrayList<Api.HttpVerb.Response>(); + + Map<String, Object> responsesMap = (Map<String, Object>) httpVerbValueMap.get("responses"); + + responsesMap + .entrySet() + .stream() + .filter((res) -> !"default".equalsIgnoreCase(res.getKey())) + .forEach((responseMap) -> { + + Map<String, Object> responseValueMap = (Map<String, Object>)responseMap.getValue(); + + Api.HttpVerb.Response response = new Api.HttpVerb.Response(); + + response.setResponseCode(responseMap.getKey()); + response.setDescription((String) responseValueMap.get("description")); + response.setVersion((String) responseValueMap.get("version")); + + if(responseValueMap != null && responseValueMap.containsKey("schema")){ + Map<String, Object> schemaMap = (Map<String, Object>)responseValueMap.get("schema"); + if(schemaMap != null && schemaMap.containsKey("$ref")){ + String schemaLink = schemaMap.get("$ref").toString(); + httpVerb.setHasReturnSchema(true); + //Filter out all the getDefinition links that appear in the YAML + httpVerb.setReturnSchemaLink(schemaLink.replace("getDefinitions", "definitions")); + int retCode = schemaLink.lastIndexOf('/'); + if(retCode != -1 && retCode != schemaLink.length()){ + httpVerb.setReturnSchemaObject(schemaLink.substring(retCode)); + } + } + } + + responses.add(response); + } + ); + + httpVerb.setResponses(responses); + } + + httpVerbs.add(httpVerb); + }); + + api.setHttpMethods(httpVerbs); + apis.add(api); + }); + + return apis; + } + + public static List<Definition> convertToDefinition(Map<String, Object> definitionMap) { + + if(definitionMap == null) + throw new IllegalArgumentException(); + + List<Definition> defintionsList = new ArrayList<>(); + + definitionMap + .entrySet() + .forEach((entry) -> { + + Definition definition = new Definition(); + String key = entry.getKey(); + Map<String, Object> valueMap = (Map<String, Object>) entry.getValue(); + + definition.setDefinitionName(key); + + if(valueMap.containsKey("description")){ + String description = valueMap.get("description").toString(); + description = formatDescription(description); + definition.setDefinitionDescription(description); + definition.setHasDescription(true); + } + + List<Definition.Property> definitionProperties = new ArrayList<>(); + + List<String> requiredProperties = (valueMap.get("required") == null) ? new ArrayList<>() : (List<String>) valueMap.get("required"); + + Set<String> requiredPropsSet = requiredProperties.stream().collect(Collectors.toSet()); + + valueMap + .entrySet() + .stream() + .filter( (e) -> "properties".equals(e.getKey())) + .forEach((propertyEntries) -> { + Map<String, Object> propertyRealEntries = (Map<String, Object>) propertyEntries.getValue(); + propertyRealEntries + .entrySet() + .forEach((propertyEntry) -> { + Definition.Property definitionProperty = new Definition.Property(); + String propertyKey = propertyEntry.getKey(); + if(requiredPropsSet.contains(propertyKey)){ + definitionProperty.setRequired(true); + } + definitionProperty.setPropertyName(propertyKey); + Map<String, Object> definitionPropertyMap = (Map<String, Object>) propertyEntry.getValue(); + + if(definitionPropertyMap.containsKey("description")){ + definitionProperty.setPropertyDescription(definitionPropertyMap.get("description").toString()); + definitionProperty.setHasPropertyDescription(true); + } + if(definitionPropertyMap.containsKey("type")){ + String type = definitionPropertyMap.get("type").toString(); + definitionProperty.setPropertyType(type); + definitionProperty.setHasType(true); + if ("array".equals(type)) { + definitionProperty.setPropertyType("object[]"); + if(!definitionPropertyMap.containsKey("items")){ + throw new RuntimeException("Unable to find the property items even though the type is array for " + propertyEntry.getKey()); + } else { + Map<String, Object> itemMap = (Map<String, Object>) definitionPropertyMap.get("items"); + if(itemMap.containsKey("$ref")){ + definitionProperty.setHasPropertyReference(true); + String refItem = itemMap.get("$ref").toString(); + int retCode = refItem.lastIndexOf('/'); + if(retCode != -1 && retCode != refItem.length()){ + definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1)); + } + definitionProperty.setPropertyReference(refItem); + } + } + } else { + if(definitionPropertyMap.containsKey("$ref")){ + definitionProperty.setHasPropertyReference(true); + String refItem = definitionPropertyMap.get("$ref").toString(); + int retCode = refItem.lastIndexOf('/'); + if(retCode != -1 && retCode != refItem.length()){ + definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1)); + } + definitionProperty.setPropertyReference(refItem); + } + } + } + definitionProperties.add(definitionProperty); + }); + }); + + definition.setPropertyList(definitionProperties); + + List<Definition.Property> schemaProperties = definitionProperties. + stream() + .filter((o) -> o.isHasPropertyReference()) + .collect(Collectors.toList()); + + List<Definition.Property> regularProperties = definitionProperties. + stream() + .filter((o) -> !o.isHasPropertyReference()) + .collect(Collectors.toList()); + + definition.setRegularPropertyList(regularProperties); + definition.setSchemaPropertyList(schemaProperties); + + defintionsList.add(definition); + }); + return defintionsList; + } + + public static String formatDescription(String description){ + + description = Arrays.stream(description.split("\n")) + .map((line) -> { + line = line.trim(); + if(line.contains("######")){ + line = line.replaceAll("#", ""); + line = line.trim(); + String headerId = line.toLowerCase().replaceAll("\\s", "-"); + + if(line.contains("Related Nodes")){ + return String.format("<h6 id=\"%s\">%s</h6>%s<ul>", headerId, line, LINE_SEPARATOR); + } else { + return String.format("<h6 id=\"%s\">%s</h6>", headerId, line); + } + } else if(line.startsWith("-")){ + line = line.replaceFirst("-", ""); + line = line.trim(); + return String.format("<li>%s</li>", line); + } else { + return String.format("<p>%s</p>", line); + } + }) + .collect(Collectors.joining(LINE_SEPARATOR)); + + if(description.contains("<ul>")){ + description = description + "</ul>"; + } + + return description; + } + +} + |