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.util.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.introspection.Version;
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 public static final String CURRENT_VERSION = Version.getLatest().toString();
40 //if the program is run from aai-common, use this directory as default"
41 public static final String ALT_SCHEMA_DIR = "aai-schema";
42 //used to check to see if program is run from aai-core
43 public static final String DEFAULT_RUN_DIR = "aai-core";
45 public static void main(String[] args) throws IOException, TemplateException {
47 String schemaDir = System.getProperty("aai.schema.dir");
48 String versionToGenerate = System.getProperty("aai.generate.version");
49 String wikiLink = System.getProperty("aai.wiki.link");
51 if(schemaDir == null){
52 if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
53 System.out.println("Warning: Schema directory is not set so using default schema dir: " + ALT_SCHEMA_DIR);
54 schemaDir = ALT_SCHEMA_DIR;
57 System.out.println("Warning: Schema directory is not set so using default schema dir: " + DEFAULT_SCHEMA_DIR);
58 schemaDir = DEFAULT_SCHEMA_DIR;
62 if(versionToGenerate == null){
63 System.out.println("Warning: Version to generate is not set so using default versionToGenerate " + CURRENT_VERSION);
64 versionToGenerate = CURRENT_VERSION;
68 System.out.println("Warning: aai.wiki.link property is not set so using default");
69 wikiLink = DEFAULT_WIKI;
72 String yamlFile = schemaDir + "/src/main/resources/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml";
73 File swaggerYamlFile = new File(yamlFile);
75 if(!swaggerYamlFile.exists()){
76 System.err.println("Unable to find the swagger yaml file: " + swaggerYamlFile);
80 Yaml yaml = new Yaml(new SafeConstructor());
81 Map<String, Object> swaggerMap = null;
83 try (BufferedReader reader = new BufferedReader(new FileReader(swaggerYamlFile))){
84 swaggerMap = (Map<String, Object>) yaml.load(reader);
85 } catch(IOException ex){
89 if(null == swaggerMap) {
90 throw new IOException();
93 Map<String, Object> map = (Map<String, Object>) swaggerMap.get("paths");
94 Map<String, Object> schemaDefinitionmap = (Map<String, Object>) swaggerMap.get("definitions");
95 Map<String, Object> infoMap = (Map<String, Object>) swaggerMap.get("info");
96 Map<String, List<Api>> tagMap = new LinkedHashMap<>();
98 List<Api> apis = convertToApi(map);
99 apis.forEach((api) -> {
100 if(!tagMap.containsKey(api.getTag())){
101 List<Api> newApis = new ArrayList<>();
103 tagMap.put(api.getTag(), newApis);
105 tagMap.get(api.getTag()).add(api);
109 Map<String, List<Api>> sortedTagMap = new TreeMap<>(tagMap);
110 sortedTagMap.forEach((key, value) -> {
111 value.sort(Comparator.comparing(Api::getPath));
114 Map<String, Object> resultMap = new HashMap<>();
116 List<Definition> definitionList = convertToDefinition(schemaDefinitionmap);
118 definitionList = definitionList
119 .stream().sorted(Comparator.comparing(Definition::getDefinitionName)).collect(Collectors.toList());
121 resultMap.put("aaiApis", tagMap);
122 resultMap.put("sortedAaiApis", sortedTagMap);
123 resultMap.put("wikiLink", wikiLink);
124 resultMap.put("definitions", definitionList);
125 resultMap.put("version", versionToGenerate);
126 if (infoMap.containsKey("description")) {
127 String infoDescription = infoMap.get("description").toString();
129 infoDescription = Arrays.stream(infoDescription.split("\n"))
132 String hyperLink = "";
133 if(line.trim().contains("Differences versus")) {
134 return String.format("");
136 if(line.trim().contains("https://")){
137 int startIndex = line.indexOf("https://");
138 int endIndex = line.lastIndexOf("/");
139 hyperLink = line.substring(startIndex, endIndex);
140 return String.format("<a href=\"%s\">%s</a><br/>", hyperLink, line);
142 return String.format("%s<br/>", line);
145 .collect(Collectors.joining(LINE_SEPARATOR));
147 resultMap.put("description", infoDescription);
150 Configuration configuration = new Configuration();
151 configuration.setClassForTemplateLoading(Api.class, "/");
152 String resourcePath = "src/main/resources";
153 if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) {
154 configuration.setDirectoryForTemplateLoading(new File(DEFAULT_RUN_DIR + "/" + resourcePath));
157 configuration.setDirectoryForTemplateLoading(new File(resourcePath));
159 Template template = configuration.getTemplate("swagger.html.ftl");
161 String outputDirStr = schemaDir + "/src/main/resources/aai_swagger_html";
163 File outputDir = new File(outputDirStr);
165 if(!outputDir.exists()){
166 boolean resp = outputDir.mkdir();
168 System.err.println("Unable to create the directory: " + outputDirStr);
171 } else if(outputDir.isFile()){
172 System.err.println("Unable to create the directory: " + outputDirStr + " since a filename with that string exists");
176 Writer file = new FileWriter(new File(outputDirStr + "/aai_swagger_" + versionToGenerate + ".html"));
177 template.process(resultMap, file);
180 public static List<Api> convertToApi(Map<String, Object> pathMap){
183 throw new IllegalArgumentException();
185 List<Api> apis = new ArrayList<>();
187 pathMap.forEach( (pathKey, pathValue) -> {
190 Map<String, Object> httpVerbMap = (Map<String, Object>) pathValue;
191 List<Api.HttpVerb> httpVerbs = new ArrayList<>();
193 api.setPath(pathKey);
195 httpVerbMap.forEach((httpVerbKey, httpVerbValue) -> {
197 Api.HttpVerb httpVerb = new Api.HttpVerb();
199 Map<String, Object> httpVerbValueMap = (Map<String,Object>)httpVerbValue;
201 httpVerb.setType(httpVerbKey);
203 if(httpVerbValueMap.containsKey("tags")){
204 httpVerb.setTags((List<String>)httpVerbValueMap.get("tags"));
207 if(httpVerbValueMap.containsKey("summary")){
208 httpVerb.setSummary((String)httpVerbValueMap.get("summary"));
211 if(httpVerbValueMap.containsKey("operationId")){
212 httpVerb.setOperationId((String)httpVerbValueMap.get("operationId"));
215 if(httpVerbValueMap.containsKey("consumes")){
216 httpVerb.setConsumes((List<String>)httpVerbValueMap.get("consumes"));
217 if(httpVerb.getConsumes() != null){
218 httpVerb.setConsumerEnabled(true);
222 if(httpVerbValueMap.containsKey("produces")){
223 httpVerb.setProduces((List<String>)httpVerbValueMap.get("produces"));
226 if(httpVerbValueMap.containsKey("parameters")){
227 List<Map<String, Object>> parameters = (List<Map<String, Object>>) httpVerbValueMap.get("parameters");
228 List<Map<String, Object>> requestParameters = parameters
230 .filter((parameter) -> !parameter.get("name").equals("body"))
231 .collect(Collectors.toList());
232 httpVerb.setParameters(requestParameters);
233 if(httpVerb.getParameters() != null){
234 httpVerb.setParametersEnabled(true);
237 List<Map<String, Object>> requestBodyList = parameters
239 .filter((parameter) -> parameter.get("name").equals("body"))
240 .collect(Collectors.toList());
242 Map<String, Object> requestBody = null;
244 if(requestBodyList != null && requestBodyList.size() == 1){
245 requestBody = requestBodyList.get(0);
246 for(String key : requestBody.keySet()) {
247 //Filter out all the relationship links that appear in the YAML
248 if(key.equals("description")) {
249 String reqBody=(String)requestBody.get(key);
250 if(reqBody.replaceAll("\\[.*.json\\)", "") != reqBody) {
251 requestBody.put(key, reqBody.replaceAll("\\[.*.json\\)", ""));
254 //Filter out all the patchDefinition links that appear in the YAML
255 if(key.equals("schema")) {
256 LinkedHashMap<String,String> reqBody = (LinkedHashMap<String,String>)requestBody.get(key);
257 String schema=reqBody.get("$ref");
258 String schemaNopatch = schema.replace("patchDefinitions", "definitions");
260 if(! schema.equals(schemaNopatch)) {
261 reqBody.put("$ref", schemaNopatch);
262 requestBody.put(key, reqBody);
266 httpVerb.setBodyParametersEnabled(true);
267 httpVerb.setBodyParameters(requestBody);
269 if(requestBody != null && requestBody.containsKey("schema")){
270 Map<String, Object> schemaMap = (Map<String, Object>)requestBody.get("schema");
271 if(schemaMap != null && schemaMap.containsKey("$ref")){
272 String schemaLink = schemaMap.get("$ref").toString();
273 httpVerb.setSchemaLink(schemaLink);
274 int retCode = schemaLink.lastIndexOf('/');
275 if(retCode != -1 && retCode != schemaLink.length()){
276 httpVerb.setSchemaType(schemaLink.substring(retCode));
283 if(httpVerbValueMap.containsKey("responses")){
285 List<Api.HttpVerb.Response> responses = new ArrayList<Api.HttpVerb.Response>();
287 Map<String, Object> responsesMap = (Map<String, Object>) httpVerbValueMap.get("responses");
292 .filter((res) -> !"default".equalsIgnoreCase(res.getKey()))
293 .forEach((responseMap) -> {
295 Map<String, Object> responseValueMap = (Map<String, Object>)responseMap.getValue();
297 Api.HttpVerb.Response response = new Api.HttpVerb.Response();
299 response.setResponseCode(responseMap.getKey());
300 response.setDescription((String) responseValueMap.get("description"));
301 response.setVersion((String) responseValueMap.get("version"));
303 if(responseValueMap != null && responseValueMap.containsKey("schema")){
304 Map<String, Object> schemaMap = (Map<String, Object>)responseValueMap.get("schema");
305 if(schemaMap != null && schemaMap.containsKey("$ref")){
306 String schemaLink = schemaMap.get("$ref").toString();
307 httpVerb.setHasReturnSchema(true);
308 //Filter out all the getDefinition links that appear in the YAML
309 httpVerb.setReturnSchemaLink(schemaLink.replace("getDefinitions", "definitions"));
310 int retCode = schemaLink.lastIndexOf('/');
311 if(retCode != -1 && retCode != schemaLink.length()){
312 httpVerb.setReturnSchemaObject(schemaLink.substring(retCode));
317 responses.add(response);
321 httpVerb.setResponses(responses);
324 httpVerbs.add(httpVerb);
327 api.setHttpMethods(httpVerbs);
334 public static List<Definition> convertToDefinition(Map<String, Object> definitionMap) {
336 if(definitionMap == null)
337 throw new IllegalArgumentException();
339 List<Definition> defintionsList = new ArrayList<>();
343 .forEach((entry) -> {
345 Definition definition = new Definition();
346 String key = entry.getKey();
347 Map<String, Object> valueMap = (Map<String, Object>) entry.getValue();
349 definition.setDefinitionName(key);
351 if(valueMap.containsKey("description")){
352 String description = valueMap.get("description").toString();
353 description = formatDescription(description);
354 definition.setDefinitionDescription(description);
355 definition.setHasDescription(true);
358 List<Definition.Property> definitionProperties = new ArrayList<>();
360 List<String> requiredProperties = (valueMap.get("required") == null) ? new ArrayList<>() : (List<String>) valueMap.get("required");
362 Set<String> requiredPropsSet = requiredProperties.stream().collect(Collectors.toSet());
367 .filter( (e) -> "properties".equals(e.getKey()))
368 .forEach((propertyEntries) -> {
369 Map<String, Object> propertyRealEntries = (Map<String, Object>) propertyEntries.getValue();
372 .forEach((propertyEntry) -> {
373 Definition.Property definitionProperty = new Definition.Property();
374 String propertyKey = propertyEntry.getKey();
375 if(requiredPropsSet.contains(propertyKey)){
376 definitionProperty.setRequired(true);
378 definitionProperty.setPropertyName(propertyKey);
379 Map<String, Object> definitionPropertyMap = (Map<String, Object>) propertyEntry.getValue();
381 if(definitionPropertyMap.containsKey("description")){
382 definitionProperty.setPropertyDescription(definitionPropertyMap.get("description").toString());
383 definitionProperty.setHasPropertyDescription(true);
385 if(definitionPropertyMap.containsKey("type")){
386 String type = definitionPropertyMap.get("type").toString();
387 definitionProperty.setPropertyType(type);
388 definitionProperty.setHasType(true);
389 if ("array".equals(type)) {
390 definitionProperty.setPropertyType("object[]");
391 if(!definitionPropertyMap.containsKey("items")){
392 throw new RuntimeException("Unable to find the property items even though the type is array for " + propertyEntry.getKey());
394 Map<String, Object> itemMap = (Map<String, Object>) definitionPropertyMap.get("items");
395 if(itemMap.containsKey("$ref")){
396 definitionProperty.setHasPropertyReference(true);
397 String refItem = itemMap.get("$ref").toString();
398 int retCode = refItem.lastIndexOf('/');
399 if(retCode != -1 && retCode != refItem.length()){
400 definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1));
402 definitionProperty.setPropertyReference(refItem);
406 if(definitionPropertyMap.containsKey("$ref")){
407 definitionProperty.setHasPropertyReference(true);
408 String refItem = definitionPropertyMap.get("$ref").toString();
409 int retCode = refItem.lastIndexOf('/');
410 if(retCode != -1 && retCode != refItem.length()){
411 definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1));
413 definitionProperty.setPropertyReference(refItem);
417 definitionProperties.add(definitionProperty);
421 definition.setPropertyList(definitionProperties);
423 List<Definition.Property> schemaProperties = definitionProperties.
425 .filter((o) -> o.isHasPropertyReference())
426 .collect(Collectors.toList());
428 List<Definition.Property> regularProperties = definitionProperties.
430 .filter((o) -> !o.isHasPropertyReference())
431 .collect(Collectors.toList());
433 definition.setRegularPropertyList(regularProperties);
434 definition.setSchemaPropertyList(schemaProperties);
436 defintionsList.add(definition);
438 return defintionsList;
441 public static String formatDescription(String description){
443 description = Arrays.stream(description.split("\n"))
446 if(line.contains("######")){
447 line = line.replaceAll("#", "");
449 String headerId = line.toLowerCase().replaceAll("\\s", "-");
451 if(line.contains("Related Nodes")){
452 return String.format("<h6 id=\"%s\">%s</h6>%s<ul>", headerId, line, LINE_SEPARATOR);
454 return String.format("<h6 id=\"%s\">%s</h6>", headerId, line);
456 } else if(line.startsWith("-")){
457 line = line.replaceFirst("-", "");
459 return String.format("<li>%s</li>", line);
461 return String.format("<p>%s</p>", line);
464 .collect(Collectors.joining(LINE_SEPARATOR));
466 if(description.contains("<ul>")){
467 description = description + "</ul>";