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;
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;
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();
}
private static boolean validVersion(String versionToGen) {
-
+
if ("ALL".equalsIgnoreCase(versionToGen)) {
return true;
}
-
+
for (Version v : Version.values()) {
if (v.name().equals(versionToGen)) {
return true;
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);
}
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);
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);
}
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);
}
-
+
}
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" +
" 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");
}
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;
}
/**
case "Network":
case "ServiceDesignAndCreation":
case "Business":
+ case "LicenseManagement":
case "CloudInfrastructure":
break;
default:
sb.append("swagger: \"2.0\"\ninfo:\n description: |\n Copyright © 2017 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 ECOMP and OpenECOMP are trademarks and service marks of AT&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");
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();