aboutsummaryrefslogtreecommitdiffstats
path: root/graph-inventory/fluent-builder-maven-plugin/src/main/java/org/onap/graphinventory/generate/SwaggerConverter.java
blob: ec09af8a4e1768a636dd71ece2e397dfb1a3226d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package org.onap.graphinventory.generate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import org.apache.maven.plugin.logging.Log;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.models.Path;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.SerializableParameter;
import io.swagger.parser.SwaggerParser;

public class SwaggerConverter {

    private final Log log;

    public SwaggerConverter(Log log) {
        this.log = log;
    }

    public Map<String, ObjectType> getDoc(String swaggerLocation) throws JsonProcessingException {
        Swagger swagger = new SwaggerParser().read(swaggerLocation);

        Map<String, Path> paths = swagger.getPaths().entrySet().stream()
                .filter(item -> !item.getKey().endsWith("/relationship-list/relationship"))
                .collect(Collectors.toMap(item -> item.getKey(), item -> item.getValue()));

        Matcher pluralMatcher;
        Matcher singularMatcher;
        Matcher topLevelMatcher;

        Map<String, ObjectType> output = new HashMap<>();
        for (Map.Entry<String, Path> entry : paths.entrySet()) {

            pluralMatcher = Patterns.pluralPattern.matcher(entry.getKey());
            singularMatcher = Patterns.singularPattern.matcher(entry.getKey());
            topLevelMatcher = Patterns.topLevelPattern.matcher(entry.getKey());
            ObjectType item;
            if (pluralMatcher.matches()) {
                if (!output.containsKey(pluralMatcher.group("name"))) {
                    output.put(pluralMatcher.group("name"), new ObjectType());
                }
                item = output.get(pluralMatcher.group("name"));
                item.setType("plural");
                item.setName(pluralMatcher.group("name"));
                item.setPartialUri(pluralMatcher.group("partial"));
                item.getPaths().add(entry.getKey());

                if (topLevelMatcher.matches()) {
                    item.setTopLevel(topLevelMatcher.group(1));
                    if (!output.containsKey(topLevelMatcher.group(1))) {
                        output.put(topLevelMatcher.group(1), new ObjectType());
                        output.get(topLevelMatcher.group(1)).setType("top level");
                        output.get(topLevelMatcher.group(1)).setName(topLevelMatcher.group(1));
                        output.get(topLevelMatcher.group(1)).setPartialUri("/" + topLevelMatcher.group(1));
                        output.get(topLevelMatcher.group(1)).getPaths().add("/" + topLevelMatcher.group(1));

                    }
                }
            } else if (singularMatcher.matches()) {

                if (!output.containsKey(singularMatcher.group("name"))) {
                    output.put(singularMatcher.group("name"), new ObjectType());

                    item = output.get(singularMatcher.group("name"));

                    item.setType("singular");
                    item.setName(singularMatcher.group("name"));
                    item.setPartialUri(singularMatcher.group("partial"));

                    item.getPaths().add(entry.getKey());

                    if (topLevelMatcher.matches()) {
                        item.setTopLevel(topLevelMatcher.group(1));
                        if (!output.containsKey(topLevelMatcher.group(1))) {
                            output.put(topLevelMatcher.group(1), new ObjectType());
                            output.get(topLevelMatcher.group(1)).setType("top level");
                            output.get(topLevelMatcher.group(1)).setName(topLevelMatcher.group(1));
                            output.get(topLevelMatcher.group(1)).setPartialUri("/" + topLevelMatcher.group(1));
                            output.get(topLevelMatcher.group(1)).getPaths().add("/" + topLevelMatcher.group(1));
                        }
                    }
                    List<Parameter> parameters = entry.getValue().getGet().getParameters();

                    if (parameters != null) {
                        parameters.stream().filter(param -> "path".equals(param.getIn())).collect(Collectors.toList());
                    }
                    for (Parameter p : parameters) {
                        ObjectField field = new ObjectField();

                        field.setName(p.getName());
                        field.setType(((SerializableParameter) p).getType());
                        item.getFields().add(field);
                    }

                } else {
                    item = output.get(singularMatcher.group("name"));
                    if (singularMatcher.group("partial").contains(item.getName() + ".")) {
                        item.setPartialUri(singularMatcher.group("partial"));
                    }
                    item.getPaths().add(entry.getKey());
                }
            }

        }
        ObjectMapper mapper = new ObjectMapper();

        for (ObjectType item : output.values()) {
            for (String path : item.getPaths()) {
                String partialUriReplacer = item.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+");
                String childParentUri = path.replaceFirst(partialUriReplacer + "$", "");
                for (ObjectType itemToUpdate : output.values()) {
                    if (itemToUpdate.getPaths().stream()
                            .anyMatch(itemToUpdateUri -> itemToUpdateUri.equals(childParentUri))) {
                        itemToUpdate.getChildren().add(item.getName());
                    }
                }
            }
        }

        for (Map.Entry<String, ObjectType> item : output.entrySet()) {

            if (item.getValue().getType().equals("plural")) {
                Set<String> children = item.getValue().getChildren();
                // should only be one
                if (!children.isEmpty()) {
                    item.getValue().setAdditionalName(children.iterator().next());
                }
            }
            Set<String> pluralChildren = new HashSet<>();
            for (String child : item.getValue().getChildren()) {
                if (output.get(child).getType().equals("plural")) {
                    Set<String> children = output.get(child).getChildren();
                    pluralChildren.addAll(children);
                }
            }
            item.getValue().getChildren().addAll(pluralChildren);

            if (item.getValue().getType().equals("plural")) {
                for (String child : item.getValue().getChildren()) {
                    output.get(child)
                            .setPartialUri(item.getValue().getPartialUri() + output.get(child).getPartialUri());
                }
            }

            if (!item.getValue().getFields().isEmpty()) {
                Matcher templates = Patterns.urlTemplatePattern.matcher(item.getValue().getPartialUri());
                List<String> localFields = new ArrayList<>();
                while (templates.find()) {
                    localFields.add(templates.group(2));
                }
                item.getValue().setFields(item.getValue().getFields().stream()
                        .filter(f -> localFields.contains(f.getName())).collect(Collectors.toList()));
            }
        }

        output.values().stream().filter(item -> item.getType().equals("plural"))
                .forEach(item -> item.getChildren().clear());

        log.debug(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(output));

        return output;
    }
}