diff options
Diffstat (limited to 'cps-ri/src')
3 files changed, 58 insertions, 11 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java index 0d77530b30..b115f9a3c7 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java @@ -34,6 +34,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -190,6 +191,21 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ schemaSetRepository.deleteByDataspaceAndNameIn(dataspaceEntity, schemaSetNames); } + + @Override + @Transactional + public void updateSchemaSetFromModules(final String dataspaceName, final String schemaSetName, + final Map<String, String> newModuleNameToContentMap, + final Collection<ModuleReference> allModuleReferences) { + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = + schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); + storeAndLinkNewModules(newModuleNameToContentMap, schemaSetEntity); + updateAllModuleReferences(allModuleReferences, schemaSetEntity.getId()); + } + + + @Override @Transactional public void deleteUnusedYangResourceModules() { @@ -364,4 +380,21 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return SchemaSet.builder().name(schemaSetEntity.getName()) .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/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java index e833796337..3fec462b44 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -109,6 +109,10 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity } @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 yr WHERE NOT EXISTS " + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true) void deleteOrphans(); diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy index 9696b28cd7..3447a1c599 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceSpec.groovy @@ -20,16 +20,16 @@ package org.onap.cps.spi.impl import org.hibernate.exception.ConstraintViolationException -import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.CpsModulePersistenceService +import org.onap.cps.spi.entities.SchemaSetEntity import org.onap.cps.spi.exceptions.DuplicatedYangResourceException +import org.onap.cps.spi.model.ModuleReference import org.onap.cps.spi.repository.DataspaceRepository import org.onap.cps.spi.repository.ModuleReferenceRepository import org.onap.cps.spi.repository.SchemaSetRepository import org.onap.cps.spi.repository.YangResourceRepository import org.springframework.dao.DataIntegrityViolationException import spock.lang.Specification - import java.sql.SQLException /** @@ -39,11 +39,10 @@ class CpsModulePersistenceServiceSpec extends Specification { CpsModulePersistenceService objectUnderTest - def dataspaceRepositoryMock = Mock(DataspaceRepository) - def yangResourceRepositoryMock = Mock(YangResourceRepository) - def schemaSetRepositoryMock = Mock(SchemaSetRepository) - def cpsAdminPersistenceServiceMock = Mock(CpsAdminPersistenceService) - def moduleReferenceRepositoryMock = Mock(ModuleReferenceRepository) + def mockDataspaceRepository = Mock(DataspaceRepository) + def mockYangResourceRepository = Mock(YangResourceRepository) + def mockSchemaSetRepository = Mock(SchemaSetRepository) + def mockModuleReferenceRepository = Mock(ModuleReferenceRepository) def yangResourceName = 'my-yang-resource-name' def yangResourceContent = 'module stores {\n' + @@ -68,15 +67,15 @@ class CpsModulePersistenceServiceSpec extends Specification { static otherIntegrityException = new DataIntegrityViolationException('another integrity exception') def setup() { - objectUnderTest = new CpsModulePersistenceServiceImpl(yangResourceRepositoryMock, schemaSetRepositoryMock, - dataspaceRepositoryMock, moduleReferenceRepositoryMock) + objectUnderTest = new CpsModulePersistenceServiceImpl(mockYangResourceRepository, mockSchemaSetRepository, + mockDataspaceRepository, mockModuleReferenceRepository) } def 'Store schema set error scenario: #scenario.'() { given: 'no yang resource are currently saved' - yangResourceRepositoryMock.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList() + mockYangResourceRepository.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList() and: 'persisting yang resource raises db constraint exception (in case of concurrent requests for example)' - yangResourceRepositoryMock.saveAll(_) >> { throw dbException } + mockYangResourceRepository.saveAll(_) >> { throw dbException } when: 'attempt to store schema set ' def newYangResourcesNameToContentMap = [(yangResourceName):yangResourceContent] objectUnderTest.storeSchemaSet('my-dataspace', 'my-schema-set', newYangResourcesNameToContentMap) @@ -90,4 +89,15 @@ class CpsModulePersistenceServiceSpec extends Specification { 'other data failure' | otherIntegrityException || DataIntegrityViolationException | 'another integrity exception' } + def 'Upgrade existing schema set'() { + given: 'old schema has empty yang resource' + mockYangResourceRepository.findAllByChecksumIn(_ as Collection<String>) >> Collections.emptyList() + 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')]) + then: 'no exception is thrown ' + noExceptionThrown() + } + } |