f279aef62df53ca78e208f409695e7ea715ff18e
[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 java.io.File;
26 import java.io.IOException;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.ListIterator;
30
31 import org.onap.aai.schemagen.swagger.GenerateSwagger;
32 import org.onap.aai.setup.SchemaConfigVersions;
33 import org.onap.aai.setup.SchemaVersion;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.BeansException;
37 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
38
39 import freemarker.template.TemplateException;
40
41 public class AutoGenerateHtml {
42
43     private static Logger logger = LoggerFactory.getLogger(AutoGenerateHtml.class);
44     private static final String AAI_GENERATE_VERSION = "aai.generate.version";
45     public static final String DEFAULT_SCHEMA_DIR = "../aai-schema";
46     // if the program is run from aai-common, use this directory as default"
47     public static final String ALT_SCHEMA_DIR = "aai-schema";
48     // used to check to see if program is run from aai-schema-gen
49     public static final String DEFAULT_RUN_DIR = "aai-schema-gen";
50
51     public static void main(String[] args) throws IOException, TemplateException {
52         String savedProperty = System.getProperty(AAI_GENERATE_VERSION);
53
54         try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
55             "org.onap.aai.setup", "org.onap.aai.schemagen")) {
56
57         SchemaConfigVersions schemaConfigVersions = ctx.getBean(SchemaConfigVersions.class);
58
59         List<SchemaVersion> versionsToGen = schemaConfigVersions.getVersions();
60         Collections.sort(versionsToGen);
61         Collections.reverse(versionsToGen);
62         ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator();
63         String schemaDir;
64         if (System.getProperty("user.dir") != null
65             && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
66             schemaDir = ALT_SCHEMA_DIR;
67         } else {
68             schemaDir = DEFAULT_SCHEMA_DIR;
69         }
70         String release = System.getProperty("aai.release", "onap");
71         while (versionIterator.hasNext()) {
72             System.setProperty(AAI_GENERATE_VERSION, versionIterator.next().toString());
73             String yamlFile =
74                 schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_"
75                     + System.getProperty(AAI_GENERATE_VERSION) + ".yaml";
76             File swaggerYamlFile = new File(yamlFile);
77             if (swaggerYamlFile.exists()) {
78                 GenerateSwagger.schemaConfigVersions = schemaConfigVersions;
79                 GenerateSwagger.main(args);
80             }
81         }
82         } catch (BeansException e) {
83             logger.warn("Unable to initialize AnnotationConfigApplicationContext ", e);
84         }
85
86         System.setProperty(AAI_GENERATE_VERSION, savedProperty);
87     }
88 }