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