31541f73331c5013a1b1b7acbc0056adeabf1939
[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.grapghgenerator;
22
23 import guru.nidi.graphviz.attribute.Arrow;
24 import guru.nidi.graphviz.attribute.Attributes;
25 import guru.nidi.graphviz.attribute.Color;
26 import guru.nidi.graphviz.attribute.EndLabel;
27 import guru.nidi.graphviz.attribute.Font;
28 import guru.nidi.graphviz.attribute.ForAll;
29 import guru.nidi.graphviz.attribute.Shape;
30 import guru.nidi.graphviz.model.Factory;
31 import guru.nidi.graphviz.model.MutableGraph;
32 import guru.nidi.graphviz.model.MutableNode;
33 import lombok.extern.slf4j.Slf4j;
34 import guru.nidi.graphviz.attribute.Label;
35 import guru.nidi.graphviz.engine.Format;
36 import guru.nidi.graphviz.engine.Graphviz;
37 import org.oran.smo.teiv.pgsqlgenerator.Entity;
38 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.stereotype.Component;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.util.List;
45
46 @Component
47 @Slf4j
48 public class RelationshipGraphGenerator {
49
50     @Value("${graphs.generate}")
51     private boolean generateRelationshipGraph;
52
53     @Value("${graphs.output}")
54     private String graphOutput;
55
56     public void generate(List<Relationship> relationships, List<Entity> entities) throws IOException {
57         if (generateRelationshipGraph) {
58             List<String> moduleNames = relationships.stream().map(Relationship::getModuleReferenceName).distinct().toList();
59             for (String moduleName : moduleNames) {
60                 List<Relationship> moduleRelationships = relationships.stream().filter(relationship -> moduleName.equals(
61                         relationship.getModuleReferenceName())).toList();
62                 List<Entity> moduleEntities = entities.stream().filter(entity -> moduleName.equals(entity
63                         .getModuleReferenceName())).toList();
64                 generateGraph(moduleName, moduleRelationships, moduleEntities);
65             }
66             generateGraph("overall", relationships, entities);
67         } else {
68             log.info("graphs.generate set to false");
69         }
70     }
71
72     private void generateGraph(String name, List<Relationship> relationships, List<Entity> entities) throws IOException {
73         MutableGraph g = prepareGraph(relationships, entities);
74         File outputFile = new File(graphOutput, name + "-rel");
75         Graphviz.fromGraph(g).render(Format.SVG).toFile(outputFile);
76         Graphviz.fromGraph(g).render(Format.DOT).toFile(outputFile);
77         log.info("Graph rendered to: {}", outputFile.getAbsolutePath());
78     }
79
80     private MutableGraph prepareGraph(List<Relationship> moduleRelationships, List<Entity> moduleEntities) {
81         Attributes<ForAll> arialFont = Font.name("Arial");
82         MutableGraph g = Factory.mutGraph("moduleName").setDirected(true).linkAttrs().add(Color.DARKSLATEGRAY4, arialFont)
83                 .nodeAttrs().add(Shape.BOX, arialFont);
84         for (Entity moduleEntity : moduleEntities) {
85             MutableNode node = Factory.mutNode(moduleEntity.getEntityName());
86             g.add(node);
87         }
88         for (Relationship moduleRelationship : moduleRelationships) {
89             MutableNode nodeA = Factory.mutNode(moduleRelationship.getASideMOType());
90             g.add(nodeA);
91             MutableNode nodeB = Factory.mutNode(moduleRelationship.getBSideMOType());
92             g.add(nodeB);
93
94             String label = moduleRelationship.getName().split("_")[1];
95             Label aSideCardinality = Label.of(getCardinality(moduleRelationship.getASideMinCardinality(), moduleRelationship
96                     .getASideMaxCardinality()));
97             Label bSideCardinality = Label.of(getCardinality(moduleRelationship.getBSideMinCardinality(), moduleRelationship
98                     .getBSideMaxCardinality()));
99
100             g.add(nodeA.addLink(Factory.to(nodeB).with(Label.of(label), EndLabel.head(aSideCardinality, null, null),
101                     EndLabel.tail(bSideCardinality, null, null), Arrow.VEE)));
102         }
103         return g;
104     }
105
106     private String getCardinality(long minCardinality, long maxCardinality) {
107         return formatCardinality(minCardinality) + ".." + formatCardinality(maxCardinality);
108     }
109
110     private String formatCardinality(long cardinality) {
111         if (cardinality == Long.MAX_VALUE) {
112             return "*";
113         } else {
114             return String.valueOf(cardinality);
115         }
116     }
117 }