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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.teiv.pgsqlgenerator.graphgenerator;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.util.List;
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;
37 public class EntityGraphGeneratorUml {
39 @Value("${graphs.generate}")
40 private boolean generateEntityGraph;
42 @Value("${graphs.output}")
43 private String graphOutput;
46 private final HelperFunctions helperFunctions = new HelperFunctions();
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);
57 log.info("graphs.generate set to false");
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);
67 log.info("PUML generated at: {}", outputFile.getAbsolutePath());
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");
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()));
85 sb.append(String.format("\"%s\" --> %s\n", moduleName, entity.getEntityName()));
87 sb.append("@enduml\n");