0b310bf6e2fab4958a15ad0a4dde965bd8e7d245
[smo/teiv.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.teiv.pgsqlgenerator.schema;
22
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.Table;
28 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
29
30 import lombok.extern.slf4j.Slf4j;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.stereotype.Component;
33
34 import java.util.List;
35 import java.util.Optional;
36
37 @Slf4j
38 @Component
39 public class BackwardCompatibilityChecker {
40
41     @Value("${green-field-installation}")
42     private boolean isGreenFieldInstallation;
43
44     public void checkForNBCChangesInModel(List<Relationship> relationshipsInBaselineSql,
45             List<Relationship> relationshipsFromModelSvc) {
46         if (!isGreenFieldInstallation) {
47             relationshipsInBaselineSql.forEach(baselineRel -> {
48                 Optional<Relationship> matchingRelationship = relationshipsFromModelSvc.stream().filter(rel -> rel.getName()
49                         .equals(baselineRel.getName())).findFirst();
50                 matchingRelationship.ifPresentOrElse(modelSvcRel -> {
51                     if (!(baselineRel.getBSideMinCardinality() == modelSvcRel.getBSideMinCardinality() && baselineRel
52                             .getBSideMaxCardinality() == modelSvcRel.getBSideMaxCardinality() && baselineRel
53                                     .getASideMinCardinality() == modelSvcRel.getASideMinCardinality() && baselineRel
54                                             .getASideMaxCardinality() == modelSvcRel.getASideMaxCardinality())) {
55                         throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
56                                 "modified cardinalities for relationship(%s)", baselineRel.getName()),
57                                 new UnsupportedOperationException());
58                     }
59                 }, () -> {
60                     throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
61                             "modified/removed relationship(%s) present in baseline", baselineRel.getName()),
62                             new UnsupportedOperationException());
63                 });
64             });
65         } else {
66             log.info("No NBC checks done as green field installation is enabled");
67         }
68     }
69
70     public void checkForNBCChangesInData(List<Table> tablesInBaselineSql, List<Table> tablesFromModelSvc) {
71         if (!isGreenFieldInstallation) {
72             tablesInBaselineSql.forEach(baselineTable -> {
73                 Optional<Table> matchingTable = tablesFromModelSvc.stream().filter(modelTable -> modelTable.getName()
74                         .equals(baselineTable.getName())).findFirst();
75                 matchingTable.ifPresentOrElse(table -> {
76                     verifyTableColumns(baselineTable.getColumns(), table.getColumns(), table.getName());
77                 }, () -> {
78                     throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
79                             "modified/removed table(%s) present in baseline", baselineTable.getName()),
80                             new UnsupportedOperationException());
81                 });
82             });
83         } else {
84             log.info("No NBC checks done as green field installation is enabled");
85         }
86     }
87
88     private void verifyTableColumns(List<Column> columnsInBaseline, List<Column> columnsInModelSvc, String tableName) {
89         columnsInBaseline.forEach(baselineColumn -> {
90             Optional<Column> matchingColumn = columnsInModelSvc.stream().filter(modelColumn -> modelColumn.getName().equals(
91                     baselineColumn.getName())).findFirst();
92             matchingColumn.ifPresentOrElse(modelColumn -> {
93                 validateColumnConstraints(baselineColumn, modelColumn, tableName);
94                 validateColumnDataType(baselineColumn, modelColumn, tableName);
95             }, () -> {
96                 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
97                         "modified/removed column(%s.%s) present in baseline", tableName, baselineColumn.getName()),
98                         new UnsupportedOperationException());
99             });
100         });
101     }
102
103     private void validateColumnConstraints(Column baselineColumn, Column modelColumn, String tableName) {
104         for (PostgresConstraint constraint : baselineColumn.getPostgresConstraints()) {
105             Optional<PostgresConstraint> matchingConstraint = modelColumn.getPostgresConstraints().stream().filter(
106                     constraint1 -> constraint1.getConstraintName().equals(constraint.getConstraintName())).findFirst();
107
108             matchingConstraint.ifPresentOrElse(constraint1 -> {
109                 if (!constraint.getTableToAddConstraintTo().equals(constraint1.getTableToAddConstraintTo()) && !constraint
110                         .getColumnToAddConstraintTo().equals(constraint1.getColumnToAddConstraintTo()) && !constraint
111                                 .getConstraintName().equals(constraint1.getConstraintName())) {
112                     throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
113                             "modified/removed constraint for column(%s.%s) present in baseline", tableName, baselineColumn
114                                     .getName()), new UnsupportedOperationException());
115                 }
116                 if (constraint instanceof ForeignKeyConstraint && !constraint.getTableToAddConstraintTo().equals(constraint1
117                         .getTableToAddConstraintTo())) {
118                     throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
119                             "modified/removed constraint for column(%s.%s) present in baseline", tableName, baselineColumn
120                                     .getName()), new UnsupportedOperationException());
121                 }
122             }, () -> {
123                 throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
124                         "modified/removed constraint for column(%s.%s) present in baseline", tableName, baselineColumn
125                                 .getName()), new UnsupportedOperationException());
126             });
127         }
128     }
129
130     private void validateColumnDataType(Column baselineColumn, Column modelColumn, String tableName) {
131         if (!baselineColumn.getDataType().equals(modelColumn.getDataType())) {
132             throw PgSchemaGeneratorException.nbcChangeIdentifiedException(String.format(
133                     "modified/removed datatype for column(%s.%s) present in baseline", tableName, baselineColumn.getName()),
134                     new UnsupportedOperationException());
135         }
136     }
137
138 }