c54f85d16c276a79c4e344ecead4e43edc706712
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / AutoGenerateHtml.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 package org.onap.aai.util;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.ListIterator;
27
28 import org.onap.aai.setup.SchemaVersion;
29 import org.onap.aai.setup.SchemaVersions;
30 import org.onap.aai.util.swagger.GenerateSwagger;
31
32 import freemarker.template.TemplateException;
33 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
34
35 public class AutoGenerateHtml {
36         
37         public static final String DEFAULT_SCHEMA_DIR = "../aai-schema";
38         //if the program is run from aai-common, use this directory as default"
39         public static final String ALT_SCHEMA_DIR = "aai-schema";
40         //used to check to see if program is run from aai-core
41         public static final String DEFAULT_RUN_DIR = "aai-core";
42
43         public static void main(String[] args) throws IOException, TemplateException {
44                 String savedProperty = System.getProperty("aai.generate.version");
45
46                 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
47                         "org.onap.aai.config",
48                         "org.onap.aai.setup"
49                 );
50
51
52                 SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);
53                 
54                 List<SchemaVersion> versionsToGen = schemaVersions.getVersions();
55                 Collections.sort(versionsToGen);
56                 Collections.reverse(versionsToGen);
57                 ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator();
58                 String schemaDir;
59                 if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
60                         schemaDir = ALT_SCHEMA_DIR;
61                 }
62                 else {
63                         schemaDir = DEFAULT_SCHEMA_DIR;
64                 }
65                 String release = System.getProperty("aai.release", "onap");
66                 while (versionIterator.hasNext()) {
67                         System.setProperty("aai.generate.version", versionIterator.next().toString());
68                 String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + System.getProperty("aai.generate.version")+ ".yaml";
69                 File swaggerYamlFile = new File(yamlFile);
70                 if(swaggerYamlFile.exists()) {
71                         GenerateSwagger.schemaVersions = schemaVersions;
72                         GenerateSwagger.main(args);
73                 }
74                 }
75                 String versionToGenerate = System.setProperty("aai.generate.version", savedProperty);   
76         }
77 }