771baee1d3effd3b3b107f90f287ade4a8dc0e4b
[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.model;
22
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25 import java.nio.charset.StandardCharsets;
26 import java.util.HashSet;
27 import java.util.Locale;
28 import java.util.Set;
29
30 import javax.xml.bind.DatatypeConverter;
31
32 import org.oran.smo.teiv.pgsqlgenerator.HashInfoEntity;
33 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
34 import org.springframework.stereotype.Component;
35
36 import lombok.Getter;
37 import lombok.extern.slf4j.Slf4j;
38
39 @Component
40 @Getter
41 @Slf4j
42 public class HashInfoDataGenerator {
43
44     private final Set<HashInfoEntity> hashInfoRowsList = new HashSet<>();
45
46     /**
47      * Generates a hash_info table entry for any table, column or constraint name passed and adds it to the hash_info data.
48      *
49      * @param prefix
50      *     Any prefix if applicable for the entry
51      * @param name
52      *     Actual name of table, column or constraint generated from model service
53      * @param type
54      *     Type of entry table, column or constraint
55      * @return Returns the generated name of table or column
56      */
57     public String generateHashAndRegisterTableRow(String prefix, String name, String type) {
58         String hashedValue;
59         if (prefix.length() + name.length() < 64) {
60             hashInfoRowsList.add(HashInfoEntity.builder().name(prefix + name).hashedValue(prefix + name).type(type)
61                     .build());
62             return prefix + name;
63         } else {
64             hashedValue = generateSHA1Hash(prefix + name);
65             hashInfoRowsList.add(HashInfoEntity.builder().name(prefix + name).hashedValue(prefix + hashedValue).type(type)
66                     .build());
67             return prefix + hashedValue;
68         }
69     }
70
71     private String generateSHA1Hash(String input) {
72         String hashedResult = "";
73         try {
74             MessageDigest md = MessageDigest.getInstance("SHA-1");
75             md.update(input.getBytes(StandardCharsets.UTF_8));
76             byte[] digest = md.digest();
77             hashedResult = DatatypeConverter.printHexBinary(digest).toUpperCase(Locale.US);
78         } catch (NoSuchAlgorithmException exception) {
79             throw PgSchemaGeneratorException.generateSHA1HashException(exception);
80         }
81         return hashedResult;
82     }
83 }