35b6a92708fff8f5953e23a401423b7a30d3a1b2
[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.graphgenerator;
22
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.apache.commons.lang3.StringUtils;
31 import org.oran.smo.teiv.pgsqlgenerator.Attribute;
32 import org.oran.smo.teiv.pgsqlgenerator.Entity;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.stereotype.Component;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.List;
39
40 import static guru.nidi.graphviz.attribute.Rank.RankDir;
41
42 @Component
43 @Slf4j
44 public class EntityGraphGenerator {
45
46     @Value("${graphs.generate}")
47     private boolean generateEntityGraph;
48
49     @Value("${graphs.output}")
50     private String graphOutput;
51
52     public void generate(List<Entity> entities) throws IOException {
53         if (generateEntityGraph) {
54             List<String> moduleNames = entities.stream().map(Entity::getModuleReferenceName).distinct().toList();
55             for (String moduleName : moduleNames) {
56                 List<Entity> moduleEntities = entities.stream().filter(entity -> moduleName.equals(entity
57                         .getModuleReferenceName())).toList();
58                 generateGraph(moduleName, moduleEntities);
59             }
60         } else {
61             log.info("graphs.generate set to false");
62         }
63     }
64
65     private void generateGraph(String name, List<Entity> entities) throws IOException {
66         MutableGraph g = prepareGraph(entities, name);
67         File outputFile = new File(graphOutput, name);
68         Graphviz.fromGraph(g).render(Format.SVG).toFile(outputFile);
69         Graphviz.fromGraph(g).render(Format.DOT).toFile(outputFile);
70         log.info("Graph rendered to: {}", outputFile.getAbsolutePath());
71     }
72
73     private MutableGraph prepareGraph(List<Entity> moduleEntities, String moduleName) {
74         MutableGraph g = Factory.mutGraph(moduleName).setDirected(true).graphAttrs().add(Rank.dir(RankDir.LEFT_TO_RIGHT))
75                 .nodeAttrs().add(Shape.RECT, Style.BOLD, Color.BLACK, Style.FILLED, Color.LIGHTGRAY.fill(), Font.name(
76                         "Arial"));
77         MutableNode moduleNameNode = Factory.mutNode(moduleName).attrs().add(Color.LIGHTBLUE.fill());
78         g.add(moduleNameNode);
79         for (Entity moduleEntity : moduleEntities) {
80             MutableNode moduleNode = Factory.mutNode(moduleEntity.getEntityName());
81             g.add(moduleNode);
82             g.add(moduleNameNode.addLink(Factory.to(moduleNode)));
83             List<Attribute> attributes = moduleEntity.getAttributes();
84             if (!attributes.isEmpty()) {
85                 addAttributeNodeToGraph(g, attributes, moduleEntity, moduleNode);
86             }
87         }
88         return g;
89     }
90
91     private void addAttributeNodeToGraph(MutableGraph graph, List<Attribute> attributes, Entity moduleEntity,
92             MutableNode moduleNode) {
93         String label = "<TABLE border='1' cellborder='0' cellspacing='0' cellpadding='4'>";
94         for (Attribute attribute : attributes) {
95             label = label.concat("<TR> <TD bgcolor='#EEEEEE' align='left'>" + attribute
96                     .getName() + "</TD> <TD align='right' bgcolor='#EEEEEE'>" + escapeHtml(attribute
97                             .getYangDataType()) + "</TD> </TR>");
98         }
99         label = label.concat("</TABLE>");
100         MutableNode attributeNode = Factory.mutNode(moduleEntity.getEntityName() + "-attributes").attrs().add(Label.html(
101                 label));
102         graph.add(attributeNode);
103         graph.add(moduleNode.addLink(attributeNode));
104     }
105
106     private String escapeHtml(String text) {
107         return StringUtils.replaceEach(text, new String[] { "<", ">" }, new String[] { "&lt;", "&gt;" });
108     }
109 }