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.relationshipgrapghgenerator;
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;
36 import java.io.IOException;
37 import java.util.List;
41 public class RelationshipGraphGenerator {
43 @Value("${relationship-graph.generate}")
44 private boolean generateRelationshipGraph;
46 @Value("${relationship-graph.output}")
47 private String graphOutput;
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);
59 generateGraph("overall", relationships, entities);
61 log.info("generate-relationship-graph set to false");
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());
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());
78 for (Relationship moduleRelationship : moduleRelationships) {
79 MutableNode nodeA = Factory.mutNode(moduleRelationship.getASideMOType());
81 MutableNode nodeB = Factory.mutNode(moduleRelationship.getBSideMOType());
83 String label = moduleRelationship.getName().split("_")[1];
84 g.add(nodeA.addLink(Factory.to(nodeB).with(Label.of(label))));