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=========================================================
21 package org.onap.aai.schemagen;
23 import java.io.BufferedWriter;
25 import java.io.IOException;
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
37 import org.onap.aai.schemagen.genxsd.HTMLfromOXM;
38 import org.onap.aai.schemagen.genxsd.NodesYAMLfromOXM;
39 import org.onap.aai.schemagen.genxsd.YAMLfromOXM;
40 import org.onap.aai.setup.SchemaConfigVersions;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.BeansException;
45 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
46 import org.w3c.dom.NodeList;
48 public class GenerateXsd {
50 private static final Logger logger = LoggerFactory.getLogger(GenerateXsd.class);
51 protected static String apiVersion = null;
52 public static AnnotationConfigApplicationContext ctx = null;
53 static String apiVersionFmt = null;
54 static boolean useAnnotationsInXsd = false;
55 static String responsesUrl = null;
56 static String responsesLabel = null;
57 static String jsonEdges = null;
58 static Map<String, String> generatedJavaType;
59 static Map<String, String> appliedPaths;
60 static String RELEASE = System.getProperty("aai.release", "onap");
62 static NodeList javaTypeNodes;
63 static Map<String, String> javaTypeDefinitions = createJavaTypeDefinitions();
65 private static Map<String, String> createJavaTypeDefinitions() {
66 StringBuilder aaiInternal = new StringBuilder();
67 Map<String, String> javaTypeDefinitions = new HashMap<String, String>();
68 aaiInternal.append(" aai-internal:\n");
69 aaiInternal.append(" properties:\n");
70 aaiInternal.append(" property-name:\n");
71 aaiInternal.append(" type: string\n");
72 aaiInternal.append(" property-value:\n");
73 aaiInternal.append(" type: string\n");
74 // javaTypeDefinitions.put("aai-internal", aaiInternal.toString());
75 return javaTypeDefinitions;
78 public static final int VALUE_NONE = 0;
79 public static final int VALUE_DESCRIPTION = 1;
80 public static final int VALUE_INDEXED_PROPS = 2;
81 public static final int VALUE_CONTAINER = 3;
83 private static final String GENERATE_TYPE_XSD = "xsd";
84 private static final String GENERATE_TYPE_YAML = "yaml";
86 private final static String NODE_DIR = System.getProperty("nodes.configuration.location");
87 private final static String EDGE_DIR = System.getProperty("edges.configuration.location");
88 private static final String BASE_ROOT = "aai-schema/";
89 private static final String BASE_AUTO_GEN_ROOT = "aai-schema/";
91 private static final String ROOT = BASE_ROOT + "src/main/resources";
92 private static final String AUTO_GEN_ROOT = BASE_AUTO_GEN_ROOT + "src/main/resources";
94 private static final String NORMAL_START_DIR = "aai-schema-gen";
95 private static final String XSD_DIR = ROOT + "/" + RELEASE + "/aai_schema";
97 private static final String YAML_DIR = (((System.getProperty("user.dir") != null)
98 && (!System.getProperty("user.dir").contains(NORMAL_START_DIR))) ? AUTO_GEN_ROOT : ROOT)
99 + "/" + RELEASE + "/aai_swagger_yaml";
101 /* These three strings are for yaml auto-generation from aai-common class */
103 private static final int SWAGGER_SUPPORT_STARTS_VERSION = 1; // minimum version to support
104 // swagger documentation
106 private static boolean validVersion(String versionToGen) {
108 if ("ALL".equalsIgnoreCase(versionToGen)) {
112 SchemaConfigVersions schemaConfigVersions =
113 SpringContextAware.getBean(SchemaConfigVersions.class);
114 if (schemaConfigVersions == null) {
117 for (SchemaVersion v : schemaConfigVersions.getVersions()) {
118 if (v.toString().equals(versionToGen)) {
126 private static boolean versionSupportsSwagger(String version) {
127 return Integer.parseInt(version.substring(1)) >= SWAGGER_SUPPORT_STARTS_VERSION;
130 public static String getAPIVersion() {
134 public static String getYamlDir() {
138 public static String getResponsesUrl() {
142 public static void main(String[] args) throws IOException {
143 String versionToGen = System.getProperty("gen_version");
144 if (versionToGen == null) {
145 System.err.println("Version is required, ie v<n> or ALL.");
148 versionToGen = versionToGen.toLowerCase();
151 String fileTypeToGen = System.getProperty("gen_type");
152 if (fileTypeToGen == null) {
153 fileTypeToGen = GENERATE_TYPE_XSD;
155 fileTypeToGen = fileTypeToGen.toLowerCase();
158 try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
159 "org.onap.aai.setup", "org.onap.aai.schemagen")) {
160 SchemaConfigVersions schemaConfigVersions = ctx.getBean(SchemaConfigVersions.class);
162 if (!fileTypeToGen.equals(GENERATE_TYPE_XSD)
163 && !fileTypeToGen.equals(GENERATE_TYPE_YAML)) {
164 System.err.println("Invalid gen_type passed. " + fileTypeToGen);
168 String responsesLabel = System.getProperty("yamlresponses_url");
169 responsesUrl = responsesLabel;
171 List<SchemaVersion> versionsToGen = new ArrayList<>();
172 if (!"ALL".equalsIgnoreCase(versionToGen) && versionToGen != null
173 && !versionToGen.matches("v\\d+") && !validVersion(versionToGen)) {
174 System.err.println("Invalid version passed. " + versionToGen);
176 } else if ("ALL".equalsIgnoreCase(versionToGen)) {
177 versionsToGen = schemaConfigVersions.getVersions();
178 Collections.sort(versionsToGen);
179 Collections.reverse(versionsToGen);
181 versionsToGen.add(new SchemaVersion(versionToGen));
184 // process file type System property
185 if (fileTypeToGen.equals(GENERATE_TYPE_YAML)) {
186 if (responsesUrl == null || responsesUrl.length() < 1 || responsesLabel == null
187 || responsesLabel.length() < 1) {
189 "generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties");
193 "description: " + "Response codes are uniform across all endpoints.\n";
197 * TODO: Oxm Path is config driven
200 if (System.getProperty("user.dir") != null
201 && !System.getProperty("user.dir").contains(NORMAL_START_DIR)) {
202 oxmPath = BASE_AUTO_GEN_ROOT + NODE_DIR;
204 oxmPath = BASE_ROOT + NODE_DIR;
207 String outfileName = null;
209 String nodesfileName = null;
211 String fileContent = null;
212 String nodesContent = null;
214 for (SchemaVersion v : versionsToGen) {
215 apiVersion = v.toString();
216 logger.debug("YAMLdir = " + YAML_DIR);
217 logger.debug("Generating " + apiVersion + " " + fileTypeToGen);
218 apiVersionFmt = "." + apiVersion + ".";
219 generatedJavaType = new HashMap<String, String>();
220 appliedPaths = new HashMap<String, String>();
221 File edgeRuleFile = null;
222 String fileName = EDGE_DIR + "DbEdgeRules_" + apiVersion + ".json";
223 logger.debug("user.dir = " + System.getProperty("user.dir"));
224 if (System.getProperty("user.dir") != null
225 && !System.getProperty("user.dir").contains(NORMAL_START_DIR)) {
226 fileName = BASE_AUTO_GEN_ROOT + fileName;
229 fileName = BASE_ROOT + fileName;
232 edgeRuleFile = new File(fileName);
233 // Document doc = ni.getSchema(translateVersion(v));
235 if (fileTypeToGen.equals(GENERATE_TYPE_XSD)) {
236 outfileName = XSD_DIR + "/aai_schema_" + apiVersion + "." + GENERATE_TYPE_XSD;
238 HTMLfromOXM swagger = ctx.getBean(HTMLfromOXM.class);
239 swagger.setVersion(v);
240 fileContent = swagger.process();
241 if (fileContent.startsWith("Schema format issue")) {
242 throw new Exception(fileContent);
244 } catch (Exception e) {
245 logger.error("Exception creating output file " + outfileName);
246 logger.error(e.getMessage());
249 } else if (versionSupportsSwagger(apiVersion)) {
251 YAML_DIR + "/aai_swagger_" + apiVersion + "." + GENERATE_TYPE_YAML;
252 nodesfileName = YAML_DIR + "/aai_swagger_" + apiVersion + "." + "nodes" + "."
253 + GENERATE_TYPE_YAML;
255 YAMLfromOXM swagger = (YAMLfromOXM) ctx.getBean(YAMLfromOXM.class);
256 swagger.setVersion(v);
257 fileContent = swagger.process();
258 Map<String, Integer> combinedJavaTypes = swagger.getCombinedJavaTypes();
259 NodesYAMLfromOXM nodesSwagger = ctx.getBean(NodesYAMLfromOXM.class);
260 nodesSwagger.setVersion(v);
261 nodesSwagger.setCombinedJavaTypes(combinedJavaTypes);
262 nodesContent = nodesSwagger.process();
263 } catch (Exception e) {
264 logger.error("Exception creating output file " + outfileName, e);
269 outfile = new File(outfileName);
270 File parentDir = outfile.getParentFile();
271 if (!parentDir.exists()) {
274 if (nodesfileName != null) {
275 BufferedWriter nodesBW = null;
276 nodesfile = new File(nodesfileName);
277 parentDir = nodesfile.getParentFile();
278 if (!parentDir.exists()) {
282 if (!nodesfile.createNewFile()) {
283 logger.error("File {} already exist", nodesfileName);
285 } catch (IOException e) {
286 logger.error("Exception creating output file " + nodesfileName, e);
289 Charset charset = StandardCharsets.UTF_8;
290 Path path = Paths.get(nodesfileName);
291 nodesBW = Files.newBufferedWriter(path, charset);
292 nodesBW.write(nodesContent);
293 } catch (IOException e) {
294 logger.error("Exception writing output file " + outfileName, e);
296 if (nodesBW != null) {
303 if (!outfile.createNewFile()) {
304 logger.error("File {} already exist", outfileName);
306 } catch (IOException e) {
307 logger.error("Exception creating output file " + outfileName, e);
309 BufferedWriter bw = null;
311 Charset charset = StandardCharsets.UTF_8;
312 Path path = Paths.get(outfileName);
313 bw = Files.newBufferedWriter(path, charset);
314 bw.write(fileContent);
315 } catch (IOException e) {
316 logger.error("Exception writing output file " + outfileName, e);
322 logger.debug("GeneratedXSD successful, saved in " + outfileName);
324 } catch (BeansException e) {
325 logger.warn("Unable to initialize AnnotationConfigApplicationContext ", e);