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