diff options
author | Jozsef Csongvai <jozsef.csongvai@bell.ca> | 2022-05-16 11:12:51 -0400 |
---|---|---|
committer | kuldipr <kuldip.rai@amdocs.com> | 2022-08-29 16:01:31 -0400 |
commit | 4f0ff92093a006ec0f2e29f695660e5476d890cc (patch) | |
tree | 294c09ae658b24d6e4e577739cb0891c6616838c /ms/blueprintsprocessor/functions/resource-resolution | |
parent | f85436b2dbe0da6b9951f913fc73a895d4f2924a (diff) |
Add endpoint for deleting templates
Issue-ID: CCSDK-3735
Signed-off-by: Jozsef Csongvai <jozsef.csongvai@bell.ca>
Signed-off-by: kuldipr <kuldip.rai@amdocs.com>
Change-Id: I80d0da87651933da103e79d878902da743bb134e
Diffstat (limited to 'ms/blueprintsprocessor/functions/resource-resolution')
3 files changed, 162 insertions, 0 deletions
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionRepository.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionRepository.kt index 0d8946ece..1ee9f7999 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionRepository.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionRepository.kt @@ -16,6 +16,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Modifying import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.query.Param import org.springframework.stereotype.Repository @@ -162,4 +163,69 @@ interface TemplateResolutionRepository : JpaRepository<TemplateResolution, Strin artifactName: String, occurrence: Int ) + + @Transactional + fun deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName( + resourceId: String, + resourceType: String, + blueprintName: String, + blueprintVersion: String, + artifactName: String + ): Int + + @Transactional + fun deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName( + key: String, + blueprintName: String, + blueprintVersion: String, + artifactName: String + ): Int + + @Transactional + @Modifying + @Query( + value = """ + DELETE FROM TEMPLATE_RESOLUTION WHERE resolution_key = :resolutionKey + AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion + AND artifact_name = :artifactName + AND occurrence > ( + SELECT MAX(occurrence) - :lastN FROM TEMPLATE_RESOLUTION + WHERE resolution_key = :resolutionKey AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName + ) + """, + nativeQuery = true + ) + fun deleteTemplates( + blueprintName: String, + blueprintVersion: String, + artifactName: String, + resolutionKey: String, + lastN: Int + ): Int + + @Transactional + @Modifying + @Query( + value = """ + DELETE FROM TEMPLATE_RESOLUTION WHERE resource_type = :resourceType + AND resource_id = :resourceId AND artifact_name = :artifactName + AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion + AND occurrence > ( + SELECT MAX(occurrence) - :lastN FROM TEMPLATE_RESOLUTION + WHERE resource_type = :resourceType + AND resource_id = :resourceId AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName + ) + """, + nativeQuery = true + ) + fun deleteTemplates( + blueprintName: String, + blueprintVersion: String, + artifactName: String, + resourceType: String, + resourceId: String, + lastN: Int + ): Int } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionService.kt index 906aedf09..af6d1abc1 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionService.kt @@ -25,6 +25,7 @@ import org.slf4j.LoggerFactory import org.springframework.dao.DataIntegrityViolationException import org.springframework.dao.EmptyResultDataAccessException import org.springframework.stereotype.Service +import java.lang.IllegalArgumentException import java.util.UUID @Service @@ -336,4 +337,55 @@ class TemplateResolutionService(private val templateResolutionRepository: Templa artifactPrefix ) } + + suspend fun deleteTemplates( + blueprintName: String, + blueprintVersion: String, + artifactPrefix: String, + resolutionKey: String, + lastNOccurrences: Int? + ): Int = lastNOccurrences?.let { + if (lastNOccurrences < 0) { + throw IllegalArgumentException("last N occurrences must be a positive integer") + } + templateResolutionRepository.deleteTemplates( + blueprintName, + blueprintVersion, + artifactPrefix, + resolutionKey, + it + ) + } ?: templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName( + resolutionKey, + blueprintName, + blueprintVersion, + artifactPrefix + ) + + suspend fun deleteTemplates( + blueprintName: String, + blueprintVersion: String, + artifactPrefix: String, + resourceType: String, + resourceId: String, + lastNOccurrences: Int? + ): Int = lastNOccurrences?.let { + if (lastNOccurrences < 0) { + throw IllegalArgumentException("last N occurrences must be a positive integer") + } + templateResolutionRepository.deleteTemplates( + blueprintName, + blueprintVersion, + artifactPrefix, + resourceType, + resourceId, + it + ) + } ?: templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName( + resourceId, + resourceType, + blueprintName, + blueprintVersion, + artifactPrefix + ) } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt index a2550ed5f..65304b29d 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionServiceTest.kt @@ -220,4 +220,48 @@ class TemplateResolutionServiceTest { assertEquals(tr, res) } } + + @Test + fun deleteTemplatesResolutionKeyAll() { + every { + templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(resolutionKey, blueprintName, blueprintVersion, artifactPrefix) + }.returns(1) + runBlocking { + val result = templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, artifactPrefix, resolutionKey, null) + assertEquals(1, result) + } + } + + @Test + fun deleteTemplatesResolutionKeyLastN() { + every { + templateResolutionRepository.deleteTemplates(blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1) + }.returns(1) + runBlocking { + val result = templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1) + assertEquals(1, result) + } + } + + @Test + fun deleteTemplatesResourceIdAndTypeAll() { + every { + templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix) + }.returns(1) + runBlocking { + val result = templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, null) + assertEquals(1, result) + } + } + + @Test + fun deleteTemplatesResourceIdAndTypeLastN() { + every { + templateResolutionRepository.deleteTemplates(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceType, 1) + }.returns(1) + runBlocking { + val result = templateResolutionService.deleteTemplates(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceType, 1) + assertEquals(1, result) + } + } } |