[AAI-150 Amsterdam] Modify xsd generation
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / util / GenerateXsd.java
index 7cc6bcf..7c79cdb 100644 (file)
@@ -24,15 +24,24 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Scanner;
+import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -59,18 +68,26 @@ import org.w3c.dom.NodeList;
 import org.openecomp.aai.dbmodel.DbEdgeRules;
 import org.openecomp.aai.db.props.AAIProperties;
 import org.openecomp.aai.introspection.Version;
+import org.openecomp.aai.serialization.db.EdgeRule;
+import org.openecomp.aai.serialization.db.EdgeRules;
+
 import com.google.common.base.Joiner;
 import com.google.common.collect.Multimap;
+import com.jayway.jsonpath.Criteria;
+import com.jayway.jsonpath.DocumentContext;
+import com.jayway.jsonpath.JsonPath;
+
 
 
 public class GenerateXsd {
+       
        static String apiVersion = null;
        static String apiVersionFmt = null;
        static boolean useAnnotationsInXsd = false;
        static String responsesUrl = null;
        static String responsesLabel = null;
-       //static Map<String, String> generatedJavaType = new HashMap<String, String>();
-       //static Map<String, String> appliedPaths = new HashMap<String, String>();
+       static String jsonEdges = null;
+
        static Map<String, String> generatedJavaType;
        static Map<String, String> appliedPaths;
        static NodeList javaTypeNodes;
@@ -83,14 +100,14 @@ public class GenerateXsd {
        
        private static final String generateTypeXSD = "xsd";
        private static final String generateTypeYAML = "yaml";
-
+       
        private static final String root = "../aai-schema/src/main/resources";
        private static final String xsd_dir = root + "/aai_schema";
        private static final String yaml_dir = root + "/aai_swagger_yaml";
-
+       
        private static int annotationsStartVersion = 9; // minimum version to support annotations in xsd
        private static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation
-
+       
        private static XPath xpath = XPathFactory.newInstance().newXPath();
        
 
@@ -229,11 +246,11 @@ public class GenerateXsd {
        }
        
        private static boolean validVersion(String versionToGen) {
-
+               
                if ("ALL".equalsIgnoreCase(versionToGen)) {
                        return true;
                }
-
+               
                for (Version v : Version.values()) {
                if (v.name().equals(versionToGen)) {
                    return true;
@@ -242,49 +259,49 @@ public class GenerateXsd {
 
            return false;
        }
-
+       
        private static boolean versionUsesAnnotations( String version) {
                if (new Integer(version.substring(1)).intValue() >= annotationsStartVersion ) {
                        return true;
                }
                return false;
        }
-
+       
        private static boolean versionSupportsSwagger( String version) {
                if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) {
                        return true;
                }
                return false;
        }
-
+       
        public static void main(String[] args) throws IOException {
                String versionToGen = System.getProperty("gen_version").toLowerCase();
                String fileTypeToGen = System.getProperty("gen_type").toLowerCase();
                if ( fileTypeToGen == null ) {
                        fileTypeToGen = generateTypeXSD;
                }
-
+               
                if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) {
                        System.err.println("Invalid gen_type passed. " + fileTypeToGen);
                        System.exit(1);
                }
-
-
+               
+               
                if ( versionToGen == null ) {
                        System.err.println("Version is required, ie v<n> or ALL.");
-                       System.exit(1);
+                       System.exit(1);                 
                }
-
+               
                responsesUrl = System.getProperty("yamlresponses_url");
                String responsesLabel = System.getProperty("yamlresponses_label");
                List<Version> versionsToGen = new ArrayList<>();
 
-
+               
                if (!"ALL".equalsIgnoreCase(versionToGen) && !versionToGen.matches("v\\d+") && !validVersion(versionToGen)) {
                        System.err.println("Invalid version passed. " + versionToGen);
                        System.exit(1);
                }
-
+               
                if ("ALL".equalsIgnoreCase(versionToGen)) {
                        versionsToGen = Arrays.asList(Version.values());
                        Collections.sort(versionsToGen);
@@ -294,7 +311,7 @@ public class GenerateXsd {
                }
 
                if ( fileTypeToGen.equals(generateTypeYAML) ) {
-                       if ( responsesUrl == null || responsesUrl.length() < 1
+                       if ( responsesUrl == null || responsesUrl.length() < 1 
                                        || responsesLabel == null || responsesLabel.length() < 1 ) {
                                System.err.println("generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties" );
                                System.exit(1);
@@ -302,11 +319,11 @@ public class GenerateXsd {
                        responsesUrl = "description: "+ responsesLabel+ "(" + responsesUrl + ").\n";
                }
                String oxmPath = root + "/oxm/";
-
+       
                String outfileName;
                File outfile;
                String fileContent;
-
+               
                for (Version v : versionsToGen) {
                        apiVersion = v.toString();
                        System.out.println("Generating " + apiVersion + " " + fileTypeToGen);
@@ -326,28 +343,32 @@ public class GenerateXsd {
                        }
                        outfile = new File(outfileName);
                        File parentDir = outfile.getParentFile();
-                       if(! parentDir.exists())
+                       if(! parentDir.exists()) 
                              parentDir.mkdirs();
-
+               
                    try {
                        outfile.createNewFile();
                    } catch (IOException e) {
                        System.out.println( "Exception creating output file " + outfileName);
                        e.printStackTrace();
                    }
+                   BufferedWriter bw = null;
                try {
-                       FileWriter fw = new FileWriter(outfile.getAbsoluteFile());
-                       BufferedWriter bw = new BufferedWriter(fw);
+                       Charset charset = Charset.forName("UTF-8");
+                       Path path = Paths.get(outfileName);
+                       bw = Files.newBufferedWriter(path, charset);
                        bw.write(fileContent);
-                       bw.close();
-
                } catch ( IOException e) {
                        System.out.println( "Exception writing output file " + outfileName);
                        e.printStackTrace();
+               } finally {
+                       if ( bw != null ) {
+                               bw.close();
+                       }
                }
                        System.out.println( "GeneratedXSD successful, saved in " + outfileName);
                }
-
+               
        }
 
 
@@ -688,7 +709,7 @@ public class GenerateXsd {
                sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
                String namespace = "org.openecomp";
                if ( useAnnotationsInXsd ) {
-                       sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/"
+                       sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" 
                                + apiVersion + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + apiVersion + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
                                                + "\n"
                                                + "xmlns:jaxb=\"http://java.sun.com/xml/ns/jaxb\"\r\n" + 
@@ -696,7 +717,7 @@ public class GenerateXsd {
                                                "    xmlns:annox=\"http://annox.dev.java.net\" \r\n" + 
                                                "    jaxb:extensionBindingPrefixes=\"annox\">\n\n");
                } else {
-                       sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/"
+                       sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" 
                                        + apiVersion + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + apiVersion + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n\n");
                }
 
@@ -806,73 +827,81 @@ public class GenerateXsd {
                        result = Class.forName("org.openecomp.aai.dbmodel.DbEdgeRules");
                }
                return result;
-       }
-       
+       }       
+
        /**
         * Guaranteed to at least return non null but empty collection of edge descriptions
         * @param nodeName name of the vertex whose edge relationships to return
         * @return collection of node neighbors based on DbEdgeRules
-        */
-       private static Collection<EdgeDescription> getEdgeRules( String nodeName ) 
+       **/
+       private static Collection<EdgeDescription> getEdgeRulesFromJson( String path, boolean skipMatch ) 
        {
 
                ArrayList<EdgeDescription> result = new ArrayList<>();
-               Iterator<String> edgeRulesIterator;
-
+               Iterator<Map<String, Object>> edgeRulesIterator;
                try {
 
-                       Field mapfield = versionedClass.getField("EdgeRules");
-                       Object map = mapfield.get(null);
-                       if (map instanceof Multimap<?,?>) {
-                               edgeRulesIterator = ((Multimap<String,String>) map).keySet().iterator();
-                       } else {
-                               throw new NoSuchFieldException ("Didn't get back the multimap field expected");
-                       }
                        GenerateXsd x = new GenerateXsd();
-
+                       
+                       List<Map<String, Object>> inEdges = JsonPath.parse(jsonEdges).read(path);
+                       
+                       edgeRulesIterator = inEdges.iterator();
+                       Map<String, Object> edgeMap;
+                       String fromNode;
+                       String toNode;
+                       String ruleKey;
+                       String direction;
+                       String multiplicity;
+                       String isParent;
+                       String hasDelTarget;
+                       EdgeDescription edgeDes;
+                       
                        while( edgeRulesIterator.hasNext() ){
-                               String ruleKey = edgeRulesIterator.next();
-                               if ( ruleKey.startsWith(nodeName + "|" ) ||
-                                               ruleKey.endsWith("|" + nodeName)) {
-                                       Collection <String> edRuleColl = DbEdgeRules.EdgeRules.get(ruleKey);
-                                       Iterator <String> ruleItr = edRuleColl.iterator();
-                                       while( ruleItr.hasNext() ){             
-                                               EdgeDescription edgeDes = x.new EdgeDescription();
-                                               edgeDes.setRuleKey(ruleKey);
-                                               String fullRuleString = ruleItr.next();
-                                               String[] toks = fullRuleString.split(",");
-                                               if (toks != null) {
-                                                       if (toks.length > 1) {
-                                                               edgeDes.setDirection(toks[1]);
-                                                       }
-                                                       if (toks.length > 2) {
-                                                               edgeDes.setMultiplicity(toks[2]);
-                                                       }
-                                                       if (toks.length > 3) {
-                                                               if (toks[3].equals("true")) 
-                                                                       edgeDes.setType(LineageType.PARENT);
-                                                               else if (toks[3].equals("parent")) 
-                                                                       edgeDes.setType(LineageType.PARENT);
-                                                               else if (toks[3].equals("child")) 
-                                                                       edgeDes.setType(LineageType.CHILD);
-                                                               else 
-                                                                       edgeDes.setType(LineageType.UNRELATED);
-                                                       }
-                                                       if (toks.length > 5) {
-                                                               edgeDes.setHasDelTarget(toks[5]);;
-                                                       }
-                                               }
-
-                                               //System.out.println( "nodeName " + nodeName + " ruleKey "  + ruleKey + " ruleString " + fullRuleString);
-                                               //result.add(ruleKey + "-" + fullRuleString);
-                                               result.add(edgeDes);
+                               edgeMap = edgeRulesIterator.next();
+                               fromNode = (String)edgeMap.get("from");
+                               toNode = (String)edgeMap.get("to");
+                               if ( skipMatch ) { 
+                                       if ( fromNode.equals(toNode)) {
+                                               continue;
                                        }
                                }
+                               edgeDes = x.new EdgeDescription();
+                               edgeDes.setRuleKey(fromNode + "|" + toNode);
+                               direction = (String)edgeMap.get("direction");
+                               edgeDes.setDirection(direction);
+                               multiplicity = (String)edgeMap.get("multiplicity");
+                               edgeDes.setMultiplicity(multiplicity);
+                               isParent = (String)edgeMap.get("isParent");
+                               if ( isParent != null && isParent.equals("true"))  {
+                                       edgeDes.setType(LineageType.PARENT);
+                               } else {
+                                       edgeDes.setType(LineageType.UNRELATED);
+                               }
+                               hasDelTarget = (String)edgeMap.get("hasDelTarget");
+                               edgeDes.setHasDelTarget(hasDelTarget);
+                               result.add(edgeDes);
+                               
                        }
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                return result;
+               
+       }
+       
+       /**
+        * Guaranteed to at least return non null but empty collection of edge descriptions
+        * @param nodeName name of the vertex whose edge relationships to return
+        * @return collection of node neighbors based on DbEdgeRules
+       **/
+       private static Collection<EdgeDescription> getEdgeRules( String nodeName ) 
+       {               
+               String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]";
+               String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]";
+               Collection<EdgeDescription> fromEdges = getEdgeRulesFromJson( fromRulesPath, false );
+               Collection<EdgeDescription> edges = getEdgeRulesFromJson( toRulesPath, true );
+               edges.addAll(fromEdges);
+               return edges;
        }
        
        /**
@@ -927,6 +956,7 @@ public class GenerateXsd {
                        case "Network":
                        case "ServiceDesignAndCreation":
                        case "Business":
+                       case "LicenseManagement":
                        case "CloudInfrastructure":
                                break;
                        default:
@@ -1639,6 +1669,8 @@ public class GenerateXsd {
                sb.append("swagger: \"2.0\"\ninfo:\n  description: |\n    Copyright &copy; 2017 AT&amp;T Intellectual Property. All rights reserved.\n\n    Licensed under the Creative Commons License, Attribution 4.0 Intl. (the &quot;License&quot;); 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 &quot;AS IS&quot; 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    ECOMP and OpenECOMP are trademarks and service marks of AT&amp;T Intellectual Property.\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: \"" + apiVersion +"\"\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: /aai/" + apiVersion + "\n");
                sb.append("schemes:\n  - https\npaths:\n");
                /*
                sb.append("responses:\n");
@@ -1650,9 +1682,15 @@ public class GenerateXsd {
                sb.append("    description: bad request\n");
                */
                try {
-                       
                        versionedClass = getEdgeRulesClass();
-                   
+                   File initialFile = new File("src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json");
+                   InputStream is = new FileInputStream(initialFile);
+
+                       Scanner scanner = new Scanner(is);
+                       jsonEdges = scanner.useDelimiter("\\Z").next();
+                       scanner.close();
+                       is.close();
+                       
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                    dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();