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.graphgenerator;
23 import guru.nidi.graphviz.attribute.*;
24 import guru.nidi.graphviz.engine.Format;
25 import guru.nidi.graphviz.engine.Graphviz;
26 import guru.nidi.graphviz.model.Factory;
27 import guru.nidi.graphviz.model.MutableGraph;
28 import guru.nidi.graphviz.model.MutableNode;
29 import lombok.extern.slf4j.Slf4j;
30 import org.oran.smo.teiv.pgsqlgenerator.Attribute;
31 import org.oran.smo.teiv.pgsqlgenerator.Entity;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.stereotype.Component;
37 import java.io.IOException;
38 import java.util.List;
40 import static guru.nidi.graphviz.attribute.Rank.RankDir;
44 public class EntityGraphGenerator {
46 @Value("${graphs.generate}")
47 private boolean generateEntityGraph;
49 @Value("${graphs.output}")
50 private String graphOutput;
53 private final HelperFunctions helperFunctions = new HelperFunctions();
55 public void generate(List<Entity> entities) throws IOException {
56 if (generateEntityGraph) {
57 List<String> moduleNames = entities.stream().map(Entity::getModuleReferenceName).distinct().toList();
58 for (String moduleName : moduleNames) {
59 List<Entity> moduleEntities = entities.stream().filter(entity -> moduleName.equals(entity
60 .getModuleReferenceName())).toList();
61 generateGraph(moduleName, moduleEntities);
64 log.info("graphs.generate set to false");
68 private void generateGraph(String name, List<Entity> entities) throws IOException {
69 MutableGraph g = prepareGraph(entities, name);
70 File outputFile = new File(graphOutput, name);
71 Graphviz.fromGraph(g).render(Format.SVG).toFile(outputFile);
72 Graphviz.fromGraph(g).render(Format.DOT).toFile(outputFile);
73 log.info("Graph rendered to: {}", outputFile.getAbsolutePath());
76 private MutableGraph prepareGraph(List<Entity> moduleEntities, String moduleName) {
77 Color fillColour = Color.rgba(helperFunctions.getNodeFillColour(moduleName)).fill();
78 MutableGraph g = Factory.mutGraph(moduleName).setDirected(true).graphAttrs().add(Rank.dir(RankDir.LEFT_TO_RIGHT))
79 .nodeAttrs().add(Shape.RECT, Style.BOLD, Color.BLACK, Style.FILLED, fillColour, Font.name("Arial"));
80 MutableNode moduleNameNode = Factory.mutNode(moduleName).attrs().add(Color.LIGHTBLUE.fill());
81 g.add(moduleNameNode);
82 for (Entity moduleEntity : moduleEntities) {
83 MutableNode moduleNode = Factory.mutNode(moduleEntity.getEntityName());
85 g.add(moduleNameNode.addLink(Factory.to(moduleNode)));
86 List<Attribute> attributes = moduleEntity.getAttributes();
87 if (!attributes.isEmpty()) {
88 addAttributeNodeToGraph(g, attributes, moduleEntity, moduleNode);
94 private void addAttributeNodeToGraph(MutableGraph graph, List<Attribute> attributes, Entity moduleEntity,
95 MutableNode moduleNode) {
96 String label = "<TABLE border='1' cellborder='0' cellspacing='0' cellpadding='4'>";
97 for (Attribute attribute : attributes) {
98 label = label.concat("<TR> <TD bgcolor='#EEEEEE' align='left'>" + attribute
99 .getName() + "</TD> <TD align='right' bgcolor='#EEEEEE'>" + helperFunctions.escapeHtml(attribute
100 .getYangDataType()) + "</TD> </TR>");
102 label = label.concat("</TABLE>");
103 MutableNode attributeNode = Factory.mutNode(moduleEntity.getEntityName() + "-attributes").attrs().add(Label.html(
105 graph.add(attributeNode);
106 graph.add(moduleNode.addLink(attributeNode));