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.StandardOpenOption;
29 import java.util.ArrayList;
30 import java.util.List;
32 import java.util.stream.Stream;
34 import org.oran.smo.teiv.pgsqlgenerator.schema.BackwardCompatibilityChecker;
35 import org.oran.smo.teiv.pgsqlgenerator.FileHelper;
36 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaParser;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.core.io.ClassPathResource;
39 import org.springframework.core.io.Resource;
40 import org.springframework.stereotype.Component;
42 import org.oran.smo.teiv.pgsqlgenerator.Entity;
43 import org.oran.smo.teiv.pgsqlgenerator.Module;
44 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
45 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
46 import org.oran.smo.teiv.pgsqlgenerator.Table;
47 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaGenerator;
48 import lombok.RequiredArgsConstructor;
49 import lombok.extern.slf4j.Slf4j;
53 @RequiredArgsConstructor
54 public class DataSchemaGenerator extends SchemaGenerator {
55 @Value("${schema.data.baseline}")
56 private String baselineDataSchema;
57 @Value("${schema.data.output}")
58 private String dataSchemaFileName;
59 @Value("${schema.data.skeleton}")
60 private String skeletonDataSchema;
61 @Value("${green-field-installation}")
62 private boolean isGreenFieldInstallation;
63 @Value("${custom-query-execution}")
64 private boolean isCustomQueryExecutionEnabled;
65 @Value("${schema.data.custom-sql-script}")
66 private String customSqlScripts;
68 private final ModelComparator modelComparator;
69 private final DataSchemaHelper dataSchemaHelper;
70 private final TableBuilder tableBuilder;
71 private final BackwardCompatibilityChecker backwardCompatibilityChecker;
74 protected void prepareSchema() {
76 Resource baselineResource = new ClassPathResource(baselineDataSchema);
77 File newDataSchema = new File(dataSchemaFileName);
78 if (isGreenFieldInstallation || !baselineResource.exists()) {
79 log.info("Baseline DOES NOT EXISTS!!");
80 Resource skeletonResource = new ClassPathResource(skeletonDataSchema);
81 FileHelper.copyResourceToFile(skeletonResource, newDataSchema);
83 log.info("Baseline EXISTS!!");
84 Path sourcePath = baselineResource.getFile().toPath();
85 List<String> stmts = Files.readAllLines(sourcePath);
86 List<String> analyzeAndCommitExcludedStmts = new ArrayList<>();
87 for (String line : stmts) {
88 if (line.startsWith("ANALYZE") || line.equals("COMMIT;")) {
91 analyzeAndCommitExcludedStmts.add(line);
94 Files.write(Paths.get(dataSchemaFileName), analyzeAndCommitExcludedStmts, StandardOpenOption.CREATE,
95 StandardOpenOption.TRUNCATE_EXISTING);
97 this.schema = newDataSchema;
98 } catch (IOException exception) {
99 throw PgSchemaGeneratorException.prepareBaselineException("ties.data", exception);
104 protected void setSqlStatements(List<Module> modules, List<Entity> entities, List<Relationship> relationships) {
105 // Create table from entities and relationships
106 List<Table> tablesFromModelSvc = tableBuilder.getTables(entities, relationships);
107 List<Table> tablesForNbcCheck = new ArrayList<>();
109 Resource skeletonResource = new ClassPathResource(skeletonDataSchema);
110 File tmpSkeletonFile = new File("target/tmpSkeletonData.sql");
111 FileHelper.copyResourceToFile(skeletonResource, tmpSkeletonFile);
112 List<Table> tablesFromSkeleton = SchemaParser.extractDataFromBaseline(tmpSkeletonFile.getAbsolutePath());
113 tablesForNbcCheck.addAll(tablesFromSkeleton);
114 } catch (IOException exception) {
115 throw PgSchemaGeneratorException.prepareBaselineException("ties.data", exception);
117 // Get tables from baseline sql
118 List<Table> tablesFromBaselineSql = isGreenFieldInstallation ?
120 SchemaParser.extractDataFromBaseline(baselineDataSchema);
122 tablesForNbcCheck.addAll(tablesFromModelSvc);
123 backwardCompatibilityChecker.checkForNBCChangesInData(tablesFromBaselineSql, tablesForNbcCheck);
124 // Compare and store differences in tables from baseline sql with tables from model service
125 Map<String, List<Table>> differences = modelComparator.identifyDifferencesInBaselineAndGenerated(tablesFromModelSvc,
126 tablesFromBaselineSql);
127 // Generate schema from differences
128 StringBuilder generatedSchema = dataSchemaHelper.generateSchemaFromDifferences(differences);
129 //add custome sql query if cutom flag is enabled and greenfield is disabled
130 if (isCustomQueryExecutionEnabled && !isGreenFieldInstallation) {
131 generatedSchema.append(appendCustomQueries());
132 generatedSchema.append("\n");
134 generatedSchema.append(generateAnalyzeTableStatement(tablesFromModelSvc));
135 generatedSchema.append("COMMIT;\n");
136 this.sqlStatements = generatedSchema.toString();
139 private StringBuilder appendCustomQueries() {
140 StringBuilder customSqlQueries = new StringBuilder();
141 File customSqlFile = new File(customSqlScripts);
143 Path customQueryFilePath = customSqlFile.toPath();
144 try (Stream<String> lines = Files.lines(customQueryFilePath)) {
145 lines.forEach(line -> {
146 customSqlQueries.append(line).append("\n");
149 } catch (IOException exception) {
150 throw PgSchemaGeneratorException.readCustomSqlFileException("ties.data", exception);
152 return customSqlQueries;
155 private StringBuilder generateAnalyzeTableStatement(List<Table> tablesFromModelSvc) {
156 StringBuilder analyzeTableStmt = new StringBuilder();
157 tablesFromModelSvc.forEach(table -> analyzeTableStmt.append(String.format("ANALYZE ties_data.\"%s\";%n%n", table
159 return analyzeTableStmt;