diff options
author | niamhcore <niamh.core@est.tech> | 2021-09-01 17:12:28 +0100 |
---|---|---|
committer | aditya puthuparambil <aditya.puthuparambil@bell.ca> | 2021-09-06 14:06:16 +0000 |
commit | d0422226b45f752086c9920bdead753c644d9289 (patch) | |
tree | 219118e284927589ad69dc4ded6d520e76e77530 /cps-ri/src | |
parent | c27465347d10b573e7a211c0b11b7c8a6fd792c0 (diff) |
P1 - Get module name and revision persistence layer
Rename ModuleReference to ExtendedModuleReference
Create new ModuleReference class (moduleName and revision)
Update existing sync model flow to use new module reference class
Create native query to get module name and revision
Issue-ID: CPS-485
Signed-off-by: niamhcore <niamh.core@est.tech>
Change-Id: I0d0da7742ceef3a68e83a5dd4852091e6adf138b
Diffstat (limited to 'cps-ri/src')
5 files changed, 96 insertions, 39 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 1c7828f32e..d7b882f6ee 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 @@ -116,11 +116,21 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ } @Override - public List<ModuleReference> getAllYangResourcesModuleReferences() { - final List<YangResourceModuleReference> yangResourceModuleReferenceList = - yangResourceRepository.findAllModuleNameAndRevision(); + public Collection<ModuleReference> getAllYangResourceModuleReferences(final String dataspaceName) { + final Set<YangResourceModuleReference> yangResourceModuleReferenceList = + yangResourceRepository.findAllModuleReferences(dataspaceName); return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference) - .collect(Collectors.toList()); + .collect(Collectors.toList()); + } + + @Override + public Collection<ModuleReference> getAllYangResourceModuleReferences(final String dataspaceName, + final String anchorName) { + final Set<YangResourceModuleReference> yangResourceModuleReferenceList = + yangResourceRepository + .findAllModuleReferences(dataspaceName, anchorName); + return yangResourceModuleReferenceList.stream().map(CpsModulePersistenceServiceImpl::toModuleReference) + .collect(Collectors.toList()); } @Override @@ -148,15 +158,15 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ @Transactional public void storeSchemaSetFromModules(final String dataspaceName, final String schemaSetName, final Map<String, String> newYangResourcesModuleNameToContentMap, - final List<ModuleReference> moduleReferenceList) { + final List<ModuleReference> moduleReferences) { storeSchemaSet(dataspaceName, schemaSetName, newYangResourcesModuleNameToContentMap); final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final var schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); final List<Long> listOfYangResourceIds = new ArrayList<>(); - moduleReferenceList.forEach(moduleReference -> + moduleReferences.forEach(moduleReference -> listOfYangResourceIds.add(yangResourceRepository.getIdByModuleNameAndRevision( - moduleReference.getName(), moduleReference.getRevision()))); + moduleReference.getModuleName(), moduleReference.getRevision()))); yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), listOfYangResourceIds); } @@ -325,10 +335,11 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ return checksum; } - private static ModuleReference toModuleReference(final YangResourceModuleReference yangResourceModuleReference) { + private static ModuleReference toModuleReference( + final YangResourceModuleReference yangResourceModuleReference) { return ModuleReference.builder() - .name(yangResourceModuleReference.getModuleName()) - .revision(yangResourceModuleReference.getRevision()) - .build(); + .moduleName(yangResourceModuleReference.getModuleName()) + .revision(yangResourceModuleReference.getRevision()) + .build(); } } 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 0b48eaaf63..b16b284a2a 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 @@ -37,8 +37,32 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum); - @Query(value = "SELECT module_name, revision FROM yang_resource", nativeQuery = true) - List<YangResourceModuleReference> findAllModuleNameAndRevision(); + @Query(value = "SELECT DISTINCT\n" + + "yr.module_name AS module_name,\n" + + "yr.revision AS revision\n" + + "FROM\n" + + "dataspace d\n" + + "JOIN schema_set ss ON ss.dataspace_id = d.id\n" + + "JOIN schema_set_yang_resources ssyr ON ssyr.schema_set_id = ss.id\n" + + "JOIN yang_resource yr ON yr.id = ssyr.yang_resource_id\n" + + "WHERE\n" + + "d.name = :dataspaceName", nativeQuery = true) + Set<YangResourceModuleReference> findAllModuleReferences(@Param("dataspaceName") String dataspaceName); + + @Query(value = "SELECT DISTINCT\n" + + "yr.module_Name AS module_name,\n" + + "yr.revision AS revision\n" + + "FROM\n" + + "dataspace d\n" + + "JOIN anchor a ON a.dataspace_id = d.id\n" + + "JOIN schema_set ss ON ss.dataspace_id = a.dataspace_id\n" + + "JOIN schema_set_yang_resources ssyr ON ssyr.schema_set_id = ss.id\n" + + "JOIN yang_resource yr ON yr.id = ssyr.yang_resource_id\n" + + "WHERE\n" + + "d.name = :dataspaceName AND\n" + + "a.name =:anchorName", nativeQuery = true) + Set<YangResourceModuleReference> findAllModuleReferences( + @Param("dataspaceName") String dataspaceName, @Param("anchorName") String anchorName); @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true) Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision); diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy index 7a16a97d4b..a139830a2b 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsModulePersistenceServiceIntegrationSpec.groovy @@ -20,22 +20,23 @@ */ package org.onap.cps.spi.impl -import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED -import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED - import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.CpsModulePersistenceService import org.onap.cps.spi.entities.YangResourceEntity -import org.onap.cps.spi.exceptions.DataspaceNotFoundException import org.onap.cps.spi.exceptions.AlreadyDefinedException +import org.onap.cps.spi.exceptions.DataspaceNotFoundException import org.onap.cps.spi.exceptions.SchemaSetInUseException import org.onap.cps.spi.exceptions.SchemaSetNotFoundException import org.onap.cps.spi.model.ModuleReference +import org.onap.cps.spi.model.ExtendedModuleReference import org.onap.cps.spi.repository.AnchorRepository import org.onap.cps.spi.repository.SchemaSetRepository import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.jdbc.Sql +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED + class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase { @Autowired @@ -71,13 +72,13 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase static final String NEW_RESOURCE_CHECKSUM = 'b13faef573ed1374139d02c40d8ce09c80ea1dc70e63e464c1ed61568d48d539' static final String NEW_RESOURCE_MODULE_NAME = 'stores' static final String NEW_RESOURCE_REVISION = '2020-09-15' - static final ModuleReference newModuleReference = ModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME) + static final ExtendedModuleReference newModuleReference = ExtendedModuleReference.builder().name(NEW_RESOURCE_MODULE_NAME) .revision(NEW_RESOURCE_REVISION).build() def newYangResourcesNameToContentMap = [(NEW_RESOURCE_NAME):NEW_RESOURCE_CONTENT] - def allYangResourcesModuleAndRevisionList = [ModuleReference.builder().build(),ModuleReference.builder().build(), - ModuleReference.builder().build(),ModuleReference.builder().build(), - ModuleReference.builder().build(), newModuleReference] + def allYangResourcesModuleAndRevisionList = [new ExtendedModuleReference(name: 'MODULE-NAME-002',namespace:null, revision: 'REVISION-002'), new ExtendedModuleReference(name: 'MODULE-NAME-003',namespace:null, revision: 'REVISION-003'), + new ExtendedModuleReference(name: 'MODULE-NAME-004',namespace:null, revision: 'REVISION-004'), ExtendedModuleReference.builder().build(), + ExtendedModuleReference.builder().build(), newModuleReference] def dataspaceEntity def setup() { @@ -109,7 +110,7 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase def 'Store and retrieve new schema set from new modules and existing modules.'() { given: 'map of new modules, a list of existing modules, module reference' def mapOfNewModules = [newModule1: 'module newmodule { yang-version 1.1; revision "2021-10-12" { } }'] - def moduleReferenceForExistingModule = new ModuleReference("test","test.org","2021-10-12") + def moduleReferenceForExistingModule = new ModuleReference("test","2021-10-12") def listOfExistingModulesModuleReference = [moduleReferenceForExistingModule] def mapOfExistingModule = [test: 'module test { yang-version 1.1; revision "2021-10-12" { } }'] objectUnderTest.storeSchemaSet(DATASPACE_NAME, "someSchemaSetName", mapOfExistingModule) @@ -135,13 +136,27 @@ class CpsModulePersistenceServiceIntegrationSpec extends CpsPersistenceSpecBase } @Sql([CLEAR_DATA, SET_DATA]) - def 'Retrieving all yang resources module references.'() { - given: 'a new schema set is stored' - objectUnderTest.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, newYangResourcesNameToContentMap) - when: 'all yang resources module references are retrieved' - def result = objectUnderTest.getAllYangResourcesModuleReferences() + def 'Retrieving all yang resources module references for the given dataspace.'() { + given: 'a dataspace name' + def dataspaceName = 'DATASPACE-002' + when: 'all yang resources module references are retrieved for the given dataspace name' + def result = objectUnderTest.getAllYangResourceModuleReferences(dataspaceName) then: 'the correct resources are returned' - result.sort() == allYangResourcesModuleAndRevisionList.sort() + result.sort() == [new ModuleReference(moduleName: 'MODULE-NAME-005', revision: 'REVISION-002'), + new ModuleReference(moduleName: 'MODULE-NAME-006', revision: 'REVISION-006')] + } + + @Sql([CLEAR_DATA, SET_DATA]) + def 'Retrieving module names and revisions for the given anchor.'() { + given: 'a dataspace name and anchor name' + def dataspaceName = 'DATASPACE-001' + def anchorName = 'ANCHOR1' + when: 'all yang resources module references are retrieved for the given anchor' + def result = objectUnderTest.getAllYangResourceModuleReferences(dataspaceName, anchorName) + then: 'the correct module names and revisions are returned' + result.sort() == [new ModuleReference(moduleName: null, revision: null), new ModuleReference(moduleName: 'MODULE-NAME-002', revision: 'REVISION-002'), + new ModuleReference(moduleName: 'MODULE-NAME-003', revision: 'REVISION-002'), + new ModuleReference(moduleName: 'MODULE-NAME-004', revision: 'REVISION-004')] } @Sql([CLEAR_DATA, SET_DATA]) diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy index 5132632452..8ec5c90e9c 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsPersistenceSpecBase.groovy @@ -55,6 +55,7 @@ class CpsPersistenceSpecBase extends Specification { static final String CLEAR_DATA = '/data/clear-all.sql' static final String DATASPACE_NAME = 'DATASPACE-001' + static final String DATASPACE_NAME2 = 'DATASPACE-002' static final String SCHEMA_SET_NAME1 = 'SCHEMA-SET-001' static final String SCHEMA_SET_NAME2 = 'SCHEMA-SET-002' static final String ANCHOR_NAME1 = 'ANCHOR-001' diff --git a/cps-ri/src/test/resources/data/schemaset.sql b/cps-ri/src/test/resources/data/schemaset.sql index adfcfa172e..61600356b9 100644 --- a/cps-ri/src/test/resources/data/schemaset.sql +++ b/cps-ri/src/test/resources/data/schemaset.sql @@ -24,26 +24,32 @@ INSERT INTO DATASPACE (ID, NAME) VALUES (1001, 'DATASPACE-001'), (1002, 'DATASPACE-002'); INSERT INTO SCHEMA_SET (ID, NAME, DATASPACE_ID) VALUES - (2001, 'SCHEMA-SET-001', 1001), (2002, 'SCHEMA-SET-002', 1001), + (2001, 'SCHEMA-SET-001', 1001), + (2002, 'SCHEMA-SET-002', 1001), (2100, 'SCHEMA-SET-100', 1001), -- for removal, not referenced by anchors - (2101, 'SCHEMA-SET-101', 1001); -- for removal, having anchor and data associated - -INSERT INTO YANG_RESOURCE (ID, NAME, CONTENT, CHECKSUM) VALUES - (3001, 'module1@2020-02-02.yang', 'CONTENT-001', 'e8bdda931099310de66532e08c3fafec391db29f55c81927b168f6aa8f81b73b'), - (3002, 'module2@2020-02-02.yang', 'CONTENT-002', '7e7d48afbe066ed0a890a09081859046d3dde52300dfcdb13be5b20780353a11'), - (3003, 'module3@2020-02-02.yang', 'CONTENT-003', 'ca20c45fec8547633f05ff8905c48ffa7b02b94ec3ad4ed79922e6ba40779df3'), - (3004, 'module4@2020-02-02.yang', 'CONTENT-004', 'f6ed09d343562e4d4ae5140f3c6a55df9c53f6da8e30dda8cbd9eaf9cd449be0'), - (3100, 'orphan@2020-02-02.yang', 'ORPHAN', 'checksum'); -- for auto-removal as orphan + (2101, 'SCHEMA-SET-101', 1001), -- for removal, having anchor and data associated + (2003, 'SCHEMA-SET-003', 1002), + (2004, 'SCHEMA-SET-004', 1002); + +INSERT INTO YANG_RESOURCE (ID, NAME, CONTENT, CHECKSUM, MODULE_NAME, REVISION) VALUES + (3001, 'module1@2020-02-02.yang', 'CONTENT-001', 'e8bdda931099310de66532e08c3fafec391db29f55c81927b168f6aa8f81b73b',null,null), + (3002, 'module2@2020-02-02.yang', 'CONTENT-002', '7e7d48afbe066ed0a890a09081859046d3dde52300dfcdb13be5b20780353a11','MODULE-NAME-002','REVISION-002'), + (3003, 'module3@2020-02-02.yang', 'CONTENT-003', 'ca20c45fec8547633f05ff8905c48ffa7b02b94ec3ad4ed79922e6ba40779df3','MODULE-NAME-003','REVISION-002'), + (3004, 'module4@2020-02-02.yang', 'CONTENT-004', 'f6ed09d343562e4d4ae5140f3c6a55df9c53f6da8e30dda8cbd9eaf9cd449be0','MODULE-NAME-004','REVISION-004'), + (3100, 'orphan@2020-02-02.yang', 'ORPHAN', 'checksum',null,null), -- for auto-removal as orphan + (3005, 'module5@2020-02-02.yang', 'CONTENT-005', 'checksum-005','MODULE-NAME-005','REVISION-002'), + (3006, 'module6@2020-02-02.yang', 'CONTENT-006', 'checksum-006','MODULE-NAME-006','REVISION-006'); INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES (2001, 3001), (2001, 3002), (2002, 3003), (2002, 3004), (2100, 3003), (2100, 3100), -- orphan removal case - (2101, 3003), (2101, 3004); + (2101, 3003), (2101, 3004), + (2003, 3005), (2004, 3006); INSERT INTO ANCHOR (ID, NAME, DATASPACE_ID, SCHEMA_SET_ID) VALUES -- anchors for removal (6001, 'ANCHOR1', 1001, 2101), (6002, 'ANCHOR2', 1001, 2101); INSERT INTO FRAGMENT (ID, XPATH, ANCHOR_ID, DATASPACE_ID) VALUES - (7001, '/XPATH', 6001, 1001); + (7001, '/XPATH', 6001, 1001);
\ No newline at end of file |