ec09af8a4e1768a636dd71ece2e397dfb1a3226d
[so.git] /
1 package org.onap.graphinventory.generate;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.regex.Matcher;
10 import java.util.stream.Collectors;
11 import org.apache.maven.plugin.logging.Log;
12 import com.fasterxml.jackson.core.JsonProcessingException;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import io.swagger.models.Path;
15 import io.swagger.models.Swagger;
16 import io.swagger.models.parameters.Parameter;
17 import io.swagger.models.parameters.SerializableParameter;
18 import io.swagger.parser.SwaggerParser;
19
20 public class SwaggerConverter {
21
22     private final Log log;
23
24     public SwaggerConverter(Log log) {
25         this.log = log;
26     }
27
28     public Map<String, ObjectType> getDoc(String swaggerLocation) throws JsonProcessingException {
29         Swagger swagger = new SwaggerParser().read(swaggerLocation);
30
31         Map<String, Path> paths = swagger.getPaths().entrySet().stream()
32                 .filter(item -> !item.getKey().endsWith("/relationship-list/relationship"))
33                 .collect(Collectors.toMap(item -> item.getKey(), item -> item.getValue()));
34
35         Matcher pluralMatcher;
36         Matcher singularMatcher;
37         Matcher topLevelMatcher;
38
39         Map<String, ObjectType> output = new HashMap<>();
40         for (Map.Entry<String, Path> entry : paths.entrySet()) {
41
42             pluralMatcher = Patterns.pluralPattern.matcher(entry.getKey());
43             singularMatcher = Patterns.singularPattern.matcher(entry.getKey());
44             topLevelMatcher = Patterns.topLevelPattern.matcher(entry.getKey());
45             ObjectType item;
46             if (pluralMatcher.matches()) {
47                 if (!output.containsKey(pluralMatcher.group("name"))) {
48                     output.put(pluralMatcher.group("name"), new ObjectType());
49                 }
50                 item = output.get(pluralMatcher.group("name"));
51                 item.setType("plural");
52                 item.setName(pluralMatcher.group("name"));
53                 item.setPartialUri(pluralMatcher.group("partial"));
54                 item.getPaths().add(entry.getKey());
55
56                 if (topLevelMatcher.matches()) {
57                     item.setTopLevel(topLevelMatcher.group(1));
58                     if (!output.containsKey(topLevelMatcher.group(1))) {
59                         output.put(topLevelMatcher.group(1), new ObjectType());
60                         output.get(topLevelMatcher.group(1)).setType("top level");
61                         output.get(topLevelMatcher.group(1)).setName(topLevelMatcher.group(1));
62                         output.get(topLevelMatcher.group(1)).setPartialUri("/" + topLevelMatcher.group(1));
63                         output.get(topLevelMatcher.group(1)).getPaths().add("/" + topLevelMatcher.group(1));
64
65                     }
66                 }
67             } else if (singularMatcher.matches()) {
68
69                 if (!output.containsKey(singularMatcher.group("name"))) {
70                     output.put(singularMatcher.group("name"), new ObjectType());
71
72                     item = output.get(singularMatcher.group("name"));
73
74                     item.setType("singular");
75                     item.setName(singularMatcher.group("name"));
76                     item.setPartialUri(singularMatcher.group("partial"));
77
78                     item.getPaths().add(entry.getKey());
79
80                     if (topLevelMatcher.matches()) {
81                         item.setTopLevel(topLevelMatcher.group(1));
82                         if (!output.containsKey(topLevelMatcher.group(1))) {
83                             output.put(topLevelMatcher.group(1), new ObjectType());
84                             output.get(topLevelMatcher.group(1)).setType("top level");
85                             output.get(topLevelMatcher.group(1)).setName(topLevelMatcher.group(1));
86                             output.get(topLevelMatcher.group(1)).setPartialUri("/" + topLevelMatcher.group(1));
87                             output.get(topLevelMatcher.group(1)).getPaths().add("/" + topLevelMatcher.group(1));
88                         }
89                     }
90                     List<Parameter> parameters = entry.getValue().getGet().getParameters();
91
92                     if (parameters != null) {
93                         parameters.stream().filter(param -> "path".equals(param.getIn())).collect(Collectors.toList());
94                     }
95                     for (Parameter p : parameters) {
96                         ObjectField field = new ObjectField();
97
98                         field.setName(p.getName());
99                         field.setType(((SerializableParameter) p).getType());
100                         item.getFields().add(field);
101                     }
102
103                 } else {
104                     item = output.get(singularMatcher.group("name"));
105                     if (singularMatcher.group("partial").contains(item.getName() + ".")) {
106                         item.setPartialUri(singularMatcher.group("partial"));
107                     }
108                     item.getPaths().add(entry.getKey());
109                 }
110             }
111
112         }
113         ObjectMapper mapper = new ObjectMapper();
114
115         for (ObjectType item : output.values()) {
116             for (String path : item.getPaths()) {
117                 String partialUriReplacer = item.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+");
118                 String childParentUri = path.replaceFirst(partialUriReplacer + "$", "");
119                 for (ObjectType itemToUpdate : output.values()) {
120                     if (itemToUpdate.getPaths().stream()
121                             .anyMatch(itemToUpdateUri -> itemToUpdateUri.equals(childParentUri))) {
122                         itemToUpdate.getChildren().add(item.getName());
123                     }
124                 }
125             }
126         }
127
128         for (Map.Entry<String, ObjectType> item : output.entrySet()) {
129
130             if (item.getValue().getType().equals("plural")) {
131                 Set<String> children = item.getValue().getChildren();
132                 // should only be one
133                 if (!children.isEmpty()) {
134                     item.getValue().setAdditionalName(children.iterator().next());
135                 }
136             }
137             Set<String> pluralChildren = new HashSet<>();
138             for (String child : item.getValue().getChildren()) {
139                 if (output.get(child).getType().equals("plural")) {
140                     Set<String> children = output.get(child).getChildren();
141                     pluralChildren.addAll(children);
142                 }
143             }
144             item.getValue().getChildren().addAll(pluralChildren);
145
146             if (item.getValue().getType().equals("plural")) {
147                 for (String child : item.getValue().getChildren()) {
148                     output.get(child)
149                             .setPartialUri(item.getValue().getPartialUri() + output.get(child).getPartialUri());
150                 }
151             }
152
153             if (!item.getValue().getFields().isEmpty()) {
154                 Matcher templates = Patterns.urlTemplatePattern.matcher(item.getValue().getPartialUri());
155                 List<String> localFields = new ArrayList<>();
156                 while (templates.find()) {
157                     localFields.add(templates.group(2));
158                 }
159                 item.getValue().setFields(item.getValue().getFields().stream()
160                         .filter(f -> localFields.contains(f.getName())).collect(Collectors.toList()));
161             }
162         }
163
164         output.values().stream().filter(item -> item.getType().equals("plural"))
165                 .forEach(item -> item.getChildren().clear());
166
167         log.debug(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(output));
168
169         return output;
170     }
171 }