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;
33 import org.oran.smo.teiv.pgsqlgenerator.schema.BackwardCompatibilityChecker;
34 import org.oran.smo.teiv.pgsqlgenerator.FileHelper;
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.core.io.Resource;
39 import org.springframework.stereotype.Component;
41 import org.oran.smo.teiv.pgsqlgenerator.Entity;
42 import org.oran.smo.teiv.pgsqlgenerator.Module;
43 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
44 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
45 import org.oran.smo.teiv.pgsqlgenerator.Table;
46 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaGenerator;
47 import lombok.RequiredArgsConstructor;
48 import lombok.extern.slf4j.Slf4j;
52 @RequiredArgsConstructor
53 public class DataSchemaGenerator extends SchemaGenerator {
54 @Value("${schema.data.baseline}")
55 private String baselineDataSchema;
56 @Value("${schema.data.output}")
57 private String dataSchemaFileName;
58 @Value("${schema.data.skeleton}")
59 private String skeletonDataSchema;
60 @Value("${green-field-installation}")
61 private boolean isGreenFieldInstallation;
63 private final ModelComparator modelComparator;
64 private final DataSchemaHelper dataSchemaHelper;
65 private final TableBuilder tableBuilder;
66 private final BackwardCompatibilityChecker backwardCompatibilityChecker;
69 protected void prepareSchema() {
71 Resource baselineResource = new ClassPathResource(baselineDataSchema);
72 File newDataSchema = new File(dataSchemaFileName);
73 if (isGreenFieldInstallation || !baselineResource.exists()) {
74 log.info("Baseline DOES NOT EXISTS!!");
75 Resource skeletonResource = new ClassPathResource(skeletonDataSchema);
76 FileHelper.copyResourceToFile(skeletonResource, newDataSchema);
78 log.info("Baseline EXISTS!!");
79 Path sourcePath = baselineResource.getFile().toPath();
80 List<String> stmts = Files.readAllLines(sourcePath);
81 List<String> analyzeAndCommitExcludedStmts = new ArrayList<>();
82 for (String line : stmts) {
83 if (line.startsWith("ANALYZE") || line.equals("COMMIT;")) {
86 analyzeAndCommitExcludedStmts.add(line);
89 Files.write(Paths.get(dataSchemaFileName), analyzeAndCommitExcludedStmts, StandardOpenOption.CREATE,
90 StandardOpenOption.TRUNCATE_EXISTING);
92 this.schema = newDataSchema;
93 } catch (IOException exception) {
94 throw PgSchemaGeneratorException.prepareBaselineException("ties.data", exception);
99 protected void setSqlStatements(List<Module> modules, List<Entity> entities, List<Relationship> relationships) {
100 // Create table from entities and relationships
101 List<Table> tablesFromModelSvc = tableBuilder.getTables(entities, relationships);
102 // Get tables from baseline sql
103 List<Table> tablesFromBaselineSql = isGreenFieldInstallation ?
105 SchemaParser.extractDataFromBaseline(baselineDataSchema);
107 backwardCompatibilityChecker.checkForNBCChangesInData(tablesFromBaselineSql, tablesFromModelSvc);
108 // Compare and store differences in tables from baseline sql with tables from model service
109 Map<String, List<Table>> differences = modelComparator.identifyDifferencesInBaselineAndGenerated(tablesFromModelSvc,
110 tablesFromBaselineSql);
111 // Generate schema from differences
112 StringBuilder generatedSchema = dataSchemaHelper.generateSchemaFromDifferences(differences);
113 generatedSchema.append(generateAnalyzeTableStatement(tablesFromModelSvc));
114 generatedSchema.append("COMMIT;\n");
115 this.sqlStatements = generatedSchema.toString();
118 private StringBuilder generateAnalyzeTableStatement(List<Table> tablesFromModelSvc) {
119 StringBuilder analyzeTableStmt = new StringBuilder();
120 tablesFromModelSvc.forEach(table -> analyzeTableStmt.append(String.format("ANALYZE ties_data.\"%s\";%n%n", table
122 return analyzeTableStmt;