diff options
author | Sourabh Sourabh <sourabh.sourabh@est.tech> | 2025-01-17 09:49:36 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2025-01-17 09:49:36 +0000 |
commit | 76a3bddc4494b6856084b02f49e37e7ab7c5d27a (patch) | |
tree | 795ff8592b2a7d745dbba1ca0682626a59b96839 /cps-ri/src/main | |
parent | d5c0c3aef85915cbdd1fd67ecd058b53e77467cc (diff) | |
parent | 9df4a57a05e3ee67ff96284a4f7b1b07c94600b1 (diff) |
Merge "One SchemaSet per moduleSetTag"
Diffstat (limited to 'cps-ri/src/main')
6 files changed, 45 insertions, 128 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java index 023e76ef89..cf9fb021a6 100755 --- a/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/CpsModulePersistenceServiceImpl.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020-2024 Nordix Foundation + * Copyright (C) 2020-2025 Nordix Foundation * Modifications Copyright (C) 2020-2022 Bell Canada. * Modifications Copyright (C) 2021 Pantheon.tech * Modifications Copyright (C) 2022 TechMahindra Ltd. @@ -174,6 +174,12 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ } @Override + public boolean schemaSetExists(final String dataspaceName, final String schemaSetName) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + return schemaSetRepository.existsByDataspaceAndName(dataspaceEntity, schemaSetName); + } + + @Override public Collection<SchemaSet> getSchemaSetsByDataspaceName(final String dataspaceName) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final List<SchemaSetEntity> schemaSetEntities = schemaSetRepository.findByDataspace(dataspaceEntity); @@ -217,7 +223,6 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ schemaSetRepository.deleteByDataspaceAndNameIn(dataspaceEntity, schemaSetNames); } - @Override @Transactional public void updateSchemaSetFromModules(final String dataspaceName, final String schemaSetName, @@ -232,8 +237,9 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - public void deleteUnusedYangResourceModules() { - yangResourceRepository.deleteOrphans(); + public void deleteAllUnusedYangModuleData() { + schemaSetRepository.deleteOrphanedSchemaSets(); + yangResourceRepository.deleteOrphanedYangResources(); } @Override @@ -242,15 +248,6 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck); } - @Override - public Collection<ModuleReference> getModuleReferencesByAttribute(final String dataspaceName, - final String anchorName, - final Map<String, String> parentAttributes, - final Map<String, String> childAttributes) { - return moduleReferenceRepository.findModuleReferences(dataspaceName, anchorName, parentAttributes, - childAttributes); - } - private Set<YangResourceEntity> synchronizeYangResources( final Map<String, String> moduleReferenceNameToContentMap) { final Map<String, YangResourceEntity> checksumToEntityMap = moduleReferenceNameToContentMap.entrySet().stream() diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceQuery.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceQuery.java index 85d0e438cb..c91e8de48d 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceQuery.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceQuery.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022-2024 Nordix Foundation. + * Copyright (C) 2022-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ package org.onap.cps.ri.repository; import java.util.Collection; -import java.util.Map; import org.onap.cps.api.model.ModuleReference; /** @@ -31,7 +30,4 @@ public interface ModuleReferenceQuery { Collection<ModuleReference> identifyNewModuleReferences(final Collection<ModuleReference> moduleReferencesToCheck); - Collection<ModuleReference> findModuleReferences(final String dataspaceName, final String anchorName, - final Map<String, String> parentAttributes, - final Map<String, String> childAttributes); } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceRepositoryImpl.java index b98696ca72..7611dcd8a5 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceRepositoryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/ModuleReferenceRepositoryImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2022 Nordix Foundation. + * Copyright (C) 2022-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,24 +20,18 @@ package org.onap.cps.ri.repository; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; -import jakarta.persistence.Query; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; -import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.model.ModuleReference; import org.springframework.transaction.annotation.Transactional; -@Slf4j @Transactional @RequiredArgsConstructor public class ModuleReferenceRepositoryImpl implements ModuleReferenceQuery { @@ -70,104 +64,14 @@ public class ModuleReferenceRepositoryImpl implements ModuleReferenceQuery { return identifyNewModuleReferencesForCmHandle(tempTableName); } - /** - * Finds module references based on specified dataspace, anchor, and attribute filters. - * This method constructs and executes a SQL query to retrieve module references. The query applies filters to - * parent and child fragments using the provided attribute maps. The `parentAttributes` are used to filter - * parent fragments, while `childAttributes` filter child fragments. - * - * @param dataspaceName the name of the dataspace to filter on. - * @param anchorName the name of the anchor to filter on. - * @param parentAttributes a map of attributes for filtering parent fragments. - * @param childAttributes a map of attributes for filtering child fragments. - * @return a collection of {@link ModuleReference} objects that match the specified filters. - */ - @Transactional - @SuppressWarnings("unchecked") - @Override - public Collection<ModuleReference> findModuleReferences(final String dataspaceName, final String anchorName, - final Map<String, String> parentAttributes, - final Map<String, String> childAttributes) { - - final String parentFragmentWhereClause = buildWhereClause(childAttributes, "parentFragment"); - final String childFragmentWhereClause = buildWhereClause(parentAttributes, "childFragment"); - - final String moduleReferencesSqlQuery = buildModuleReferencesSqlQuery(parentFragmentWhereClause, - childFragmentWhereClause); - - final Query query = entityManager.createNativeQuery(moduleReferencesSqlQuery); - setQueryParameters(query, parentAttributes, childAttributes, anchorName, dataspaceName); - return processQueryResults(query.getResultList()); - } - - private String buildWhereClause(final Map<String, String> attributes, final String alias) { - return attributes.keySet().stream() - .map(attributeName -> String.format("%s.attributes->>'%s' = ?", alias, attributeName)) - .collect(Collectors.joining(" AND ")); - } - - private void setQueryParameters(final Query query, final Map<String, String> parentAttributes, - final Map<String, String> childAttributes, final String anchorName, - final String dataspaceName) { - final String childAttributeValue = childAttributes.entrySet().iterator().next().getValue(); - query.setParameter(1, childAttributeValue); - - final String parentAttributeValue = parentAttributes.entrySet().iterator().next().getValue(); - query.setParameter(2, parentAttributeValue); - - query.setParameter(3, anchorName); - query.setParameter(4, dataspaceName); - } - - @SuppressFBWarnings(value = "VA_FORMAT_STRING_USES_NEWLINE", justification = "no \n in string just in file format") - private String buildModuleReferencesSqlQuery(final String parentFragmentClause, final String childFragmentClause) { - return """ - WITH Fragment AS ( - SELECT childFragment.attributes->>'id' AS schema_set_name - FROM fragment parentFragment - JOIN fragment childFragment ON parentFragment.parent_id = childFragment.id - JOIN anchor anchorInfo ON parentFragment.anchor_id = anchorInfo.id - JOIN dataspace dataspaceInfo ON anchorInfo.dataspace_id = dataspaceInfo.id - WHERE %s - AND %s - AND anchorInfo.name = ? - AND dataspaceInfo.name = ? - LIMIT 1 - ), - SchemaSet AS ( - SELECT id - FROM schema_set - WHERE name = (SELECT schema_set_name FROM Fragment) - ) - SELECT yangResource.module_name, yangResource.revision - FROM yang_resource yangResource - JOIN schema_set_yang_resources schemaSetYangResources - ON yangResource.id = schemaSetYangResources.yang_resource_id - WHERE schemaSetYangResources.schema_set_id = (SELECT id FROM SchemaSet); - """.formatted(parentFragmentClause, childFragmentClause); - } - - private Collection<ModuleReference> processQueryResults(final List<Object[]> queryResults) { - if (queryResults.isEmpty()) { - log.info("No module references found for the provided attributes."); - return Collections.emptyList(); - } - return queryResults.stream() - .map(queryResult -> { - final String name = (String) queryResult[0]; - final String revision = (String) queryResult[1]; - return new ModuleReference(name, revision); - }) - .collect(Collectors.toList()); - } - private Collection<ModuleReference> identifyNewModuleReferencesForCmHandle(final String tempTableName) { - final String sql = String.format( - "SELECT %1$s.module_name, %1$s.revision" - + " FROM %1$s LEFT JOIN yang_resource" - + " ON yang_resource.module_name=%1$s.module_name" - + " AND yang_resource.revision=%1$s.revision" - + " WHERE yang_resource.module_name IS NULL;", tempTableName); + final String sql = """ + SELECT %1$s.module_name, %1$s.revision + FROM %1$s + LEFT JOIN yang_resource + ON yang_resource.module_name=%1$s.module_name AND yang_resource.revision=%1$s.revision + WHERE yang_resource.module_name IS NULL; + """.formatted(tempTableName); @SuppressWarnings("unchecked") final List<Object[]> resultsAsObjects = entityManager.createNativeQuery(sql).getResultList(); diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetRepository.java index 4e4948e601..b8dd7b755c 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetRepository.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech * Modifications Copyright (C) 2022 TechMahindra Ltd. - * Modifications Copyright (C) 2023-2024 Nordix Foundation + * Modifications Copyright (C) 2023-2025 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,8 @@ import org.springframework.stereotype.Repository; @Repository public interface SchemaSetRepository extends JpaRepository<SchemaSetEntity, Integer> { + boolean existsByDataspaceAndName(DataspaceEntity dataspaceEntity, String schemaSetName); + Optional<SchemaSetEntity> findByDataspaceAndName(DataspaceEntity dataspaceEntity, String schemaSetName); /** @@ -76,4 +78,13 @@ public interface SchemaSetRepository extends JpaRepository<SchemaSetEntity, Inte deleteByDataspaceIdAndNameIn(dataspaceEntity.getId(), schemaSetNames); } + /** + * Delete any schema set no longer used by any anchor. + */ + @Modifying + @Query(value = """ + DELETE FROM schema_set WHERE NOT EXISTS + (SELECT 1 FROM anchor WHERE anchor.schema_set_id = schema_set.id) + """, nativeQuery = true) + void deleteOrphanedSchemaSets(); } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java index 8350d5728c..410dcc2e26 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. + * Copyright (C) 2021-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,13 @@ import java.util.List; public interface SchemaSetYangResourceRepository { + + /** + * Link yang resources (ids) with a schema set (id). + * + * @param schemaSetId the schema set id + * @param yangResourceIds list of yang resource ids + */ void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Integer> yangResourceIds); } diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java index 831766cc9a..628502f846 100644 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceRepository.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech - * Modifications Copyright (C) 2021-2024 Nordix Foundation + * Modifications Copyright (C) 2021-2025 Nordix Foundation * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -92,7 +92,9 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity void deleteSchemaSetYangResourceForSchemaSetId(@Param("schemaSetId") int schemaSetId); @Modifying - @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS " - + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true) - void deleteOrphans(); + @Query(value = """ + DELETE FROM yang_resource WHERE NOT EXISTS (SELECT 1 FROM schema_set_yang_resources + WHERE schema_set_yang_resources.yang_resource_id = yang_resource.id) + """, nativeQuery = true) + void deleteOrphanedYangResources(); } |