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