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