2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.schemagen.swagger;
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;
31 import java.util.stream.Collectors;
33 public class GenerateSwagger {
35 public static final String LINE_SEPARATOR = System.getProperty("line.separator");
36 public static final String DEFAULT_WIKI = "";
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";
44 public static SchemaVersions schemaVersions;
46 public SchemaVersions getSchemaVersions() {
47 return schemaVersions;
52 public static void main(String[] args) throws IOException, TemplateException {
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");
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;
70 System.out.println("Warning: Schema directory is not set so using default schema dir: " + DEFAULT_SCHEMA_DIR);
71 schemaDir = DEFAULT_SCHEMA_DIR;
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;
81 System.out.println("Warning: aai.wiki.link property is not set so using default");
82 wikiLink = DEFAULT_WIKI;
85 String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml";
86 File swaggerYamlFile = new File(yamlFile);
88 if(!swaggerYamlFile.exists()){
89 System.err.println("Unable to find the swagger yaml file: " + swaggerYamlFile);
93 Yaml yaml = new Yaml(new SafeConstructor());
94 Map<String, Object> swaggerMap = null;
96 try (BufferedReader reader = new BufferedReader(new FileReader(swaggerYamlFile))){
97 swaggerMap = (Map<String, Object>) yaml.load(reader);
98 } catch(Exception ex){
102 if(null == swaggerMap) {
103 throw new IOException();
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<>();
111 List<Api> apis = convertToApi(map);
112 apis.forEach((api) -> {
113 if(!tagMap.containsKey(api.getTag())){
114 List<Api> newApis = new ArrayList<>();
116 tagMap.put(api.getTag(), newApis);
118 tagMap.get(api.getTag()).add(api);
122 Map<String, List<Api>> sortedTagMap = new TreeMap<>(tagMap);
123 sortedTagMap.forEach((key, value) -> {
124 value.sort(Comparator.comparing(Api::getPath));
127 Map<String, Object> resultMap = new HashMap<>();
129 List<Definition> definitionList = convertToDefinition(schemaDefinitionmap);
131 definitionList = definitionList
132 .stream().sorted(Comparator.comparing(Definition::getDefinitionName)).collect(Collectors.toList());
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();
142 infoDescription = Arrays.stream(infoDescription.split("\n"))
145 String hyperLink = "";
146 if(line.trim().contains("Differences versus")) {
147 return String.format("");
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);
155 return String.format("%s<br/>", line);
158 .collect(Collectors.joining(LINE_SEPARATOR));
160 resultMap.put("description", infoDescription);
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));
170 configuration.setDirectoryForTemplateLoading(new File(resourcePath));
172 Template template = configuration.getTemplate("swagger.html.ftl");
174 String outputDirStr = schemaDir + "/src/main/resources/" + release + "/aai_swagger_html";
176 File outputDir = new File(outputDirStr);
178 if(!outputDir.exists()){
179 boolean resp = outputDir.mkdir();
181 System.err.println("Unable to create the directory: " + outputDirStr);
184 } else if(outputDir.isFile()){
185 System.err.println("Unable to create the directory: " + outputDirStr + " since a filename with that string exists");
189 Writer file = new FileWriter(new File(outputDirStr + "/aai_swagger_" + versionToGenerate + ".html"));
190 template.process(resultMap, file);
193 public static List<Api> convertToApi(Map<String, Object> pathMap){
196 throw new IllegalArgumentException();
198 List<Api> apis = new ArrayList<>();
200 pathMap.forEach( (pathKey, pathValue) -> {
203 Map<String, Object> httpVerbMap = (Map<String, Object>) pathValue;
204 List<Api.HttpVerb> httpVerbs = new ArrayList<>();
206 api.setPath(pathKey);
208 httpVerbMap.forEach((httpVerbKey, httpVerbValue) -> {
210 Api.HttpVerb httpVerb = new Api.HttpVerb();
212 Map<String, Object> httpVerbValueMap = (Map<String,Object>)httpVerbValue;
214 httpVerb.setType(httpVerbKey);
216 if(httpVerbValueMap.containsKey("tags")){
217 httpVerb.setTags((List<String>)httpVerbValueMap.get("tags"));
220 if(httpVerbValueMap.containsKey("summary")){
221 httpVerb.setSummary((String)httpVerbValueMap.get("summary"));
224 if(httpVerbValueMap.containsKey("operationId")){
225 httpVerb.setOperationId((String)httpVerbValueMap.get("operationId"));
228 if(httpVerbValueMap.containsKey("consumes")){
229 httpVerb.setConsumes((List<String>)httpVerbValueMap.get("consumes"));
230 if(httpVerb.getConsumes() != null){
231 httpVerb.setConsumerEnabled(true);
235 if(httpVerbValueMap.containsKey("produces")){
236 httpVerb.setProduces((List<String>)httpVerbValueMap.get("produces"));
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
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);
250 List<Map<String, Object>> requestBodyList = parameters
252 .filter((parameter) -> parameter.get("name").equals("body"))
253 .collect(Collectors.toList());
255 Map<String, Object> requestBody = null;
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\\)", ""));
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");
273 if(! schema.equals(schemaNopatch)) {
274 reqBody.put("$ref", schemaNopatch);
275 requestBody.put(key, reqBody);
279 httpVerb.setBodyParametersEnabled(true);
280 httpVerb.setBodyParameters(requestBody);
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));
296 if(httpVerbValueMap.containsKey("responses")){
298 List<Api.HttpVerb.Response> responses = new ArrayList<Api.HttpVerb.Response>();
300 Map<String, Object> responsesMap = (Map<String, Object>) httpVerbValueMap.get("responses");
305 .filter((res) -> !"default".equalsIgnoreCase(res.getKey()))
306 .forEach((responseMap) -> {
308 Map<String, Object> responseValueMap = (Map<String, Object>)responseMap.getValue();
310 Api.HttpVerb.Response response = new Api.HttpVerb.Response();
312 response.setResponseCode(responseMap.getKey());
313 response.setDescription((String) responseValueMap.get("description"));
314 response.setVersion((String) responseValueMap.get("version"));
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));
330 responses.add(response);
334 httpVerb.setResponses(responses);
337 httpVerbs.add(httpVerb);
340 api.setHttpMethods(httpVerbs);
347 public static List<Definition> convertToDefinition(Map<String, Object> definitionMap) {
349 if(definitionMap == null)
350 throw new IllegalArgumentException();
352 List<Definition> defintionsList = new ArrayList<>();
356 .forEach((entry) -> {
358 Definition definition = new Definition();
359 String key = entry.getKey();
360 Map<String, Object> valueMap = (Map<String, Object>) entry.getValue();
362 definition.setDefinitionName(key);
364 if(valueMap.containsKey("description")){
365 String description = valueMap.get("description").toString();
366 description = formatDescription(description);
367 definition.setDefinitionDescription(description);
368 definition.setHasDescription(true);
371 List<Definition.Property> definitionProperties = new ArrayList<>();
373 List<String> requiredProperties = (valueMap.get("required") == null) ? new ArrayList<>() : (List<String>) valueMap.get("required");
375 Set<String> requiredPropsSet = requiredProperties.stream().collect(Collectors.toSet());
380 .filter( (e) -> "properties".equals(e.getKey()))
381 .forEach((propertyEntries) -> {
382 Map<String, Object> propertyRealEntries = (Map<String, Object>) propertyEntries.getValue();
385 .forEach((propertyEntry) -> {
386 Definition.Property definitionProperty = new Definition.Property();
387 String propertyKey = propertyEntry.getKey();
388 if(requiredPropsSet.contains(propertyKey)){
389 definitionProperty.setRequired(true);
391 definitionProperty.setPropertyName(propertyKey);
392 Map<String, Object> definitionPropertyMap = (Map<String, Object>) propertyEntry.getValue();
394 if(definitionPropertyMap.containsKey("description")){
395 definitionProperty.setPropertyDescription(definitionPropertyMap.get("description").toString());
396 definitionProperty.setHasPropertyDescription(true);
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());
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));
415 definitionProperty.setPropertyReference(refItem);
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));
426 definitionProperty.setPropertyReference(refItem);
430 definitionProperties.add(definitionProperty);
434 definition.setPropertyList(definitionProperties);
436 List<Definition.Property> schemaProperties = definitionProperties.
438 .filter((o) -> o.isHasPropertyReference())
439 .collect(Collectors.toList());
441 List<Definition.Property> regularProperties = definitionProperties.
443 .filter((o) -> !o.isHasPropertyReference())
444 .collect(Collectors.toList());
446 definition.setRegularPropertyList(regularProperties);
447 definition.setSchemaPropertyList(schemaProperties);
449 defintionsList.add(definition);
451 return defintionsList;
454 public static String formatDescription(String description){
456 description = Arrays.stream(description.split("\n"))
459 if(line.contains("######")){
460 line = line.replaceAll("#", "");
462 String headerId = line.toLowerCase().replaceAll("\\s", "-");
464 if(line.contains("Related Nodes")){
465 return String.format("<h6 id=\"%s\">%s</h6>%s<ul>", headerId, line, LINE_SEPARATOR);
467 return String.format("<h6 id=\"%s\">%s</h6>", headerId, line);
469 } else if(line.startsWith("-")){
470 line = line.replaceFirst("-", "");
472 return String.format("<li>%s</li>", line);
474 return String.format("<p>%s</p>", line);
477 .collect(Collectors.joining(LINE_SEPARATOR));
479 if(description.contains("<ul>")){
480 description = description + "</ul>";