f5a42110af14fe5f82e591e0f66c8942bb1b1c67
[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.Entity;
29 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
30 import org.springframework.beans.factory.annotation.Value;
31 import org.springframework.stereotype.Component;
32
33 import lombok.extern.slf4j.Slf4j;
34
35 @Component
36 @Slf4j
37 public class RelationshipGraphGeneratorUml {
38
39     @Value("${graphs.generate}")
40     private boolean generateRelationshipGraph;
41
42     @Value("${graphs.output}")
43     private String graphOutput;
44
45     public void generate(List<Relationship> relationships, List<Entity> entities) throws IOException {
46         if (generateRelationshipGraph) {
47             List<String> moduleNames = relationships.stream().map(Relationship::getModuleReferenceName).distinct().toList();
48             for (String moduleName : moduleNames) {
49                 List<Relationship> moduleRelationships = relationships.stream().filter(relationship -> moduleName.equals(
50                         relationship.getModuleReferenceName())).toList();
51                 List<Entity> moduleEntities = entities.stream().filter(entity -> moduleName.equals(entity
52                         .getModuleReferenceName())).toList();
53                 generateGraph(moduleName, moduleRelationships, moduleEntities);
54             }
55             generateGraph("overall", relationships, entities);
56         } else {
57             log.info("graphs.generate set to false");
58         }
59     }
60
61     private void generateGraph(String name, List<Relationship> relationships, List<Entity> entities) throws IOException {
62         String plantUmlSource = prepareGraph(relationships, entities);
63         File outputFile = new File(graphOutput, name + "-rel.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 prepareGraph(List<Relationship> relationships, List<Entity> entities) {
71         StringBuilder sb = new StringBuilder();
72         sb.append("@startuml\n");
73         sb.append("skinparam componentStyle rectangle\n");
74         for (Entity entity : entities) {
75             sb.append(String.format("class %s {\n", entity.getEntityName()));
76             sb.append("}\n");
77         }
78         for (Relationship relationship : relationships) {
79             String label = relationship.getName().split("_")[1];
80             String aSideCardinality = getCardinality(relationship.getASideMinCardinality(), relationship
81                     .getASideMaxCardinality());
82             String bSideCardinality = getCardinality(relationship.getBSideMinCardinality(), relationship
83                     .getBSideMaxCardinality());
84
85             sb.append(String.format("%s \"%s\" --> \"%s\" %s : %s\n", relationship.getASideMOType(), aSideCardinality,
86                     bSideCardinality, relationship.getBSideMOType(), label));
87         }
88         sb.append("@enduml\n");
89         return sb.toString();
90     }
91
92     private String getCardinality(long minCardinality, long maxCardinality) {
93         return formatCardinality(minCardinality) + ".." + formatCardinality(maxCardinality);
94     }
95
96     private String formatCardinality(long cardinality) {
97         return cardinality == Long.MAX_VALUE ? "*" : String.valueOf(cardinality);
98     }
99 }