81845ca8395d96e22cd2f09ad899ddaf465ecdd8
[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.schema.data;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.nio.file.StandardCopyOption;
29 import java.nio.file.StandardOpenOption;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.oran.smo.teiv.pgsqlgenerator.schema.BackwardCompatibilityChecker;
34 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaParser;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.core.io.ClassPathResource;
37 import org.springframework.stereotype.Component;
38
39 import org.oran.smo.teiv.pgsqlgenerator.Entity;
40 import org.oran.smo.teiv.pgsqlgenerator.Module;
41 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
42 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
43 import org.oran.smo.teiv.pgsqlgenerator.Table;
44 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaGenerator;
45 import lombok.RequiredArgsConstructor;
46 import lombok.extern.slf4j.Slf4j;
47
48 @Component
49 @Slf4j
50 @RequiredArgsConstructor
51 public class DataSchemaGenerator extends SchemaGenerator {
52     @Value("${schema.data.baseline}")
53     private String baselineDataSchema;
54     @Value("${schema.data.output}")
55     private String dataSchemaFileName;
56     @Value("${schema.data.skeleton}")
57     private String skeletonDataSchema;
58     @Value("${green-field-installation}")
59     private boolean isGreenFieldInstallation;
60
61     private final ModelComparator modelComparator;
62     private final DataSchemaHelper dataSchemaHelper;
63     private final TableBuilder tableBuilder;
64     private final BackwardCompatibilityChecker backwardCompatibilityChecker;
65
66     @Override
67     protected void prepareSchema() {
68         try {
69             File baselineDataSchemaFile = new File(baselineDataSchema);
70             File newDataSchema = new File(dataSchemaFileName);
71             if (isGreenFieldInstallation || !baselineDataSchemaFile.exists()) {
72                 log.info("Baseline DOES NOT EXISTS!!");
73                 File skeletonSql = new ClassPathResource(skeletonDataSchema).getFile();
74                 Files.copy(skeletonSql.toPath(), newDataSchema.toPath(), StandardCopyOption.REPLACE_EXISTING);
75             } else {
76                 log.info("Baseline EXISTS!!");
77                 Path sourcePath = baselineDataSchemaFile.toPath();
78                 List<String> stmts = Files.readAllLines(sourcePath);
79                 List<String> commitExcludedStmts = stmts.stream().filter(line -> !line.equals("COMMIT;")).toList();
80
81                 Files.write(Paths.get(dataSchemaFileName), commitExcludedStmts, StandardOpenOption.CREATE,
82                         StandardOpenOption.TRUNCATE_EXISTING);
83             }
84             this.schema = newDataSchema;
85         } catch (IOException exception) {
86             throw PgSchemaGeneratorException.prepareBaselineException("ties.data", exception);
87         }
88     }
89
90     @Override
91     protected void setSqlStatements(List<Module> modules, List<Entity> entities, List<Relationship> relationships) {
92         // Create table from entities and relationships
93         List<Table> tablesFromModelSvc = tableBuilder.getTables(entities, relationships);
94         // Get tables from baseline sql
95         List<Table> tablesFromBaselineSql = isGreenFieldInstallation ?
96                 List.of() :
97                 SchemaParser.extractDataFromBaseline(baselineDataSchema);
98         // Check for NBCs
99         backwardCompatibilityChecker.checkForNBCChangesInData(tablesFromBaselineSql, tablesFromModelSvc);
100         // Compare and store differences in tables from baseline sql with tables from model service
101         Map<String, List<Table>> differences = modelComparator.identifyDifferencesInBaselineAndGenerated(tablesFromModelSvc,
102                 tablesFromBaselineSql);
103         // Generate schema from differences
104         StringBuilder generatedSchema = dataSchemaHelper.generateSchemaFromDifferences(differences);
105         generatedSchema.append("\nCOMMIT;\n");
106         this.sqlStatements = generatedSchema.toString();
107     }
108
109 }