aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/graphgraph/reader/BasicSchemaReader.java
blob: 0cdbd9c8bab4e1243a69286dc70d155409dedc2d (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
  ============LICENSE_START=======================================================
  org.onap.aai
  ================================================================================
  Copyright © 2019-2020 Orange Intellectual Property. All rights reserved.
  ================================================================================
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  ============LICENSE_END=========================================================
 */
package org.onap.aai.graphgraph.reader;

import com.google.common.collect.Multimap;
import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.alg.shortestpath.FloydWarshallShortestPaths;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.onap.aai.edges.EdgeIngestor;
import org.onap.aai.edges.EdgeRule;
import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
import org.onap.aai.graphgraph.MoxyLoaderRepository;
import org.onap.aai.graphgraph.dto.Edge;
import org.onap.aai.graphgraph.dto.NodeName;
import org.onap.aai.graphgraph.dto.NodeProperty;
import org.onap.aai.graphgraph.dto.Property;
import org.onap.aai.introspection.Introspector;
import org.onap.aai.schema.enums.PropertyMetadata;
import org.onap.aai.setup.SchemaVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.Map.Entry;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BasicSchemaReader implements SchemaReader {

    private static final Logger LOGGER = LoggerFactory.getLogger(BasicSchemaReader.class);

    private Map<String, Introspector> allEntities;
    private Graph<String, MetadataEdge> graph = new DefaultDirectedGraph<>(MetadataEdge.class);
    private EdgeIngestor edgeIngestor;
    private final String version;
    private final List<String> schemaErrors = new LinkedList<>();
    private final MoxyLoaderRepository moxyLoaderRepository;

    public BasicSchemaReader(String version, MoxyLoaderRepository moxyLoaderRepository, EdgeIngestor edgeIngestor) {
        this.version = version;
        this.moxyLoaderRepository = moxyLoaderRepository;
        this.edgeIngestor = edgeIngestor;
    }

    public List<String> getSchemaErrors() {
        return schemaErrors;
    }

    private void initAllEntitiesAndCreateGraph() {
        if (allEntities != null) {
            return;
        }

        try {
            allEntities = moxyLoaderRepository.getMoxyLoaders().get(getSchemaName()).getAllObjects();
            graph = createGraph(true, true);

        } catch (Exception e) {
            LOGGER.error("Failed creation of BasicSchemaReader, version: " + getSchemaName(), e);
        }
    }

    private Graph<String, MetadataEdge> createGraph(boolean withParentChild, boolean withEdgeRules) {
        Graph<String, MetadataEdge> directedGraph = new DefaultDirectedGraph<>(MetadataEdge.class);
        for (Entry<String, Introspector> currentParent : allEntities.entrySet()) {
            directedGraph.addVertex(currentParent.getKey());
            currentParent.getValue().getProperties().stream()
                    .filter(v -> allEntities.containsKey(v))
                    .filter(v -> !currentParent.getKey().equals(v))
                    .forEach(v -> {
                        directedGraph.addVertex(v);
                        if (withParentChild) {
                            addParentChildEdge(currentParent.getKey(), v, directedGraph);
                        }
                    });
        }

        if (!withEdgeRules) {
            return directedGraph;
        }

        Multimap<String, EdgeRule> allRules = null;
        try {
            allRules = edgeIngestor.getAllRules(new SchemaVersion(getSchemaName()));

        } catch (EdgeRuleNotFoundException e) {
            LOGGER.error("Edge rule not found", e);
        }

        Objects.requireNonNull(allRules).asMap().values().stream()
                .flatMap(Collection::stream)
                .forEach(e -> {
                    switch (e.getDirection()) {
                        case OUT:
                            addEdgerule(e.getFrom(), e.getTo(), e.getLabel(), directedGraph);
                            break;
                        case IN:
                            addEdgerule(e.getTo(), e.getFrom(), e.getLabel(), directedGraph);
                            break;
                        case BOTH:
                            addEdgerule(e.getFrom(), e.getTo(), e.getLabel(), directedGraph);
                            addEdgerule(e.getTo(), e.getFrom(), e.getLabel(), directedGraph);
                            break;
                    }
                });

        return directedGraph;
    }

    private void addEdgerule(String parent, String child, String label, Graph<String, MetadataEdge> graph) {
        //shortening labels, long edge names are unreadable in the UI
        if (label.contains(".")) {
            String[] split = label.split("\\.");
            label = split[split.length - 1];
        }
        checkVertexExist(graph, parent);
        checkVertexExist(graph, child);

        graph.addEdge(child, parent,
                new MetadataEdge(EdgeType.EDGE_RULE.getTypeName(), child, parent, label));
    }

    private void checkVertexExist(Graph<String, MetadataEdge> graph, String vertex) {
        if (!graph.vertexSet().contains(vertex)) {
            graph.addVertex(vertex);
            schemaErrors.add(String.format("Schema is inconsistent, missing node %s", vertex));
        }
    }

    private void addParentChildEdge(String parent, String child, Graph<String, MetadataEdge> graph) {
        graph.addEdge(parent, child,
                new MetadataEdge(EdgeType.PARENT.getTypeName(), parent, child, EdgeType.PARENT.getTypeName()));
        graph.addEdge(child, parent,
                new MetadataEdge(EdgeType.CHILD.getTypeName(), child, parent, EdgeType.CHILD.getTypeName()));
    }

    @Override
    public String getSchemaName() {
        return version;
    }

    @Override
    public List<NodeName> getAllVertexNames(String edgeFilter) {
        initAllEntitiesAndCreateGraph();

        return createGraph(
                isParentChildFilter(edgeFilter),
                isEdgeRulesFilter(edgeFilter)).edgeSet().stream()
                .flatMap(e -> Stream.of(e.getSource(), e.getTarget()))
                .sorted().distinct()
                .map(NodeName::new)
                .collect(Collectors.toList());
    }

    @Override
    public List<NodeProperty> getVertexProperties(String nodeName) {
        initAllEntitiesAndCreateGraph();
        if (!allEntities.containsKey(nodeName)) {
            return Collections.emptyList();
        }
        Introspector introspector = allEntities.get(nodeName);
        List<String> properties = introspector.getProperties().stream().sorted().collect(Collectors.toList());

        return properties.stream()
                .map(p -> new NodeProperty(
                        p,
                        introspector.getPropertyMetadata(p).getOrDefault(
                                PropertyMetadata.DESCRIPTION, "no description available"),
                        introspector.getType(p),
                        introspector.getAllKeys().contains(p),
                        introspector.getIndexedProperties().contains(p),
                        introspector.getRequiredProperties().contains(p)))
                .collect(Collectors.toList());
    }

    @Override
    public List<Property> getEdgeProperties(String fromNode, String toNode, String type) {
        initAllEntitiesAndCreateGraph();
        if (type.equals(EdgeType.EDGE_RULE.getTypeName())) {
            try {
                List<EdgeRule> rules = edgeIngestor.getAllRules(new SchemaVersion(getSchemaName()))
                        .asMap().values().stream()
                        .flatMap(Collection::stream)
                        .filter(identifyEdgeRule(fromNode, toNode))
                        .collect(Collectors.toList());

                Optional<List<Property>> properties = rules.stream().map(this::edgeRuleProperties).findFirst();
                return properties.orElse(Collections.emptyList());

            } catch (EdgeRuleNotFoundException e) {
                LOGGER.error("Edge rule not found", e);
            }
        }
        return Collections.emptyList();
    }

    private Predicate<EdgeRule> identifyEdgeRule(String fromNode, String toNode) {
        return e -> {
            switch (e.getDirection()) {
                case OUT:
                    return e.getFrom().equals(fromNode) && e.getTo().equals(toNode);
                case IN:
                    return e.getFrom().equals(toNode) && e.getTo().equals(fromNode);
                case BOTH:
                    return e.getFrom().equals(toNode) && e.getTo().equals(fromNode)
                            || e.getFrom().equals(fromNode) && e.getTo().equals(toNode);
                default:
                    return false;
            }
        };
    }

    private List<Property> edgeRuleProperties(EdgeRule r) {
        List<Property> ps = new LinkedList<>();
        ps.add(new Property("Multiplicity", r.getMultiplicityRule().name()));
        ps.add(new Property("Is default edge", String.valueOf(r.isDefault())));
        ps.add(new Property("Description", r.getDescription()));
        ps.add(new Property("Is private edge", String.valueOf(r.isPrivateEdge())));
        ps.add(new Property("Contains", r.getContains()));
        ps.add(new Property("Prevent delete", r.getPreventDelete()));
        ps.add(new Property("Label", r.getLabel()));
        ps.add(new Property("Delete other v", r.getDeleteOtherV()));
        return ps;
    }

    @Override
    public org.onap.aai.graphgraph.dto.Graph getGraph(
            String initialNode, int parentHops, int cousinHops, int childHops, String edgeFilter) {
        initAllEntitiesAndCreateGraph();

        Optional<String> anyVertex = graph.vertexSet().stream().findFirst();
        if (!anyVertex.isPresent()) {
            return org.onap.aai.graphgraph.dto.Graph.emptyGraph();
        }
        Set<Edge> edges = computeAllEdges(
                anyVertex.get(), isParentChildFilter(edgeFilter), isEdgeRulesFilter(edgeFilter));

        if (!"all".equals(initialNode)) {
            Set<String> subGraphVertices = computeNodes(initialNode, parentHops, EdgeType.CHILD.getTypeName());
            subGraphVertices.addAll(computeNodes(initialNode, childHops, EdgeType.PARENT.getTypeName()));
            subGraphVertices.addAll(computeNodes(initialNode, cousinHops, EdgeType.EDGE_RULE.getTypeName()));
            edges = filterEdges(edges, subGraphVertices);
        }

        return new org.onap.aai.graphgraph.dto.Graph(new LinkedList<>(computeNodeNames(edges)),
                new LinkedList<>(edges), Collections.emptyList(), getVertexProperties(initialNode));
    }

    private boolean isParentChildFilter(String edgeFilter) {
        return "Parents".equals(edgeFilter);
    }

    private boolean isEdgeRulesFilter(String edgeFilter) {
        return "Edgerules".equals(edgeFilter);
    }

    private Set<Edge> filterEdgesStrict(Set<Edge> edges, Set<String> subGraphVertices) {
        return edges.stream()
                .filter(e -> subGraphVertices.contains(e.getSource()) && subGraphVertices.contains(e.getTarget()))
                .collect(Collectors.toSet());
    }

    private Set<Edge> filterEdges(Set<Edge> edges, Set<String> subGraphVertices) {
        return edges.stream()
                .filter(e -> subGraphVertices.contains(e.getSource()) || subGraphVertices.contains(e.getTarget()))
                .filter(e -> !e.getType().equals(EdgeType.EDGE_RULE.getTypeName()))
                .collect(Collectors.toSet());
    }

    private Set<NodeName> computeNodeNames(Set<Edge> edges) {
        return edges.stream()
                .flatMap(e -> e.getNodeNames().stream())
                .collect(Collectors.toSet());
    }

    private Set<Edge> computeAllEdges(String initial, boolean parentChild, boolean edgeRules) {
        Set<Edge> result = new HashSet<>();
        List<String> toQuery = new LinkedList<>();
        toQuery.add(initial);
        final List<String> toVisit = new LinkedList<>();
        Set<String> visited = new HashSet<>();

        while (!toQuery.isEmpty()) {
            for (String v : toQuery) {
                visited.add(v);

                graph.edgesOf(v).forEach(edge -> {
                    String neighbour = edge.getTarget();
                    toVisit.add(neighbour);

                    if (EdgeType.CHILD.isType(edge.getType()) && parentChild) {
                        result.add(new Edge(
                                neighbour, edge.getSource(), EdgeType.PARENT.getTypeName(), createTooltip(
                                neighbour, edge.getSource(), EdgeType.PARENT.getTypeName(), edge.getLabel())));
                    }

                    if (EdgeType.EDGE_RULE.isType(edge.getType()) && edgeRules) {
                        result.add(new Edge(
                                edge.getSource(), neighbour, edge.getLabel(), createTooltip(
                                neighbour, edge.getSource(), EdgeType.EDGE_RULE.getTypeName(), edge.getLabel())));
                    }
                });
            }
            toQuery.clear();
            toQuery.addAll(toVisit.stream().filter(s -> !visited.contains(s)).collect(Collectors.toList()));
        }

        return result;

    }

    private Set<String> computeNodes(String vertex, int hops, String relationshipName) {
        List<String> toQuery = new LinkedList<>();
        toQuery.add(vertex);

        Set<String> visited = new HashSet<>();
        int i = 0;

        final List<String> toVisit = new LinkedList<>();
        while (!toQuery.isEmpty() && hops > i) {
            i++;
            toVisit.clear();

            for (String v : toQuery) {
                visited.add(v);
                graph.edgesOf(v).stream()
                        .filter(e -> e.getType().equals(relationshipName))
                        .map(MetadataEdge::getTarget)
                        .forEach(toVisit::add);
            }

            toQuery.clear();
            toQuery.addAll(toVisit.stream().filter(v -> !visited.contains(v)).collect(Collectors.toList()));
        }

        return visited;
    }

    @Override
    public org.onap.aai.graphgraph.dto.Graph getGraph(String fromNode, String toNode, String edgeFilter) {
        initAllEntitiesAndCreateGraph();
        Graph<String, MetadataEdge> tempGraph = createGraph(
                isParentChildFilter(edgeFilter), isEdgeRulesFilter(edgeFilter));
        List<List<NodeName>> paths = new LinkedList<>();
        FloydWarshallShortestPaths<String, MetadataEdge> shortestPaths;

        while (true) {
            shortestPaths = new FloydWarshallShortestPaths<>(tempGraph);
            GraphPath<String, MetadataEdge> p = shortestPaths.getPath(fromNode, toNode);
            if (p == null || p.getEdgeList() == null || p.getEdgeList().isEmpty()) {
                break;
            }
            String previous = fromNode;
            List<NodeName> path = new LinkedList<>();
            for (MetadataEdge e : p.getEdgeList()) {
                if (e.getTarget().equals(previous)) {
                    previous = e.getSource();
                    path.add(new NodeName(e.getTarget()));
                } else {
                    previous = e.getTarget();
                    path.add(new NodeName(e.getSource()));
                }
            }
            path.add(new NodeName(previous));
            paths.add(path);
            tempGraph.removeEdge(p.getEdgeList().get(p.getLength() - 1)); //remove last edge from path
        }

        Set<Edge> edges = computeAllEdges(fromNode, isParentChildFilter(edgeFilter), isEdgeRulesFilter(edgeFilter));
        edges = filterEdgesStrict(
                edges,
                paths.stream()
                        .flatMap(Collection::stream)
                        .map(NodeName::getId)
                        .collect(Collectors.toSet()));
        return new org.onap.aai.graphgraph.dto.Graph(
                new LinkedList<>(computeNodeNames(edges)), new LinkedList<>(edges),
                paths, getVertexProperties(fromNode));
    }

    private List<Property> createTooltip(String target, String v, String type, String label) {
        List<Property> properties = new LinkedList<>();
        properties.add(new Property("From", target));
        properties.add(new Property("To", v));
        properties.add(new Property("Type", type));
        properties.add(new Property("Relationship", label));
        properties.addAll(getEdgeProperties(
                v, target,
                EdgeType.EDGE_RULE.isType(type) ? EdgeType.EDGE_RULE.getTypeName() : EdgeType.PARENT.getTypeName()));
        return properties;
    }
}