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.List;
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;
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;
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;
61 private final ModelComparator modelComparator;
62 private final DataSchemaHelper dataSchemaHelper;
63 private final TableBuilder tableBuilder;
64 private final BackwardCompatibilityChecker backwardCompatibilityChecker;
67 protected void prepareSchema() {
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);
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();
81 Files.write(Paths.get(dataSchemaFileName), commitExcludedStmts, StandardOpenOption.CREATE,
82 StandardOpenOption.TRUNCATE_EXISTING);
84 this.schema = newDataSchema;
85 } catch (IOException exception) {
86 throw PgSchemaGeneratorException.prepareBaselineException("ties.data", exception);
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 ?
97 SchemaParser.extractDataFromBaseline(baselineDataSchema);
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();