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.schema.data;
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;
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;
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;
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;
62 private final ModelComparator modelComparator;
63 private final DataSchemaHelper dataSchemaHelper;
64 private final TableBuilder tableBuilder;
65 private final BackwardCompatibilityChecker backwardCompatibilityChecker;
68 protected void prepareSchema() {
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);
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;")) {
85 analyzeAndCommitExcludedStmts.add(line);
88 Files.write(Paths.get(dataSchemaFileName), analyzeAndCommitExcludedStmts, StandardOpenOption.CREATE,
89 StandardOpenOption.TRUNCATE_EXISTING);
91 this.schema = newDataSchema;
92 } catch (IOException exception) {
93 throw PgSchemaGeneratorException.prepareBaselineException("ties.data", exception);
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 ?
104 SchemaParser.extractDataFromBaseline(baselineDataSchema);
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();
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
121 return analyzeTableStmt;