6c564d6ad4224bd6597cd1131b22175c6fe780be
[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         log.info("Graph rendered to: {}", outputFile.getAbsolutePath());
69     }
70
71     private MutableGraph prepareGraph(List<Entity> moduleEntities, String moduleName) {
72         MutableGraph g = Factory.mutGraph(moduleName).setDirected(true).graphAttrs().add(Rank.dir(RankDir.LEFT_TO_RIGHT))
73                 .nodeAttrs().add(Shape.RECT, Style.BOLD, Color.BLACK, Style.FILLED, Color.LIGHTGRAY.fill());
74         MutableNode moduleNameNode = Factory.mutNode(moduleName).attrs().add(Color.LIGHTBLUE.fill());
75         g.add(moduleNameNode);
76         for (Entity moduleEntity : moduleEntities) {
77             MutableNode moduleNode = Factory.mutNode(moduleEntity.getEntityName());
78             g.add(moduleNode);
79             g.add(moduleNameNode.addLink(Factory.to(moduleNode)));
80             List<Attribute> attributes = moduleEntity.getAttributes();
81             if (!attributes.isEmpty()) {
82                 addAttributeNodeToGraph(g, attributes, moduleEntity, moduleNode);
83             }
84         }
85         return g;
86     }
87
88     private void addAttributeNodeToGraph(MutableGraph graph, List<Attribute> attributes, Entity moduleEntity,
89             MutableNode moduleNode) {
90         String label = "<TABLE border='1' cellborder='0' cellspacing='0' cellpadding='4'>";
91         for (Attribute attribute : attributes) {
92             label = label.concat("<TR> <TD bgcolor='#EEEEEE' align='left'>" + attribute
93                     .getName() + "</TD> <TD align='right' bgcolor='#EEEEEE'>" + attribute
94                             .getYangDataType() + "</TD> </TR>");
95         }
96         label = label.concat("</TABLE>");
97         MutableNode attributeNode = Factory.mutNode(moduleEntity.getEntityName() + "-attributes").attrs().add(Label.html(
98                 label));
99         graph.add(attributeNode);
100         graph.add(moduleNode.addLink(attributeNode));
101     }
102 }