1 package org.onap.graphinventory.generate;
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;
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;
24 public class SwaggerConverter {
26 private final Log log;
28 public SwaggerConverter(Log log) {
32 public Map<String, ObjectType> getDoc(String swaggerLocation) throws JsonProcessingException {
35 swaggerLocation = processLocation(swaggerLocation);
36 Swagger swagger = new SwaggerParser().read(swaggerLocation);
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()));
42 Matcher pluralMatcher;
43 Matcher singularMatcher;
44 Matcher topLevelMatcher;
46 Map<String, ObjectType> output = new HashMap<>();
47 for (Map.Entry<String, Path> entry : paths.entrySet()) {
49 pluralMatcher = Patterns.pluralPattern.matcher(entry.getKey());
50 singularMatcher = Patterns.singularPattern.matcher(entry.getKey());
51 topLevelMatcher = Patterns.topLevelPattern.matcher(entry.getKey());
53 if (pluralMatcher.matches()) {
54 if (!output.containsKey(pluralMatcher.group("name"))) {
55 output.put(pluralMatcher.group("name"), new ObjectType());
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());
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));
74 } else if (singularMatcher.matches()) {
76 if (!output.containsKey(singularMatcher.group("name"))) {
77 output.put(singularMatcher.group("name"), new ObjectType());
79 item = output.get(singularMatcher.group("name"));
81 item.setType("singular");
82 item.setName(singularMatcher.group("name"));
83 item.setPartialUri(singularMatcher.group("partial"));
85 item.getPaths().add(entry.getKey());
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));
97 List<Parameter> parameters = entry.getValue().getGet().getParameters();
99 if (parameters != null) {
100 parameters.stream().filter(param -> "path".equals(param.getIn())).collect(Collectors.toList());
102 for (Parameter p : parameters) {
103 ObjectField field = new ObjectField();
105 field.setName(p.getName());
106 field.setType(((SerializableParameter) p).getType());
107 item.getFields().add(field);
111 item = output.get(singularMatcher.group("name"));
112 if (singularMatcher.group("partial").contains(item.getName() + ".")) {
113 item.setPartialUri(singularMatcher.group("partial"));
115 item.getPaths().add(entry.getKey());
120 ObjectMapper mapper = new ObjectMapper();
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());
135 for (Map.Entry<String, ObjectType> item : output.entrySet()) {
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());
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);
151 item.getValue().getChildren().addAll(pluralChildren);
153 if (item.getValue().getType().equals("plural")) {
154 for (String child : item.getValue().getChildren()) {
156 .setPartialUri(item.getValue().getPartialUri() + output.get(child).getPartialUri());
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));
166 item.getValue().setFields(item.getValue().getFields().stream()
167 .filter(f -> localFields.contains(f.getName())).collect(Collectors.toList()));
171 output.values().stream().filter(item -> item.getType().equals("plural"))
172 .forEach(item -> item.getChildren().clear());
174 log.debug(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(output));
179 private String processLocation(String swaggerLocation) {
181 java.nio.file.Path path = Paths.get(swaggerLocation);
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) {