diff options
author | 2025-02-04 18:30:20 +0000 | |
---|---|---|
committer | 2025-02-06 16:04:02 +0000 | |
commit | 4ec2a5d944b4c2bad799e6610f03912f5c955b50 (patch) | |
tree | b5802aece8a2d459c918a33a253d094743ab9fd5 /cps-ri | |
parent | 59f1cc4c5994da34f2c48a2347499f9cfb7bd836 (diff) |
Fix duplicated yang resource references V2
- Store Yang module references using SchemaSetEntity instead of SQL
- Added integration test to check number of references
- Update dispatcher for integration test to only return yang resources requested modules
- refactor schema set create & upgrade methods for better re-uses and readability
- consistent naming in related methods like(new)yangResourceContentPerName
(the name can be filename, module name or schema set name) depending on the context
- replaced 'var' with actual class names in affected classes
Issue-ID: CPS-2605
Change-Id: I8870c70832ac533bd17ce8af409a071f659e4acf
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Diffstat (limited to 'cps-ri')
9 files changed, 103 insertions, 292 deletions
diff --git a/cps-ri/pom.xml b/cps-ri/pom.xml index 7ae85df851..aa5fad9ac2 100644 --- a/cps-ri/pom.xml +++ b/cps-ri/pom.xml @@ -3,7 +3,7 @@ ============LICENSE_START=======================================================
Copyright (C) 2020-2021 Pantheon.tech
Modifications Copyright (C) 2020-2021 Bell Canada
- Modifications Copyright (C) 2020-2024 Nordix Foundation
+ Modifications Copyright (C) 2020-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.
@@ -33,7 +33,7 @@ <artifactId>cps-ri</artifactId>
<properties>
- <minimum-coverage>0.29</minimum-coverage>
+ <minimum-coverage>0.31</minimum-coverage>
<!-- Additional coverage is provided by integration-test module -->
</properties>
diff --git a/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java index 494d6919da..588a639ab8 100755 --- a/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/ri/CpsAdminPersistenceServiceImpl.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. @@ -91,10 +91,10 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final var schemaSetEntity = - schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final var anchorEntity = AnchorEntity.builder() + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = schemaSetRepository + .getByDataspaceAndName(dataspaceEntity, schemaSetName); + final AnchorEntity anchorEntity = AnchorEntity.builder() .name(anchorName) .dataspace(dataspaceEntity) .schemaSet(schemaSetEntity) @@ -114,7 +114,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public Collection<Anchor> getAnchors(final String dataspaceName) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity); return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet()); } @@ -154,14 +154,14 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Transactional @Override public void deleteAnchor(final String dataspaceName, final String anchorName) { - final var anchorEntity = getAnchorEntity(dataspaceName, anchorName); + final AnchorEntity anchorEntity = getAnchorEntity(dataspaceName, anchorName); anchorRepository.delete(anchorEntity); } @Transactional @Override public void deleteAnchors(final String dataspaceName, final Collection<String> anchorNames) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); anchorRepository.deleteAllByDataspaceAndNameIn(dataspaceEntity, anchorNames); } @@ -178,7 +178,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic } private AnchorEntity getAnchorEntity(final String dataspaceName, final String anchorName) { - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); return anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); } 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 aaf6165471..dbc6c28ec5 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 @@ -36,7 +36,6 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -103,10 +102,9 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override public Collection<ModuleReference> getYangResourceModuleReferences(final String dataspaceName) { - final Set<YangResourceModuleReference> yangResourceModuleReferenceList = + final Collection<YangResourceModuleReference> yangResourceModuleReferences = yangResourceRepository.findAllModuleReferencesByDataspace(dataspaceName); - return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference) - .collect(Collectors.toList()); + return yangResourceModuleReferences.stream().map(CpsModulePersistenceServiceImpl::toModuleReference).toList(); } @Override @@ -150,19 +148,24 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - public void storeSchemaSet(final String dataspaceName, final String schemaSetName, - final Map<String, String> moduleReferenceNameToContentMap) { - final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(moduleReferenceNameToContentMap); - final SchemaSetEntity schemaSetEntity = new SchemaSetEntity(); - schemaSetEntity.setName(schemaSetName); - schemaSetEntity.setDataspace(dataspaceEntity); - schemaSetEntity.setYangResources(yangResourceEntities); - try { - schemaSetRepository.save(schemaSetEntity); - } catch (final DataIntegrityViolationException e) { - throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e); - } + @Timed(value = "cps.module.persistence.schemaset.create", + description = "Time taken to store a schemaset (list of module references)") + public void createSchemaSet(final String dataspaceName, final String schemaSetName, + final Map<String, String> yangResourceContentPerName) { + final Set<YangResourceEntity> yangResourceEntities = synchronizeYangResources(yangResourceContentPerName); + createAndSaveSchemaSetEntity(dataspaceName, schemaSetName, yangResourceEntities); + } + + @Override + @Transactional + @Timed(value = "cps.module.persistence.schemaset.createFromNewAndExistingModules", + description = "Time taken to store a schemaset (from new and existing)") + public void createSchemaSetFromNewAndExistingModules(final String dataspaceName, final String schemaSetName, + final Map<String, String> newYangResourceContentPerName, + final Collection<ModuleReference> allModuleReferences) { + synchronizeYangResources(newYangResourceContentPerName); + final Set<YangResourceEntity> allYangResourceEntities = getYangResourceEntities(allModuleReferences); + createAndSaveSchemaSetEntity(dataspaceName, schemaSetName, allYangResourceEntities); } @Override @@ -181,22 +184,6 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - @Timed(value = "cps.module.persistence.schemaset.store", - description = "Time taken to store a schemaset (list of module references)") - public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName, - final Map<String, String> newModuleNameToContentMap, - final Collection<ModuleReference> allModuleReferences) { - storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap); - final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final SchemaSetEntity schemaSetEntity = - schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final List<Integer> allYangResourceIds = - yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences); - yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), allYangResourceIds); - } - - @Override - @Transactional public void deleteSchemaSet(final String dataspaceName, final String schemaSetName) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final SchemaSetEntity schemaSetEntity = @@ -213,14 +200,16 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Override @Transactional - public void updateSchemaSetFromModules(final String dataspaceName, final String schemaSetName, - final Map<String, String> newModuleNameToContentMap, - final Collection<ModuleReference> allModuleReferences) { + public void updateSchemaSetFromNewAndExistingModules(final String dataspaceName, final String schemaSetName, + final Map<String, String> newYangResourcesPerName, + final Collection<ModuleReference> allModuleReferences) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - storeAndLinkNewModules(newModuleNameToContentMap, schemaSetEntity); - updateAllModuleReferences(allModuleReferences, schemaSetEntity.getId()); + synchronizeYangResources(newYangResourcesPerName); + final Set<YangResourceEntity> allYangResourceEntities = getYangResourceEntities(allModuleReferences); + schemaSetEntity.setYangResources(allYangResourceEntities); + schemaSetRepository.save(schemaSetEntity); } @Override @@ -237,9 +226,35 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return moduleReferenceRepository.identifyNewModuleReferences(moduleReferencesToCheck); } - private Set<YangResourceEntity> synchronizeYangResources( - final Map<String, String> moduleReferenceNameToContentMap) { - final Map<String, YangResourceEntity> checksumToEntityMap = moduleReferenceNameToContentMap.entrySet().stream() + private Set<YangResourceEntity> synchronizeYangResources(final Map<String, String> yangResourceContentPerName) { + final Map<String, YangResourceEntity> yangResourceEntitiesPerChecksum = + getYangResourceEntityPerChecksum(yangResourceContentPerName); + + final List<YangResourceEntity> existingYangResourceEntities = + yangResourceRepository.findAllByChecksumIn(yangResourceEntitiesPerChecksum.keySet()); + + existingYangResourceEntities.forEach(exist -> yangResourceEntitiesPerChecksum.remove(exist.getChecksum())); + final Collection<YangResourceEntity> newYangResourceEntities = yangResourceEntitiesPerChecksum.values(); + + if (!newYangResourceEntities.isEmpty()) { + try { + yangResourceRepository.saveAll(newYangResourceEntities); + } catch (final DataIntegrityViolationException dataIntegrityViolationException) { + convertExceptionIfNeeded(dataIntegrityViolationException, newYangResourceEntities); + } + } + + // return ALL yang resourceEntities + return ImmutableSet.<YangResourceEntity>builder() + .addAll(existingYangResourceEntities) + .addAll(newYangResourceEntities) + .build(); + } + + private static Map<String, YangResourceEntity> getYangResourceEntityPerChecksum( + final Map<String, String> yangResourceContentPerName) { + final Map<String, YangResourceEntity> yangResourceEntityPerChecksum = + yangResourceContentPerName.entrySet().stream() .map(entry -> { final String checksum = DigestUtils.sha256Hex(entry.getValue().getBytes(StandardCharsets.UTF_8)); final Map<String, String> moduleNameAndRevisionMap = createModuleNameAndRevisionMap(entry.getKey(), @@ -256,29 +271,22 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ YangResourceEntity::getChecksum, entity -> entity )); + return yangResourceEntityPerChecksum; + } - final List<YangResourceEntity> existingYangResourceEntities = - yangResourceRepository.findAllByChecksumIn(checksumToEntityMap.keySet()); - existingYangResourceEntities.forEach(yangFile -> checksumToEntityMap.remove(yangFile.getChecksum())); - - final Collection<YangResourceEntity> newYangResourceEntities = checksumToEntityMap.values(); - if (!newYangResourceEntities.isEmpty()) { - try { - yangResourceRepository.saveAll(newYangResourceEntities); - } catch (final DataIntegrityViolationException dataIntegrityViolationException) { - // Throw a CPS duplicated Yang resource exception if the cause of the error is a yang checksum - // database constraint violation. If it is not, then throw the original exception - final Optional<DuplicatedYangResourceException> convertedException = - convertToDuplicatedYangResourceException( - dataIntegrityViolationException, newYangResourceEntities); - throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException; - } + private void createAndSaveSchemaSetEntity(final String dataspaceName, + final String schemaSetName, + final Set<YangResourceEntity> yangResourceEntities) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = new SchemaSetEntity(); + schemaSetEntity.setName(schemaSetName); + schemaSetEntity.setDataspace(dataspaceEntity); + schemaSetEntity.setYangResources(yangResourceEntities); + try { + schemaSetRepository.save(schemaSetEntity); + } catch (final DataIntegrityViolationException e) { + throw AlreadyDefinedException.forSchemaSet(schemaSetName, dataspaceName, e); } - - return ImmutableSet.<YangResourceEntity>builder() - .addAll(existingYangResourceEntities) - .addAll(newYangResourceEntities) - .build(); } private static Map<String, String> createModuleNameAndRevisionMap(final String sourceName, final String source) { @@ -323,6 +331,14 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return RevisionSourceIdentifier.create(sourceName); } + private void convertExceptionIfNeeded(final DataIntegrityViolationException dataIntegrityViolationException, + final Collection<YangResourceEntity> newYangResourceEntities) { + final Optional<DuplicatedYangResourceException> convertedException = + convertToDuplicatedYangResourceException( + dataIntegrityViolationException, newYangResourceEntities); + throw convertedException.isPresent() ? convertedException.get() : dataIntegrityViolationException; + } + /** * Convert the specified data integrity violation exception into a CPS duplicated Yang resource exception * if the cause of the error is a yang checksum database constraint violation. @@ -372,6 +388,13 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return "no checksum found"; } + private Set<YangResourceEntity> getYangResourceEntities(final Collection<ModuleReference> moduleReferences) { + return moduleReferences.stream().map(moduleReference -> + yangResourceRepository + .findByModuleNameAndRevision(moduleReference.getModuleName(), moduleReference.getRevision())) + .collect(Collectors.toSet()); + } + private static ModuleReference toModuleReference( final YangResourceModuleReference yangResourceModuleReference) { return ModuleReference.builder() @@ -392,20 +415,4 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ .dataspaceName(schemaSetEntity.getDataspace().getName()).build(); } - private void storeAndLinkNewModules(final Map<String, String> newModuleNameToContentMap, - final SchemaSetEntity schemaSetEntity) { - final Set<YangResourceEntity> yangResourceEntities - = new HashSet<>(synchronizeYangResources(newModuleNameToContentMap)); - schemaSetEntity.setYangResources(yangResourceEntities); - schemaSetRepository.save(schemaSetEntity); - } - - private void updateAllModuleReferences(final Collection<ModuleReference> allModuleReferences, - final Integer schemaSetEntityId) { - yangResourceRepository.deleteSchemaSetYangResourceForSchemaSetId(schemaSetEntityId); - final List<Integer> allYangResourceIds = - yangResourceRepository.getResourceIdsByModuleReferences(allModuleReferences); - yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntityId, allYangResourceIds); - } - } 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 deleted file mode 100644 index 410dcc2e26..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepository.java +++ /dev/null @@ -1,36 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ri.repository; - -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/SchemaSetYangResourceRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepositoryImpl.java deleted file mode 100644 index 989809af5b..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/SchemaSetYangResourceRepositoryImpl.java +++ /dev/null @@ -1,58 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ri.repository; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import java.sql.PreparedStatement; -import java.util.List; -import org.hibernate.Session; -import org.springframework.transaction.annotation.Transactional; - -@Transactional -public class SchemaSetYangResourceRepositoryImpl implements SchemaSetYangResourceRepository { - - private static final int MAX_INSERT_BATCH_SIZE = 100; - - @PersistenceContext - private EntityManager entityManager; - - @Override - public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Integer> yangResourceIds) { - final Session session = entityManager.unwrap(Session.class); - session.doWork(connection -> { - try (PreparedStatement preparedStatement = connection.prepareStatement( - "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES ( ?, ?)")) { - int sqlQueryCount = 1; - for (final int yangResourceId : yangResourceIds) { - preparedStatement.setInt(1, schemaSetId); - preparedStatement.setInt(2, yangResourceId); - preparedStatement.addBatch(); - if (sqlQueryCount % MAX_INSERT_BATCH_SIZE == 0 || sqlQueryCount == yangResourceIds.size()) { - preparedStatement.executeBatch(); - } - sqlQueryCount++; - } - } - }); - } -} - diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepository.java deleted file mode 100644 index 2875511c1e..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepository.java +++ /dev/null @@ -1,31 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ri.repository; - -import java.util.Collection; -import java.util.List; -import org.onap.cps.api.model.ModuleReference; - -public interface YangResourceNativeRepository { - - List<Integer> getResourceIdsByModuleReferences(Collection<ModuleReference> moduleReferences); - -} diff --git a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepositoryImpl.java deleted file mode 100644 index 34f1ee362a..0000000000 --- a/cps-ri/src/main/java/org/onap/cps/ri/repository/YangResourceNativeRepositoryImpl.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.ri.repository; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.PersistenceContext; -import jakarta.persistence.Query; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.StringJoiner; -import lombok.extern.slf4j.Slf4j; -import org.hibernate.type.StandardBasicTypes; -import org.onap.cps.api.model.ModuleReference; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; - -@Slf4j -@Repository -public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository { - - @PersistenceContext - private EntityManager entityManager; - - @Override - @Transactional - public List<Integer> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) { - if (moduleReferences.isEmpty()) { - return Collections.emptyList(); - } - final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences)) - .unwrap(org.hibernate.query.NativeQuery.class) - .addScalar("id", StandardBasicTypes.INTEGER); - final List<Integer> yangResourceIds = query.getResultList(); - if (yangResourceIds.size() != moduleReferences.size()) { - log.warn("ModuleReferences size : {} and QueryResult size : {}", moduleReferences.size(), - yangResourceIds.size()); - } - return yangResourceIds; - } - - private String getCombinedSelectSqlQuery(final Collection<ModuleReference> moduleReferences) { - final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL "); - moduleReferences.forEach(moduleReference -> - sqlQueryJoiner.add(String.format("SELECT id FROM yang_resource WHERE module_name='%s' and revision='%s'", - moduleReference.getModuleName(), - moduleReference.getRevision())) - ); - return sqlQueryJoiner.toString(); - } -} 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 628502f846..e36e376bc6 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 @@ -32,8 +32,10 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository -public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Integer>, - YangResourceNativeRepository, SchemaSetYangResourceRepository { +public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Integer> { + + YangResourceEntity findByModuleNameAndRevision(@Param("moduleName") String moduleName, + @Param("revision") String revision); List<YangResourceEntity> findAllByChecksumIn(Collection<String> checksums); @@ -88,10 +90,6 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity @Param("moduleName") String moduleName, @Param("revision") String revision); @Modifying - @Query(value = "DELETE FROM schema_set_yang_resources WHERE schema_set_id = :schemaSetId", nativeQuery = true) - void deleteSchemaSetYangResourceForSchemaSetId(@Param("schemaSetId") int schemaSetId); - - @Modifying @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) diff --git a/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy index 2915bc8e8c..9abfdbeb33 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/ri/CpsModulePersistenceServiceImplSpec.groovy @@ -78,8 +78,8 @@ class CpsModulePersistenceServiceImplSpec extends Specification { and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)' mockYangResourceRepository.saveAll(_) >> { throw dbException } when: 'attempt to store schema set ' - def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent] - objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap) + def newYangResourceContentPerName = [(yangResourceName):yangResourceContent] + objectUnderTest.createSchemaSet('my-dataspace', 'my-schema-set', newYangResourceContentPerName) then: 'an #expectedThrownException is thrown' def e = thrown(expectedThrownException) assert e.getMessage().contains(expectedThrownExceptionMessage) @@ -96,7 +96,7 @@ class CpsModulePersistenceServiceImplSpec extends Specification { def schemaSetEntity = new SchemaSetEntity(id: 1) mockSchemaSetRepository.getByDataspaceAndName(_, _) >> schemaSetEntity when: 'schema set update is requested' - objectUnderTest.updateSchemaSetFromModules('my-dataspace', 'my-schemaset', [:], [new ModuleReference('some module name', 'some revision name')]) + objectUnderTest.updateSchemaSetFromNewAndExistingModules('my-dataspace', 'my-schemaset', [:], [new ModuleReference('some module name', 'some revision name')]) then: 'no exception is thrown ' noExceptionThrown() } |