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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.teiv.pgsqlgenerator.grapghgenerator;
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;
40 import java.io.IOException;
41 import java.util.List;
45 public class RelationshipGraphGenerator {
47 @Value("${graphs.generate}")
48 private boolean generateRelationshipGraph;
50 @Value("${graphs.output}")
51 private String graphOutput;
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);
63 generateGraph("overall", relationships, entities);
65 log.info("graphs.generate set to false");
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());
76 private MutableGraph prepareGraph(List<Relationship> moduleRelationships, List<Entity> moduleEntities) {
77 MutableGraph g = Factory.mutGraph("moduleName").setDirected(true).linkAttrs().add(Color.DARKSLATEGRAY4).nodeAttrs()
79 for (Entity moduleEntity : moduleEntities) {
80 MutableNode node = Factory.mutNode(moduleEntity.getEntityName());
83 for (Relationship moduleRelationship : moduleRelationships) {
84 MutableNode nodeA = Factory.mutNode(moduleRelationship.getASideMOType());
86 MutableNode nodeB = Factory.mutNode(moduleRelationship.getBSideMOType());
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()));
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)));
101 private String getCardinality(long minCardinality, long maxCardinality) {
102 return formatCardinality(minCardinality) + ".." + formatCardinality(maxCardinality);
105 private String formatCardinality(long cardinality) {
106 if (cardinality == Long.MAX_VALUE) {
109 return String.valueOf(cardinality);