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.model;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.StandardCopyOption;
27 import java.util.ArrayList;
28 import java.util.Comparator;
29 import java.util.List;
31 import org.oran.smo.teiv.pgsqlgenerator.HashInfoEntity;
32 import org.oran.smo.teiv.pgsqlgenerator.schema.BackwardCompatibilityChecker;
33 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaParser;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.stereotype.Component;
36 import org.springframework.util.ResourceUtils;
38 import org.oran.smo.teiv.pgsqlgenerator.Entity;
39 import org.oran.smo.teiv.pgsqlgenerator.Module;
40 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
41 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
42 import org.oran.smo.teiv.pgsqlgenerator.schema.SchemaGenerator;
43 import org.oran.smo.teiv.pgsqlgenerator.schema.Table;
44 import lombok.RequiredArgsConstructor;
45 import lombok.extern.slf4j.Slf4j;
49 @RequiredArgsConstructor
50 public class ModelSchemaGenerator extends SchemaGenerator {
51 @Value("${schema.model.output}")
52 private String modelOutputFileName;
53 @Value("${schema.model.skeleton}")
54 private String skeletonModelSchemaFileClassPath;
55 @Value("${schema.model.baseline}")
56 private String baselineModelSchemaFileClassPath;
57 @Value("${schema.model.temp-baseline}")
58 private String tempBaselineModelSchemaFileClassPath;
59 @Value("${green-field-installation}")
60 private boolean isGreenFieldInstallation;
62 private final HashInfoDataGenerator hashInfoDataGenerator;
63 private final BackwardCompatibilityChecker backwardCompatibilityChecker;
66 protected void prepareSchema() {
68 File skeletonFile = ResourceUtils.getFile("classpath:" + skeletonModelSchemaFileClassPath);
69 File newGeneratedModelFile = new File(modelOutputFileName);
70 Files.copy(skeletonFile.toPath(), newGeneratedModelFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
71 if (!isGreenFieldInstallation) {
72 File helmBaseline = ResourceUtils.getFile(baselineModelSchemaFileClassPath);
73 File modelBaseline = new File(tempBaselineModelSchemaFileClassPath);
74 Files.copy(helmBaseline.toPath(), modelBaseline.toPath(), StandardCopyOption.REPLACE_EXISTING);
76 this.schema = newGeneratedModelFile;
77 } catch (IOException exception) {
78 throw PgSchemaGeneratorException.prepareBaselineException("ties.model", exception);
83 protected void setSqlStatements(List<Module> modules, List<Entity> entities, List<Relationship> relationships) {
84 if (!isGreenFieldInstallation) {
85 // Get relationships from baseline sql
86 List<Relationship> relFromBaselineSql = SchemaParser.extractFromModelBaseline(
87 tempBaselineModelSchemaFileClassPath);
89 backwardCompatibilityChecker.checkForNBCChangesInModel(relFromBaselineSql, relationships);
91 StringBuilder tiesModelSql = new StringBuilder();
93 List<HashInfoEntity> hashInfoList = new ArrayList<>(hashInfoDataGenerator.getHashInfoRowsList().stream().toList());
94 hashInfoList.sort(Comparator.comparing(HashInfoEntity::getName));
96 tiesModelSql.append(prepareCopyStatement(hashInfoList));
97 tiesModelSql.append(prepareCopyStatement(modules));
98 tiesModelSql.append(prepareCopyStatement(entities));
99 tiesModelSql.append(prepareCopyStatement(relationships));
100 tiesModelSql.append(";\n").append("\nCOMMIT;");
101 this.sqlStatements = tiesModelSql.toString();
104 private StringBuilder prepareCopyStatement(List<? extends Table> table) {
105 StringBuilder copyStatement = new StringBuilder();
106 if (!table.isEmpty()) {
107 copyStatement.append("COPY ties_model.").append(table.get(0).getTableName()).append(table.get(0)
108 .getColumnsForCopyStatement()).append(" FROM stdin;\n");
109 table.forEach(table1 -> copyStatement.append(table1.getRecordForCopyStatement()));
110 copyStatement.append("\\.\n\n");
112 return copyStatement;