0cd60fdfc31d655e81d5e3d843d67a355b2ebcd6
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.teiv.pgsqlgenerator.relationshipgrapghgenerator;
22
23 import guru.nidi.graphviz.model.Factory;
24 import guru.nidi.graphviz.model.MutableGraph;
25 import guru.nidi.graphviz.model.MutableNode;
26 import lombok.extern.slf4j.Slf4j;
27 import guru.nidi.graphviz.attribute.Label;
28 import guru.nidi.graphviz.engine.Format;
29 import guru.nidi.graphviz.engine.Graphviz;
30 import org.oran.smo.teiv.pgsqlgenerator.Entity;
31 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.stereotype.Component;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.List;
38
39 @Component
40 @Slf4j
41 public class RelationshipGraphGenerator {
42
43     @Value("${relationship-graph.generate}")
44     private boolean generateRelationshipGraph;
45
46     @Value("${relationship-graph.output}")
47     private String graphOutput;
48
49     public void generate(List<Relationship> relationships, List<Entity> entities) throws IOException {
50         if (generateRelationshipGraph) {
51             List<String> moduleNames = relationships.stream().map(Relationship::getModuleReferenceName).distinct().toList();
52             for (String moduleName : moduleNames) {
53                 List<Relationship> moduleRelationships = relationships.stream().filter(relationship -> moduleName.equals(
54                         relationship.getModuleReferenceName())).toList();
55                 List<Entity> moduleEntities = entities.stream().filter(entity -> moduleName.equals(entity
56                         .getModuleReferenceName())).toList();
57                 generateGraph(moduleName, moduleRelationships, moduleEntities);
58             }
59             generateGraph("overall", relationships, entities);
60         } else {
61             log.info("generate-relationship-graph set to false");
62         }
63     }
64
65     private void generateGraph(String name, List<Relationship> relationships, List<Entity> entities) throws IOException {
66         MutableGraph g = prepareGraph(relationships, entities);
67         File outputFile = new File(graphOutput, name);
68         Graphviz.fromGraph(g).render(Format.SVG).toFile(outputFile);
69         log.info("Graph rendered to: {}", outputFile.getAbsolutePath());
70     }
71
72     private MutableGraph prepareGraph(List<Relationship> moduleRelationships, List<Entity> moduleEntities) {
73         MutableGraph g = Factory.mutGraph("moduleName").setDirected(false);
74         for (Entity moduleEntity : moduleEntities) {
75             MutableNode node = Factory.mutNode(moduleEntity.getEntityName());
76             g.add(node);
77         }
78         for (Relationship moduleRelationship : moduleRelationships) {
79             MutableNode nodeA = Factory.mutNode(moduleRelationship.getASideMOType());
80             g.add(nodeA);
81             MutableNode nodeB = Factory.mutNode(moduleRelationship.getBSideMOType());
82             g.add(nodeB);
83             String label = moduleRelationship.getName().split("_")[1];
84             g.add(nodeA.addLink(Factory.to(nodeB).with(Label.of(label))));
85         }
86         return g;
87     }
88 }