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;
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;
30 import javax.xml.bind.DatatypeConverter;
32 import org.oran.smo.teiv.pgsqlgenerator.HashInfoEntity;
33 import org.oran.smo.teiv.pgsqlgenerator.PgSchemaGeneratorException;
34 import org.springframework.stereotype.Component;
37 import lombok.extern.slf4j.Slf4j;
42 public class HashInfoDataGenerator {
44 private final Set<HashInfoEntity> hashInfoRowsList = new HashSet<>();
47 * Generates a hash_info table entry for any table, column or constraint name passed and adds it to the hash_info data.
50 * Any prefix if applicable for the entry
52 * Actual name of table, column or constraint generated from model service
54 * Type of entry table, column or constraint
55 * @return Returns the generated name of table or column
57 public String generateHashAndRegisterTableRow(String prefix, String name, String type) {
59 if (prefix.length() + name.length() < 64) {
60 hashInfoRowsList.add(HashInfoEntity.builder().name(prefix + name).hashedValue(prefix + name).type(type)
64 hashedValue = generateSHA1Hash(prefix + name);
65 hashInfoRowsList.add(HashInfoEntity.builder().name(prefix + name).hashedValue(prefix + hashedValue).type(type)
67 return prefix + hashedValue;
71 private String generateSHA1Hash(String input) {
72 String hashedResult = "";
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);