2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.util;
25 import org.onap.aai.introspection.Version;
26 import org.onap.aai.util.genxsd.EdgeRuleSet;
27 import org.onap.aai.util.genxsd.HTMLfromOXM;
28 import org.onap.aai.util.genxsd.YAMLfromOXM;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.io.BufferedWriter;
34 import java.io.IOException;
35 import java.nio.charset.Charset;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
41 public class GenerateXsd {
43 private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class");
44 protected static String apiVersion = null;
45 static String apiVersionFmt = null;
46 static boolean useAnnotationsInXsd = false;
47 static String responsesUrl = null;
48 static String responsesLabel = null;
49 static String jsonEdges = null;
50 static EdgeRuleSet edgeRuleSet = null;
52 static Map<String, String> generatedJavaType;
53 static Map<String, String> appliedPaths;
55 public static final int VALUE_NONE = 0;
56 public static final int VALUE_DESCRIPTION = 1;
57 public static final int VALUE_INDEXED_PROPS = 2;
58 public static final int VALUE_CONTAINER = 3;
60 private static final String generateTypeXSD = "xsd";
61 private static final String generateTypeYAML = "yaml";
63 private static final String root = "../aai-schema/src/main/resources";
64 private static final String autoGenRoot = "aai-schema/src/main/resources";
65 private static final String normalStartDir = "aai-core";
66 private static final String xsd_dir = root + "/aai_schema";
67 private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml";
69 /* These three strings are for yaml auto-generation from aai-common class*/
70 // private static final String alt_yaml_dir = autoGenRoot + "/aai_swagger_yaml";
72 private static int annotationsStartVersion = 9; // minimum version to support annotations in xsd
73 private static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation
75 private static boolean validVersion(String versionToGen) {
77 if ("ALL".equalsIgnoreCase(versionToGen)) {
81 for (Version v : Version.values()) {
82 if (v.name().equals(versionToGen)) {
90 private static boolean versionUsesAnnotations( String version) {
91 if (new Integer(version.substring(1)).intValue() >= annotationsStartVersion ) {
97 private static boolean versionSupportsSwagger( String version) {
98 if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) {
104 public static String getAPIVersion() {
108 public static String getYamlDir() {
112 public static String getResponsesUrl() {
116 public static void main(String[] args) throws IOException {
117 String versionToGen = System.getProperty("gen_version").toLowerCase();
118 String fileTypeToGen = System.getProperty("gen_type").toLowerCase();
119 if ( fileTypeToGen == null ) {
120 fileTypeToGen = generateTypeXSD;
123 if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) {
124 System.err.println("Invalid gen_type passed. " + fileTypeToGen);
129 String responsesLabel = System.getProperty("yamlresponses_url");
130 responsesUrl = responsesLabel;
132 List<Version> versionsToGen = new ArrayList<>();
133 if ( versionToGen == null ) {
134 System.err.println("Version is required, ie v<n> or ALL.");
137 else if (!"ALL".equalsIgnoreCase(versionToGen) && !versionToGen.matches("v\\d+") && !validVersion(versionToGen)) {
138 System.err.println("Invalid version passed. " + versionToGen);
141 else if ("ALL".equalsIgnoreCase(versionToGen)) {
142 versionsToGen = Arrays.asList(Version.values());
143 Collections.sort(versionsToGen);
144 Collections.reverse(versionsToGen);
146 versionsToGen.add(Version.getVersion(versionToGen));
149 //process file type System property
150 fileTypeToGen = (fileTypeToGen == null ? generateTypeXSD : fileTypeToGen.toLowerCase());
151 if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) {
152 System.err.println("Invalid gen_type passed. " + fileTypeToGen);
154 } else if ( fileTypeToGen.equals(generateTypeYAML) ) {
155 if ( responsesUrl == null || responsesUrl.length() < 1
156 || responsesLabel == null || responsesLabel.length() < 1 ) {
157 System.err.println("generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties" );
160 responsesUrl = "description: "+ "Response codes found in [response codes]("+responsesLabel+ ").\n";
164 if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) {
165 oxmPath = autoGenRoot + "/oxm/";
168 oxmPath = root + "/oxm/";
173 String fileContent = null;
175 for (Version v : versionsToGen) {
176 apiVersion = v.toString();
177 logger.debug("YAMLdir = "+yaml_dir);
178 logger.debug("Generating " + apiVersion + " " + fileTypeToGen);
179 File oxm_file = new File(oxmPath + "aai_oxm_" + apiVersion + ".xml");
180 apiVersionFmt = "." + apiVersion + ".";
181 generatedJavaType = new HashMap<String, String>();
182 appliedPaths = new HashMap<String, String>();
183 File edgeRuleFile = null;
184 logger.debug("user.dir = "+System.getProperty("user.dir"));
185 if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) {
186 edgeRuleFile = new File(normalStartDir + "/src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json");
189 edgeRuleFile = new File("src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json");
192 if ( fileTypeToGen.equals(generateTypeXSD) ) {
193 useAnnotationsInXsd = versionUsesAnnotations(apiVersion);
194 outfileName = xsd_dir + "/aai_schema_" + apiVersion + "." + generateTypeXSD;
196 HTMLfromOXM swagger = new HTMLfromOXM(oxm_file, v);
197 fileContent = swagger.process();
198 } catch(Exception e) {
199 logger.error( "Exception creating output file " + outfileName);
200 logger.error( e.getMessage());
203 } else if ( versionSupportsSwagger(apiVersion )) {
204 outfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + generateTypeYAML;
206 YAMLfromOXM swagger = new YAMLfromOXM(oxm_file, v, edgeRuleFile);
207 fileContent = swagger.process();
208 } catch(Exception e) {
209 logger.error( "Exception creating output file " + outfileName);
210 logger.error( e.getMessage());
216 outfile = new File(outfileName);
217 File parentDir = outfile.getParentFile();
218 if(! parentDir.exists())
222 outfile.createNewFile();
223 } catch (IOException e) {
224 logger.error( "Exception creating output file " + outfileName);
227 BufferedWriter bw = null;
229 Charset charset = Charset.forName("UTF-8");
230 Path path = Paths.get(outfileName);
231 bw = Files.newBufferedWriter(path, charset);
232 bw.write(fileContent);
233 } catch ( IOException e) {
234 logger.error( "Exception writing output file " + outfileName);
241 logger.debug( "GeneratedXSD successful, saved in " + outfileName);