From 9df4a57a05e3ee67ff96284a4f7b1b07c94600b1 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Thu, 16 Jan 2025 14:41:18 +0000 Subject: One SchemaSet per moduleSetTag - Registration: create and upgrade cases. - Handle moduleSetTag deletion (all orphans) for testware - Unit tests updated - additional logging of details for upgrade scenarios - Integration Tests updated - Remove cache for module sets being processed - Removed DbCleaner (startup) - Removed redundant methods in NCMP Inventory for deleting schema set(s) - Removed validation check for all schema set interactions - Updated some schema set tests to use special characters previously not allowed - Checked integration test scenarios for upgrades with and without tags: all scenarios covered! TODO - REST endpoint to remove orphaned schema set data, separate story: CPS-2554 - Investigate exception handling regarding DuplicateYangResourceException: CPS-2555 Issue-ID: CPS-2540 Signed-off-by: ToineSiebelink Change-Id: Iaa59cbdb86b7a4a8044624829bc002506ff40cc7 --- .../cps/ri/CpsModulePersistenceServiceImpl.java | 23 ++--- .../cps/ri/repository/ModuleReferenceQuery.java | 6 +- .../repository/ModuleReferenceRepositoryImpl.java | 112 ++------------------- .../cps/ri/repository/SchemaSetRepository.java | 13 ++- .../SchemaSetYangResourceRepository.java | 9 +- .../cps/ri/repository/YangResourceRepository.java | 10 +- 6 files changed, 45 insertions(+), 128 deletions(-) (limited to 'cps-ri') 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. @@ -173,6 +173,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 getSchemaSetsByDataspaceName(final String dataspaceName) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); @@ -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 getModuleReferencesByAttribute(final String dataspaceName, - final String anchorName, - final Map parentAttributes, - final Map childAttributes) { - return moduleReferenceRepository.findModuleReferences(dataspaceName, anchorName, parentAttributes, - childAttributes); - } - private Set synchronizeYangResources( final Map moduleReferenceNameToContentMap) { final Map 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 identifyNewModuleReferences(final Collection moduleReferencesToCheck); - Collection findModuleReferences(final String dataspaceName, final String anchorName, - final Map parentAttributes, - final Map 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 findModuleReferences(final String dataspaceName, final String anchorName, - final Map parentAttributes, - final Map 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 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 parentAttributes, - final Map 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 processQueryResults(final List 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 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 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 { + boolean existsByDataspaceAndName(DataspaceEntity dataspaceEntity, String schemaSetName); + Optional findByDataspaceAndName(DataspaceEntity dataspaceEntity, String schemaSetName); /** @@ -76,4 +78,13 @@ public interface SchemaSetRepository extends JpaRepository 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