276edf5ca551f666e4a20700b7853c9d9252d683
[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 java.io.File;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.util.List;
27
28 import org.oran.smo.teiv.pgsqlgenerator.Attribute;
29 import org.oran.smo.teiv.pgsqlgenerator.Entity;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.stereotype.Component;
33 import lombok.extern.slf4j.Slf4j;
34
35 @Component
36 @Slf4j
37 public class EntityGraphGeneratorUml {
38
39     @Value("${graphs.generate}")
40     private boolean generateEntityGraph;
41
42     @Value("${graphs.output}")
43     private String graphOutput;
44
45     @Autowired
46     private final HelperFunctions helperFunctions = new HelperFunctions();
47
48     public void generate(List<Entity> entities) throws IOException {
49         if (generateEntityGraph) {
50             List<String> moduleNames = entities.stream().map(Entity::getModuleReferenceName).distinct().toList();
51             for (String moduleName : moduleNames) {
52                 List<Entity> moduleEntities = entities.stream().filter(entity -> moduleName.equals(entity
53                         .getModuleReferenceName())).toList();
54                 generateGraph(moduleName, moduleEntities);
55             }
56         } else {
57             log.info("graphs.generate set to false");
58         }
59     }
60
61     private void generateGraph(String name, List<Entity> entities) throws IOException {
62         String plantUmlSource = prepareGraphSource(name, entities);
63         File outputFile = new File(graphOutput, name + ".puml");
64         try (PrintWriter writer = new PrintWriter(outputFile)) {
65             writer.write(plantUmlSource);
66         }
67         log.info("PUML generated at: {}", outputFile.getAbsolutePath());
68     }
69
70     private String prepareGraphSource(String moduleName, List<Entity> entities) {
71         StringBuilder sb = new StringBuilder();
72         sb.append("@startuml\n");
73         sb.append("skinparam class {\n");
74         sb.append("    BackgroundColor<<Entity>> " + helperFunctions.getNodeFillColour(moduleName) + " \n");
75         sb.append("    BackgroundColor<<Module>> LightBlue\n");
76         sb.append("}\n");
77         sb.append(String.format("class %s <<Module>> {%n}%n", moduleName));
78         for (Entity entity : entities) {
79             sb.append(String.format("class %s <<Entity>> {%n", entity.getEntityName()));
80             List<Attribute> attributes = entity.getAttributes();
81             for (Attribute attribute : attributes) {
82                 sb.append(String.format("    %s : %s\n", attribute.getName(), attribute.getYangDataType()));
83             }
84             sb.append("}\n");
85             sb.append(String.format("\"%s\" --> %s\n", moduleName, entity.getEntityName()));
86         }
87         sb.append("@enduml\n");
88         return sb.toString();
89     }
90 }