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.grapghgenerator;
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.Value;
31 import org.springframework.stereotype.Component;
32 import lombok.extern.slf4j.Slf4j;
36 public class EntityGraphGeneratorUml {
38 @Value("${graphs.generate}")
39 private boolean generateEntityGraph;
41 @Value("${graphs.output}")
42 private String graphOutput;
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);
53 log.info("graphs.generate set to false");
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);
63 log.info("PUML generated at: {}", outputFile.getAbsolutePath());
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");
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()));
81 sb.append(String.format("\"%s\" --> %s\n", moduleName, entity.getEntityName()));
83 sb.append("@enduml\n");