diff options
Diffstat (limited to 'aai-schema-gen/src/main')
3 files changed, 1674 insertions, 1444 deletions
diff --git a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java index b9efa2f..e28c127 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/NodesYAMLfromOXM.java @@ -8,7 +8,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -46,482 +46,573 @@ 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(); - } -} + 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:" + LINE_SEPARATOR + " "); + 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( + DOUBLE_LINE_SEPARATOR + " 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." + DOUBLE_LINE_SEPARATOR + " You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)" + DOUBLE_LINE_SEPARATOR + " 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." + LINE_SEPARATOR + " version: \"" + + v.toString() + "\"" + LINE_SEPARATOR ); + sb.append(" title: Active and Available Inventory REST API" + LINE_SEPARATOR); + sb.append( + " license:" + LINE_SEPARATOR + " name: Apache 2.0" + LINE_SEPARATOR + " url: http://www.apache.org/licenses/LICENSE-2.0.html" + LINE_SEPARATOR); + sb.append(" contact:" + LINE_SEPARATOR + " name:" + LINE_SEPARATOR + " url:" + LINE_SEPARATOR + " email:" + LINE_SEPARATOR); + sb.append("host:" + LINE_SEPARATOR + "basePath: " + basePath + "/" + v.toString() + LINE_SEPARATOR); + sb.append("schemes:" + LINE_SEPARATOR + " - https\npaths:" + LINE_SEPARATOR); + 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"); + boolean processInventory = false; + if (!"Inventory".equals(javaTypeName)) { + if (generatedJavaType.containsKey(javaTypeName)) { + continue; + } + // will combine all matching java-types + elem = getJavaTypeElementSwagger(javaTypeName); + } else { + processInventory = true; + } + + XSDElement javaTypeElement = new XSDElement(elem); + + if (processInventory) { + getTopLevelPaths(javaTypeElement); + } + + 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) { + // refactored to support top level paths from the schema file, set the ignore + // parameter to false allows the logic to match all top level paths, including + // Search and Actions, as hard-coded prior to refactoring + if (checkTopLevel(tag, false)) { + 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 index dfa702b..39eb9d9 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/OxmFileProcessor.java @@ -8,7 +8,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -40,442 +40,494 @@ 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; + 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"); + 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 + static List<String> nodeFilter = createNodeFilter(); + 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 Map combinedJavaTypes; + protected String apiVersionFmt = null; + protected List<String> topLevelPaths = new ArrayList<String>(); + 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(); + EdgeIngestor ei; + NodeIngestor ni; + + public OxmFileProcessor(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei) { + this.schemaVersions = schemaVersions; + this.ni = ni; + this.ei = ei; } - 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; - } + private static List<String> createNodeFilter() { + List<String> list = Arrays.asList("search", "actions", "aai-internal", "nodes"); + return list; + } + private Map<String, String> createJavaTypeDefinitions() { + StringBuffer aaiInternal = new StringBuffer(); + StringBuffer nodes = new StringBuffer(); + Map<String, String> javaTypeDefinitions = new HashMap<String, String>(); + // update to use platform portable line separator + aaiInternal.append(" aai-internal:" + LINE_SEPARATOR); + aaiInternal.append(" properties:" + LINE_SEPARATOR); + aaiInternal.append(" property-name:" + LINE_SEPARATOR); + aaiInternal.append(" type: string" + LINE_SEPARATOR); + aaiInternal.append(" property-value:" + LINE_SEPARATOR); + aaiInternal.append(" type: string" + LINE_SEPARATOR); + // javaTypeDefinitions.put("aai-internal", aaiInternal.toString()); + nodes.append(" nodes:" + LINE_SEPARATOR); + nodes.append(" properties:" + LINE_SEPARATOR); + nodes.append(" inventory-item-data:" + LINE_SEPARATOR); + nodes.append(" type: array" + LINE_SEPARATOR); + nodes.append(" items:" + LINE_SEPARATOR); + nodes.append(" $ref: \"#/definitions/inventory-item-data\"" + LINE_SEPARATOR); + javaTypeDefinitions.put("nodes", nodes.toString()); + return javaTypeDefinitions; + } - public void setOxmVersion(File oxmFile, SchemaVersion v) { - this.oxmFile = oxmFile; - this.v = v; - } + 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 setXmlVersion(String xml, SchemaVersion v) { + this.xml = xml; + this.v = v; + } - public void setVersion(SchemaVersion v) { - this.oxmFile = null; - this.v = v; - } + public void setVersion(SchemaVersion v) { + this.oxmFile = null; + this.v = v; + } - public void setNodeIngestor(NodeIngestor ni) { - this.ni = ni; - } + public void setNodeIngestor(NodeIngestor ni) { + this.ni = ni; + } public void setEdgeIngestor(EdgeIngestor ei) { - this.ei = 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 ) { + return schemaVersions; + } + + public void setSchemaVersions(SchemaVersions schemaVersions) { + this.schemaVersions = schemaVersions; + } + + protected void getTopLevelPaths(XSDElement elem) { + NodeList parentNodes; + Element parentElement; + NodeList xmlElementNodes; + + parentNodes = elem.getElementsByTagName("java-attributes"); + if (parentNodes.getLength() == 0) { + return; + } + parentElement = (Element) parentNodes.item(0); + xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + if (xmlElementNodes.getLength() <= 0) { + return; + } + + XSDElement xmlElementElement; + + for (int i = 0; i < xmlElementNodes.getLength(); ++i) { + xmlElementElement = new XSDElement((Element) xmlElementNodes.item(i)); + if (!xmlElementElement.getParentNode().isSameNode(parentElement)) { + continue; + } + String topLevel = xmlElementElement.getAttribute("type"); + topLevel = topLevel.substring(topLevel.lastIndexOf('.') + 1); + if (!topLevelPaths.contains(topLevel)) { + if ("Nodes".equals(topLevel) || "AaiInternal".equals(topLevel)) { + continue; + } + topLevelPaths.add(topLevel); + } + } + } + + protected boolean checkTopLevel(String topLevel, boolean ignoreActionsSearch) { + // when ignoreActionsSearch is set to true, with a topLevel that matches one of the values + // to ignore, the logic will handle those values, as if they are not at the top level. + // this was done when refactoring checks that may or may not include these top levels. + // Using this API allows new top levels to be added to the schema file and + // included in the generated yaml without changing this generation logic. + if (ignoreActionsSearch) { + if ("Actions".equals(topLevel) || "Search".equals(topLevel)) { + return false; + } + } + if (topLevelPaths.contains(topLevel)) { + return true; + } + return false; + } + + 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); - } + 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/YAMLfromOXM.java b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java index 6e2af57..45e2ea3 100644 --- a/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java +++ b/aai-schema-gen/src/main/java/org/onap/aai/schemagen/genxsd/YAMLfromOXM.java @@ -8,7 +8,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -45,546 +45,633 @@ 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; - } + 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:" + LINE_SEPARATOR + " "); + 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( + DOUBLE_LINE_SEPARATOR + " Copyright © 2017-18 AT&T Intellectual Property. All rights reserved." + DOUBLE_LINE_SEPARATOR + " 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." + DOUBLE_LINE_SEPARATOR + " 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." + LINE_SEPARATOR + " version: \"" + + v.toString() + "\"" + LINE_SEPARATOR ); + sb.append(" title: Active and Available Inventory REST API" + LINE_SEPARATOR); + sb.append( + " license:" + LINE_SEPARATOR + " name: Apache 2.0\n url: http://www.apache.org/licenses/LICENSE-2.0.html" + LINE_SEPARATOR); + sb.append(" contact:" + LINE_SEPARATOR + " name:" + LINE_SEPARATOR + " url:" + LINE_SEPARATOR + " email:" + LINE_SEPARATOR); + sb.append("host:" + LINE_SEPARATOR + "basePath: " + basePath + "/" + v.toString() + LINE_SEPARATOR); + sb.append("schemes:" + LINE_SEPARATOR + " - https\npaths:" + LINE_SEPARATOR); + 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"); + boolean processInventory = false; + if (!"Inventory".equals(javaTypeName)) { + if (generatedJavaType.containsKey(getXmlRootElementName(javaTypeName))) { + continue; + } + // will combine all matching java-types + elem = getJavaTypeElementSwagger(javaTypeName); + } else { + processInventory = true; + } + + XSDElement javaTypeElement = new XSDElement(elem); + + if (processInventory) { + getTopLevelPaths(javaTypeElement); + } + + 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) { + // set ignore to true to skip Actions and Search + boolean topCheck = checkTopLevel(tag, true); + if (!topCheck) { + 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) { + // set ignore to true to skip Actions and Search + boolean topCheck = checkTopLevel(tag, true); + if (topCheck) { + return true; + } + } + return false; + } } |