Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / AutoGenerateHtml.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.schemagen;
24
25 import freemarker.template.TemplateException;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.ListIterator;
32
33 import org.onap.aai.schemagen.swagger.GenerateSwagger;
34 import org.onap.aai.setup.SchemaVersion;
35 import org.onap.aai.setup.SchemaVersions;
36 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
37
38 public class AutoGenerateHtml {
39
40     private static final String AAI_GENERATE_VERSION = "aai.generate.version";
41     public static final String DEFAULT_SCHEMA_DIR = "../aai-schema";
42     // if the program is run from aai-common, use this directory as default"
43     public static final String ALT_SCHEMA_DIR = "aai-schema";
44     // used to check to see if program is run from aai-schema-gen
45     public static final String DEFAULT_RUN_DIR = "aai-schema-gen";
46
47     public static void main(String[] args) throws IOException, TemplateException {
48         String savedProperty = System.getProperty(AAI_GENERATE_VERSION);
49
50         AnnotationConfigApplicationContext ctx =
51             new AnnotationConfigApplicationContext("org.onap.aai.setup", "org.onap.aai.schemagen");
52
53         SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);
54
55         List<SchemaVersion> versionsToGen = schemaVersions.getVersions();
56         Collections.sort(versionsToGen);
57         Collections.reverse(versionsToGen);
58         ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator();
59         String schemaDir;
60         if (System.getProperty("user.dir") != null
61             && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
62             schemaDir = ALT_SCHEMA_DIR;
63         } else {
64             schemaDir = DEFAULT_SCHEMA_DIR;
65         }
66         String release = System.getProperty("aai.release", "onap");
67         while (versionIterator.hasNext()) {
68             System.setProperty(AAI_GENERATE_VERSION, versionIterator.next().toString());
69             String yamlFile =
70                 schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_"
71                     + System.getProperty(AAI_GENERATE_VERSION) + ".yaml";
72             File swaggerYamlFile = new File(yamlFile);
73             if (swaggerYamlFile.exists()) {
74                 GenerateSwagger.schemaVersions = schemaVersions;
75                 GenerateSwagger.main(args);
76             }
77         }
78         System.setProperty(AAI_GENERATE_VERSION, savedProperty);
79     }
80 }