Move the aai-schema, annotations and
[aai/schema-service.git] / aai-schema-gen / src / main / java / org / onap / aai / schemagen / swagger / GenerateSwagger.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.schemagen.swagger;
21
22 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
23 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.constructor.SafeConstructor;
24 import freemarker.template.Configuration;
25 import freemarker.template.Template;
26 import freemarker.template.TemplateException;
27 import org.onap.aai.setup.SchemaVersions;
28
29 import java.io.*;
30 import java.util.*;
31 import java.util.stream.Collectors;
32
33 public class GenerateSwagger {
34
35     public static final String LINE_SEPARATOR = System.getProperty("line.separator");
36     public static final String DEFAULT_WIKI = "";
37
38     public static final String DEFAULT_SCHEMA_DIR = "../aai-schema";
39     //if the program is run from aai-common, use this directory as default"
40     public static final String ALT_SCHEMA_DIR = "aai-schema";
41     //used to check to see if program is run from aai-schema-gen
42     public static final String DEFAULT_RUN_DIR = "aai-schema-gen";
43
44     public  static SchemaVersions schemaVersions;
45
46     public SchemaVersions getSchemaVersions() {
47                 return schemaVersions;
48         }
49
50
51
52         public static void main(String[] args) throws IOException, TemplateException {
53
54
55
56
57        // SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
58         String CURRENT_VERSION   = schemaVersions.getDefaultVersion().toString();
59         String schemaDir         = System.getProperty("aai.schema.dir");
60         String versionToGenerate = System.getProperty("aai.generate.version");
61         String wikiLink = System.getProperty("aai.wiki.link");
62         String release  = System.getProperty("aai.release", "onap");
63
64         if(schemaDir == null){
65             if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
66                 System.out.println("Warning: Schema directory is not set so using default schema dir: " + ALT_SCHEMA_DIR);
67                 schemaDir = ALT_SCHEMA_DIR;
68                 }
69                 else {
70                         System.out.println("Warning: Schema directory is not set so using default schema dir: " + DEFAULT_SCHEMA_DIR);
71                         schemaDir = DEFAULT_SCHEMA_DIR;
72                 }
73         }
74
75         if(versionToGenerate == null){
76             System.out.println("Warning: Version to generate is not set so using default versionToGenerate " + CURRENT_VERSION);
77             versionToGenerate = CURRENT_VERSION;
78         }
79
80         if(wikiLink == null){
81             System.out.println("Warning: aai.wiki.link property is not set so using default");
82             wikiLink = DEFAULT_WIKI;
83         }
84
85         String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml";
86         File swaggerYamlFile = new File(yamlFile);
87
88         if(!swaggerYamlFile.exists()){
89             System.err.println("Unable to find the swagger yaml file: " + swaggerYamlFile);
90             System.exit(1);
91         }
92
93         Yaml yaml = new Yaml(new SafeConstructor());
94         Map<String, Object> swaggerMap = null;
95
96         try (BufferedReader reader = new BufferedReader(new FileReader(swaggerYamlFile))){
97             swaggerMap = (Map<String, Object>) yaml.load(reader);
98         } catch(Exception ex){
99             ex.printStackTrace();
100         }
101
102         if(null == swaggerMap) {
103             throw new IOException();
104         }
105
106         Map<String, Object> map = (Map<String, Object>) swaggerMap.get("paths");
107         Map<String, Object> schemaDefinitionmap = (Map<String, Object>) swaggerMap.get("definitions");
108         Map<String, Object> infoMap = (Map<String, Object>) swaggerMap.get("info");
109         Map<String, List<Api>> tagMap = new LinkedHashMap<>();
110
111         List<Api> apis = convertToApi(map);
112         apis.forEach((api) -> {
113             if(!tagMap.containsKey(api.getTag())){
114                 List<Api> newApis = new ArrayList<>();
115                 newApis.add(api);
116                 tagMap.put(api.getTag(), newApis);
117             } else {
118                 tagMap.get(api.getTag()).add(api);
119             }
120         });
121
122         Map<String, List<Api>> sortedTagMap = new TreeMap<>(tagMap);
123         sortedTagMap.forEach((key, value) -> {
124             value.sort(Comparator.comparing(Api::getPath));
125         });
126
127         Map<String, Object> resultMap = new HashMap<>();
128
129         List<Definition> definitionList = convertToDefinition(schemaDefinitionmap);
130
131         definitionList = definitionList
132             .stream().sorted(Comparator.comparing(Definition::getDefinitionName)).collect(Collectors.toList());
133
134         resultMap.put("aaiApis", tagMap);
135         resultMap.put("sortedAaiApis", sortedTagMap);
136         resultMap.put("wikiLink", wikiLink);
137         resultMap.put("definitions", definitionList);
138         resultMap.put("version", versionToGenerate);
139         if (infoMap.containsKey("description")) {
140             String infoDescription = infoMap.get("description").toString();
141
142             infoDescription = Arrays.stream(infoDescription.split("\n"))
143                     .map(line -> {
144                         line = line.trim();
145                         String hyperLink = "";
146                         if(line.trim().contains("Differences versus")) {
147                                 return String.format("");
148                         }
149                         if(line.trim().contains("https://")){
150                             int startIndex = line.indexOf("https://");
151                             int endIndex = line.lastIndexOf("/");
152                             hyperLink = line.substring(startIndex, endIndex);
153                             return String.format("<a href=\"%s\">%s</a><br/>", hyperLink, line);
154                         }
155                         return String.format("%s<br/>", line);
156                     })
157
158                     .collect(Collectors.joining(LINE_SEPARATOR));
159
160             resultMap.put("description", infoDescription);
161         }
162
163         Configuration configuration = new Configuration();
164         configuration.setClassForTemplateLoading(Api.class, "/");
165         String resourcePath = "src/main/resources";
166         if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
167                 configuration.setDirectoryForTemplateLoading(new File(DEFAULT_RUN_DIR + "/" + resourcePath));
168         }
169         else {
170                 configuration.setDirectoryForTemplateLoading(new File(resourcePath));
171         }
172         Template template = configuration.getTemplate("swagger.html.ftl");
173
174         String outputDirStr = schemaDir + "/src/main/resources/" + release + "/aai_swagger_html";
175
176         File outputDir = new File(outputDirStr);
177
178         if(!outputDir.exists()){
179             boolean resp = outputDir.mkdir();
180             if(!resp){
181                 System.err.println("Unable to create the directory: " + outputDirStr);
182                 System.exit(1);
183             }
184         } else if(outputDir.isFile()){
185             System.err.println("Unable to create the directory: " + outputDirStr + " since a filename with that string exists");
186             System.exit(1);
187         }
188
189         Writer file = new FileWriter(new File(outputDirStr +  "/aai_swagger_" + versionToGenerate + ".html"));
190         template.process(resultMap, file);
191     }
192
193     public static List<Api> convertToApi(Map<String, Object> pathMap){
194
195         if(pathMap == null)
196             throw new IllegalArgumentException();
197
198         List<Api> apis = new ArrayList<>();
199
200         pathMap.forEach( (pathKey, pathValue) -> {
201
202             Api api = new Api();
203             Map<String, Object> httpVerbMap = (Map<String, Object>) pathValue;
204             List<Api.HttpVerb> httpVerbs = new ArrayList<>();
205
206             api.setPath(pathKey);
207
208             httpVerbMap.forEach((httpVerbKey, httpVerbValue) -> {
209
210                 Api.HttpVerb httpVerb = new Api.HttpVerb();
211
212                 Map<String, Object> httpVerbValueMap = (Map<String,Object>)httpVerbValue;
213
214                 httpVerb.setType(httpVerbKey);
215
216                 if(httpVerbValueMap.containsKey("tags")){
217                     httpVerb.setTags((List<String>)httpVerbValueMap.get("tags"));
218                 }
219
220                 if(httpVerbValueMap.containsKey("summary")){
221                     httpVerb.setSummary((String)httpVerbValueMap.get("summary"));
222                 }
223
224                 if(httpVerbValueMap.containsKey("operationId")){
225                     httpVerb.setOperationId((String)httpVerbValueMap.get("operationId"));
226                 }
227
228                 if(httpVerbValueMap.containsKey("consumes")){
229                     httpVerb.setConsumes((List<String>)httpVerbValueMap.get("consumes"));
230                     if(httpVerb.getConsumes() != null){
231                         httpVerb.setConsumerEnabled(true);
232                     }
233                 }
234
235                 if(httpVerbValueMap.containsKey("produces")){
236                     httpVerb.setProduces((List<String>)httpVerbValueMap.get("produces"));
237                 }
238
239                 if(httpVerbValueMap.containsKey("parameters")){
240                     List<Map<String, Object>> parameters = (List<Map<String, Object>>) httpVerbValueMap.get("parameters");
241                     List<Map<String, Object>> requestParameters = parameters
242                             .stream()
243                             .filter((parameter) -> !parameter.get("name").equals("body"))
244                             .collect(Collectors.toList());
245                     httpVerb.setParameters(requestParameters);
246                     if(httpVerb.getParameters() != null){
247                         httpVerb.setParametersEnabled(true);
248                     }
249
250                     List<Map<String, Object>> requestBodyList = parameters
251                             .stream()
252                             .filter((parameter) -> parameter.get("name").equals("body"))
253                             .collect(Collectors.toList());
254
255                     Map<String, Object> requestBody = null;
256
257                     if(requestBodyList != null && requestBodyList.size() == 1){
258                         requestBody = requestBodyList.get(0);
259                         for(String key : requestBody.keySet()) {
260                             //Filter out all the relationship links that appear in the YAML
261                                 if(key.equals("description")) {
262                                         String reqBody=(String)requestBody.get(key);
263                                         if(reqBody.replaceAll("\\[.*.json\\)", "") != reqBody) {
264                                                 requestBody.put(key, reqBody.replaceAll("\\[.*.json\\)", ""));
265                                         }
266                                 }
267                             //Filter out all the patchDefinition links that appear in the YAML
268                                 if(key.equals("schema")) {
269                                         LinkedHashMap<String,String> reqBody = (LinkedHashMap<String,String>)requestBody.get(key);
270                                         String schema=reqBody.get("$ref");
271                                         String schemaNopatch = schema.replace("patchDefinitions", "definitions");
272
273                                         if(! schema.equals(schemaNopatch)) {
274                                                 reqBody.put("$ref", schemaNopatch);
275                                                 requestBody.put(key, reqBody);
276                                         }
277                                 }
278                         }
279                         httpVerb.setBodyParametersEnabled(true);
280                         httpVerb.setBodyParameters(requestBody);
281
282                         if(requestBody != null && requestBody.containsKey("schema")){
283                             Map<String, Object> schemaMap = (Map<String, Object>)requestBody.get("schema");
284                             if(schemaMap != null && schemaMap.containsKey("$ref")){
285                                 String schemaLink = schemaMap.get("$ref").toString();
286                                 httpVerb.setSchemaLink(schemaLink);
287                                 int retCode = schemaLink.lastIndexOf('/');
288                                 if(retCode != -1 && retCode != schemaLink.length()){
289                                     httpVerb.setSchemaType(schemaLink.substring(retCode));
290                                 }
291                             }
292                         }
293                     }
294                 }
295
296                 if(httpVerbValueMap.containsKey("responses")){
297
298                     List<Api.HttpVerb.Response> responses = new ArrayList<Api.HttpVerb.Response>();
299
300                     Map<String, Object> responsesMap = (Map<String, Object>) httpVerbValueMap.get("responses");
301
302                     responsesMap
303                         .entrySet()
304                         .stream()
305                         .filter((res) -> !"default".equalsIgnoreCase(res.getKey()))
306                         .forEach((responseMap) -> {
307
308                             Map<String, Object> responseValueMap = (Map<String, Object>)responseMap.getValue();
309
310                             Api.HttpVerb.Response response = new Api.HttpVerb.Response();
311
312                             response.setResponseCode(responseMap.getKey());
313                             response.setDescription((String) responseValueMap.get("description"));
314                             response.setVersion((String) responseValueMap.get("version"));
315
316                             if(responseValueMap != null && responseValueMap.containsKey("schema")){
317                                 Map<String, Object> schemaMap = (Map<String, Object>)responseValueMap.get("schema");
318                                 if(schemaMap != null && schemaMap.containsKey("$ref")){
319                                     String schemaLink = schemaMap.get("$ref").toString();
320                                     httpVerb.setHasReturnSchema(true);
321                                     //Filter out all the getDefinition links that appear in the YAML
322                                     httpVerb.setReturnSchemaLink(schemaLink.replace("getDefinitions", "definitions"));
323                                     int retCode = schemaLink.lastIndexOf('/');
324                                     if(retCode != -1 && retCode != schemaLink.length()){
325                                         httpVerb.setReturnSchemaObject(schemaLink.substring(retCode));
326                                     }
327                                 }
328                             }
329
330                             responses.add(response);
331                         }
332                     );
333
334                     httpVerb.setResponses(responses);
335                 }
336
337                 httpVerbs.add(httpVerb);
338             });
339
340             api.setHttpMethods(httpVerbs);
341             apis.add(api);
342         });
343
344         return apis;
345     }
346
347     public static List<Definition> convertToDefinition(Map<String, Object> definitionMap) {
348
349         if(definitionMap == null)
350             throw new IllegalArgumentException();
351
352         List<Definition> defintionsList = new ArrayList<>();
353
354         definitionMap
355             .entrySet()
356             .forEach((entry) -> {
357
358                 Definition definition = new Definition();
359                 String key = entry.getKey();
360                 Map<String, Object> valueMap = (Map<String, Object>) entry.getValue();
361
362                 definition.setDefinitionName(key);
363
364                 if(valueMap.containsKey("description")){
365                     String description = valueMap.get("description").toString();
366                     description = formatDescription(description);
367                     definition.setDefinitionDescription(description);
368                     definition.setHasDescription(true);
369                 }
370
371                 List<Definition.Property> definitionProperties = new ArrayList<>();
372
373                 List<String> requiredProperties = (valueMap.get("required") == null) ? new ArrayList<>() : (List<String>) valueMap.get("required");
374
375                 Set<String> requiredPropsSet = requiredProperties.stream().collect(Collectors.toSet());
376
377                 valueMap
378                     .entrySet()
379                     .stream()
380                     .filter( (e) -> "properties".equals(e.getKey()))
381                     .forEach((propertyEntries) -> {
382                         Map<String, Object> propertyRealEntries = (Map<String, Object>) propertyEntries.getValue();
383                         propertyRealEntries
384                             .entrySet()
385                             .forEach((propertyEntry) -> {
386                                 Definition.Property definitionProperty = new Definition.Property();
387                                 String propertyKey = propertyEntry.getKey();
388                                 if(requiredPropsSet.contains(propertyKey)){
389                                     definitionProperty.setRequired(true);
390                                 }
391                                 definitionProperty.setPropertyName(propertyKey);
392                                 Map<String, Object> definitionPropertyMap = (Map<String, Object>) propertyEntry.getValue();
393
394                                 if(definitionPropertyMap.containsKey("description")){
395                                     definitionProperty.setPropertyDescription(definitionPropertyMap.get("description").toString());
396                                     definitionProperty.setHasPropertyDescription(true);
397                                 }
398                                 if(definitionPropertyMap.containsKey("type")){
399                                     String type = definitionPropertyMap.get("type").toString();
400                                     definitionProperty.setPropertyType(type);
401                                     definitionProperty.setHasType(true);
402                                     if ("array".equals(type)) {
403                                         definitionProperty.setPropertyType("object[]");
404                                         if(!definitionPropertyMap.containsKey("items")){
405                                             throw new RuntimeException("Unable to find the property items even though the type is array for " + propertyEntry.getKey());
406                                         } else {
407                                             Map<String, Object> itemMap = (Map<String, Object>) definitionPropertyMap.get("items");
408                                             if(itemMap.containsKey("$ref")){
409                                                 definitionProperty.setHasPropertyReference(true);
410                                                 String refItem = itemMap.get("$ref").toString();
411                                                 int retCode = refItem.lastIndexOf('/');
412                                                 if(retCode != -1 && retCode != refItem.length()){
413                                                     definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1));
414                                                 }
415                                                 definitionProperty.setPropertyReference(refItem);
416                                             }
417                                         }
418                                     } else {
419                                         if(definitionPropertyMap.containsKey("$ref")){
420                                             definitionProperty.setHasPropertyReference(true);
421                                             String refItem = definitionPropertyMap.get("$ref").toString();
422                                             int retCode = refItem.lastIndexOf('/');
423                                             if(retCode != -1 && retCode != refItem.length()){
424                                                 definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1));
425                                             }
426                                             definitionProperty.setPropertyReference(refItem);
427                                         }
428                                     }
429                                 }
430                                 definitionProperties.add(definitionProperty);
431                             });
432                     });
433
434                 definition.setPropertyList(definitionProperties);
435
436                 List<Definition.Property> schemaProperties = definitionProperties.
437                         stream()
438                         .filter((o) -> o.isHasPropertyReference())
439                         .collect(Collectors.toList());
440
441                 List<Definition.Property> regularProperties = definitionProperties.
442                         stream()
443                         .filter((o) -> !o.isHasPropertyReference())
444                         .collect(Collectors.toList());
445
446                 definition.setRegularPropertyList(regularProperties);
447                 definition.setSchemaPropertyList(schemaProperties);
448
449                 defintionsList.add(definition);
450             });
451         return defintionsList;
452     }
453
454     public static String formatDescription(String description){
455
456         description = Arrays.stream(description.split("\n"))
457             .map((line) -> {
458                 line = line.trim();
459                 if(line.contains("######")){
460                     line = line.replaceAll("#", "");
461                     line = line.trim();
462                     String headerId = line.toLowerCase().replaceAll("\\s", "-");
463
464                     if(line.contains("Related Nodes")){
465                         return String.format("<h6 id=\"%s\">%s</h6>%s<ul>", headerId, line, LINE_SEPARATOR);
466                     } else {
467                         return String.format("<h6 id=\"%s\">%s</h6>", headerId, line);
468                     }
469                 } else if(line.startsWith("-")){
470                     line = line.replaceFirst("-", "");
471                     line = line.trim();
472                     return String.format("<li>%s</li>", line);
473                 } else {
474                     return String.format("<p>%s</p>", line);
475                 }
476             })
477             .collect(Collectors.joining(LINE_SEPARATOR));
478
479         if(description.contains("<ul>")){
480             description = description + "</ul>";
481         }
482
483         return description;
484     }
485
486 }
487