Update aai-schema-ingest dependency in the schema-service
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / GenerateXsd.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.schemagen;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
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;
35 import java.util.Map;
36
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;
47
48 public class GenerateXsd {
49
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");
61
62     static NodeList javaTypeNodes;
63     static Map<String, String> javaTypeDefinitions = createJavaTypeDefinitions();
64
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;
76     }
77
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;
82
83     private static final String GENERATE_TYPE_XSD = "xsd";
84     private static final String GENERATE_TYPE_YAML = "yaml";
85
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/";
90
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";
93
94     private static final String NORMAL_START_DIR = "aai-schema-gen";
95     private static final String XSD_DIR = ROOT + "/" + RELEASE + "/aai_schema";
96
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";
100
101     /* These three strings are for yaml auto-generation from aai-common class */
102
103     private static final int SWAGGER_SUPPORT_STARTS_VERSION = 1; // minimum version to support
104                                                                  // swagger documentation
105
106     private static boolean validVersion(String versionToGen) {
107
108         if ("ALL".equalsIgnoreCase(versionToGen)) {
109             return true;
110         }
111
112         SchemaConfigVersions schemaConfigVersions =
113             SpringContextAware.getBean(SchemaConfigVersions.class);
114         if (schemaConfigVersions == null) {
115             return false;
116         }
117         for (SchemaVersion v : schemaConfigVersions.getVersions()) {
118             if (v.toString().equals(versionToGen)) {
119                 return true;
120             }
121         }
122
123         return false;
124     }
125
126     private static boolean versionSupportsSwagger(String version) {
127         return Integer.parseInt(version.substring(1)) >= SWAGGER_SUPPORT_STARTS_VERSION;
128     }
129
130     public static String getAPIVersion() {
131         return apiVersion;
132     }
133
134     public static String getYamlDir() {
135         return YAML_DIR;
136     }
137
138     public static String getResponsesUrl() {
139         return responsesUrl;
140     }
141
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.");
146             System.exit(1);
147         } else {
148             versionToGen = versionToGen.toLowerCase();
149         }
150
151         String fileTypeToGen = System.getProperty("gen_type");
152         if (fileTypeToGen == null) {
153             fileTypeToGen = GENERATE_TYPE_XSD;
154         } else {
155             fileTypeToGen = fileTypeToGen.toLowerCase();
156         }
157
158         try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
159             "org.onap.aai.setup", "org.onap.aai.schemagen")) {
160             SchemaConfigVersions schemaConfigVersions = ctx.getBean(SchemaConfigVersions.class);
161
162             if (!fileTypeToGen.equals(GENERATE_TYPE_XSD)
163                 && !fileTypeToGen.equals(GENERATE_TYPE_YAML)) {
164                 System.err.println("Invalid gen_type passed. " + fileTypeToGen);
165                 System.exit(1);
166             }
167
168             String responsesLabel = System.getProperty("yamlresponses_url");
169             responsesUrl = responsesLabel;
170
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);
175                 System.exit(1);
176             } else if ("ALL".equalsIgnoreCase(versionToGen)) {
177                 versionsToGen = schemaConfigVersions.getVersions();
178                 Collections.sort(versionsToGen);
179                 Collections.reverse(versionsToGen);
180             } else {
181                 versionsToGen.add(new SchemaVersion(versionToGen));
182             }
183
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) {
188                     System.err.println(
189                         "generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties");
190                     System.exit(1);
191                 } else {
192                     responsesUrl = "description: " + "Response codes found in [response codes]("
193                         + responsesLabel + ").\n";
194                 }
195             }
196             /*
197              * TODO: Oxm Path is config driven
198              */
199             String oxmPath;
200             if (System.getProperty("user.dir") != null
201                 && !System.getProperty("user.dir").contains(NORMAL_START_DIR)) {
202                 oxmPath = BASE_AUTO_GEN_ROOT + NODE_DIR;
203             } else {
204                 oxmPath = BASE_ROOT + NODE_DIR;
205             }
206
207             String outfileName = null;
208             File outfile;
209             String nodesfileName = null;
210             File nodesfile;
211             String fileContent = null;
212             String nodesContent = null;
213
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;
227
228                 } else {
229                     fileName = BASE_ROOT + fileName;
230
231                 }
232                 edgeRuleFile = new File(fileName);
233                 // Document doc = ni.getSchema(translateVersion(v));
234
235                 if (fileTypeToGen.equals(GENERATE_TYPE_XSD)) {
236                     outfileName = XSD_DIR + "/aai_schema_" + apiVersion + "." + GENERATE_TYPE_XSD;
237                     try {
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);
243                         }
244                     } catch (Exception e) {
245                         logger.error("Exception creating output file " + outfileName);
246                         logger.error(e.getMessage());
247                         System.exit(-1);
248                     }
249                 } else if (versionSupportsSwagger(apiVersion)) {
250                     outfileName =
251                         YAML_DIR + "/aai_swagger_" + apiVersion + "." + GENERATE_TYPE_YAML;
252                     nodesfileName = YAML_DIR + "/aai_swagger_" + apiVersion + "." + "nodes" + "."
253                         + GENERATE_TYPE_YAML;
254                     try {
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);
265                     }
266                 } else {
267                     continue;
268                 }
269                 outfile = new File(outfileName);
270                 File parentDir = outfile.getParentFile();
271                 if (!parentDir.exists()) {
272                     parentDir.mkdirs();
273                 }
274                 if (nodesfileName != null) {
275                     BufferedWriter nodesBW = null;
276                     nodesfile = new File(nodesfileName);
277                     parentDir = nodesfile.getParentFile();
278                     if (!parentDir.exists()) {
279                         parentDir.mkdirs();
280                     }
281                     try {
282                         if (!nodesfile.createNewFile()) {
283                             logger.error("File {} already exist", nodesfileName);
284                         }
285                     } catch (IOException e) {
286                         logger.error("Exception creating output file " + nodesfileName, e);
287                     }
288                     try {
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);
295                     } finally {
296                         if (nodesBW != null) {
297                             nodesBW.close();
298                         }
299                     }
300                 }
301
302                 try {
303                     if (!outfile.createNewFile()) {
304                         logger.error("File {} already exist", outfileName);
305                     }
306                 } catch (IOException e) {
307                     logger.error("Exception creating output file " + outfileName, e);
308                 }
309                 BufferedWriter bw = null;
310                 try {
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);
317                 } finally {
318                     if (bw != null) {
319                         bw.close();
320                     }
321                 }
322                 logger.debug("GeneratedXSD successful, saved in " + outfileName);
323             }
324         } catch (BeansException e) {
325             logger.warn("Unable to initialize AnnotationConfigApplicationContext ", e);
326         }
327
328     }
329
330 }