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