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;
23 import org.oran.smo.teiv.pgsqlgenerator.Column;
24 import org.oran.smo.teiv.pgsqlgenerator.ForeignKeyConstraint;
25 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
26 import org.oran.smo.teiv.pgsqlgenerator.PostgresConstraint;
27 import org.oran.smo.teiv.pgsqlgenerator.PostgresIndex;
28 import org.oran.smo.teiv.pgsqlgenerator.Table;
29 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
31 import lombok.extern.slf4j.Slf4j;
32 import org.springframework.beans.factory.annotation.Value;
33 import org.springframework.stereotype.Component;
35 import java.util.List;
36 import java.util.Optional;
40 public class BackwardCompatibilityChecker {
42 @Value("${green-field-installation}")
43 private boolean isGreenFieldInstallation;
44 String noNBCCheckMsg = "No NBC checks done as green field installation is enabled";
46 public void checkForNBCChangesInModel(List<Relationship> relationshipsInBaselineSql,
47 List<Relationship> relationshipsFromModelSvc) {
48 if (!isGreenFieldInstallation) {
49 relationshipsInBaselineSql.forEach(baselineRel -> {
50 Optional<Relationship> matchingRelationship = relationshipsFromModelSvc.stream().filter(rel -> rel.getName()
51 .equals(baselineRel.getName())).findFirst();
52 matchingRelationship.ifPresentOrElse(modelSvcRel -> {
53 if (!(baselineRel.getBSideMinCardinality() == modelSvcRel.getBSideMinCardinality() && baselineRel
54 .getBSideMaxCardinality() == modelSvcRel.getBSideMaxCardinality() && baselineRel
55 .getASideMinCardinality() == modelSvcRel.getASideMinCardinality() && baselineRel
56 .getASideMaxCardinality() == modelSvcRel.getASideMaxCardinality())) {
57 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
58 "modified cardinalities for relationship(%s)", baselineRel.getName()),
59 new UnsupportedOperationException());
62 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
63 "modified/removed relationship(%s) present in baseline", baselineRel.getName()),
64 new UnsupportedOperationException());
68 log.info(noNBCCheckMsg);
72 public void checkForNBCChangesInData(List<Table> tablesInBaselineSql, List<Table> tablesFromModelSvc) {
73 if (!isGreenFieldInstallation) {
74 tablesInBaselineSql.forEach(baselineTable -> {
75 Optional<Table> matchingTable = tablesFromModelSvc.stream().filter(modelTable -> modelTable.getName()
76 .equals(baselineTable.getName())).findFirst();
77 matchingTable.ifPresentOrElse(table -> verifyTableColumns(baselineTable.getColumns(), table.getColumns(),
78 table.getName()), () -> {
79 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
80 "modified/removed table(%s) present in baseline", baselineTable.getName()),
81 new UnsupportedOperationException());
85 log.info(noNBCCheckMsg);
89 public void checkForNBCChangesInConsumerAndGroupsSchema(String baselineConsumerDataSchema,
90 String skeletonConsumerDataSchema) {
91 if (!isGreenFieldInstallation) {
92 final List<Table> baselineTables = SchemaParser.extractDataFromBaseline(baselineConsumerDataSchema);
93 final List<Table> skeletonTables = SchemaParser.extractDataFromBaseline(skeletonConsumerDataSchema);
94 checkForNBCChangesInData(baselineTables, skeletonTables);
96 log.info(noNBCCheckMsg);
100 private void verifyTableColumns(List<Column> columnsInBaseline, List<Column> columnsInModelSvc, String tableName) {
101 columnsInBaseline.forEach(baselineColumn -> {
102 Optional<Column> matchingColumn = columnsInModelSvc.stream().filter(modelColumn -> modelColumn.getName().equals(
103 baselineColumn.getName())).findFirst();
104 matchingColumn.ifPresentOrElse(modelColumn -> {
105 validateColumnConstraints(baselineColumn, modelColumn, tableName);
106 validateColumnIndexes(baselineColumn, modelColumn, tableName);
107 validateColumnDataType(baselineColumn, modelColumn, tableName);
109 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
110 "modified/removed column(%s.%s) present in baseline", tableName, baselineColumn.getName()),
111 new UnsupportedOperationException());
116 private void validateColumnConstraints(Column baselineColumn, Column modelColumn, String tableName) {
117 for (PostgresConstraint constraint : baselineColumn.getPostgresConstraints()) {
118 Optional<PostgresConstraint> matchingConstraint = modelColumn.getPostgresConstraints().stream().filter(
119 constraint1 -> constraint1.getConstraintName().equals(constraint.getConstraintName())).findFirst();
121 String modifiedOrRemovedConstraintFromBaseline = "modified/removed constraint for column(%s.%s) present in baseline";
122 matchingConstraint.ifPresentOrElse(constraint1 -> {
123 if (!constraint.getTableToAddConstraintTo().equals(constraint1.getTableToAddConstraintTo()) && !constraint
124 .getColumnToAddConstraintTo().equals(constraint1.getColumnToAddConstraintTo()) && !constraint
125 .getConstraintName().equals(constraint1.getConstraintName())) {
126 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
127 modifiedOrRemovedConstraintFromBaseline, tableName, baselineColumn.getName()),
128 new UnsupportedOperationException());
130 if (constraint instanceof ForeignKeyConstraint && !constraint.getTableToAddConstraintTo().equals(constraint1
131 .getTableToAddConstraintTo())) {
132 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
133 modifiedOrRemovedConstraintFromBaseline, tableName, baselineColumn.getName()),
134 new UnsupportedOperationException());
137 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
138 modifiedOrRemovedConstraintFromBaseline, tableName, baselineColumn.getName()),
139 new UnsupportedOperationException());
144 private void validateColumnIndexes(Column baselineColumn, Column modelColumn, String tableName) {
145 for (PostgresIndex indexInBaseline : baselineColumn.getPostgresIndexList()) {
146 Optional<PostgresIndex> matchingIndex = modelColumn.getPostgresIndexList().stream().filter(
147 indexInGenerated -> indexInGenerated.getIndexName().equals(indexInBaseline.getIndexName())).findFirst();
149 matchingIndex.ifPresentOrElse(indexInGenerated -> {
150 String indexStatementInBaseline = String.format(indexInBaseline.getIndexType().getCreateIndexStmt(),
151 indexInBaseline.getIndexName(), indexInBaseline.getTableNameToAddIndexTo(), indexInBaseline
152 .getColumnNameToAddIndexTo());
153 String indexStatementInGenerated = String.format(indexInGenerated.getIndexType().getCreateIndexStmt(),
154 indexInGenerated.getIndexName(), indexInGenerated.getTableNameToAddIndexTo(), indexInGenerated
155 .getColumnNameToAddIndexTo());
156 if (!indexStatementInGenerated.equals(indexStatementInBaseline)) {
157 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
158 "modified/removed index %s for column(%s.%s) present in baseline", indexInBaseline
159 .getIndexName(), tableName, baselineColumn.getName()),
160 new UnsupportedOperationException());
163 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
164 "modified/removed index %s for column(%s.%s) present in baseline", indexInBaseline.getIndexName(),
165 tableName, baselineColumn.getName()), new UnsupportedOperationException());
170 private void validateColumnDataType(Column baselineColumn, Column modelColumn, String tableName) {
171 if (!baselineColumn.getDataType().equals(modelColumn.getDataType())) {
172 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
173 "modified/removed datatype for column(%s.%s) present in baseline", tableName, baselineColumn.getName()),
174 new UnsupportedOperationException());