From fa536ca8bd901dc51833cfe906f7b22846674d2f Mon Sep 17 00:00:00 2001 From: Jozsef Csongvai Date: Mon, 16 May 2022 11:09:06 -0400 Subject: Enable deleting resources by lastN occurrences Also enable deletion using resource-type and resource-id. Issue-ID: CCSDK-3735 Signed-off-by: Jozsef Csongvai Signed-off-by: kuldipr Change-Id: Id1b487fce97f582bd3781dfd5bcff61a8df08c5c --- .../resolution/db/ResourceResolutionDBService.kt | 70 +++++++++++++++++++--- .../resolution/db/ResourceResolutionRepository.kt | 61 ++++++++++++++++++- .../db/ResourceResolutionDBServiceTest.kt | 51 ++++++++++++++-- 3 files changed, 166 insertions(+), 16 deletions(-) (limited to 'ms/blueprintsprocessor/functions') diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBService.kt index ed9e6d1d6..3041fa773 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBService.kt @@ -26,6 +26,7 @@ import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment import org.slf4j.LoggerFactory import org.springframework.dao.EmptyResultDataAccessException import org.springframework.stereotype.Service +import java.lang.IllegalArgumentException import java.util.UUID @Service @@ -281,27 +282,80 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso } /** - * This is a deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey method to delete resources - * associated to a specific resolution-key + * This method to deletes resources associated to a specific resolution-key * * @param blueprintName name of the CBA * @param blueprintVersion version of the CBA * @param artifactName name of the artifact * @param resolutionKey value of the resolution-key + * @param lastNOccurrences number of occurrences to delete starting from the last, + * all occurrences will be deleted when null + * + * @return number of deleted rows */ - suspend fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey( + fun deleteResources( blueprintName: String, blueprintVersion: String, artifactName: String, - resolutionKey: String - ) { - resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey( + resolutionKey: String, + lastNOccurrences: Int? + ): Int = lastNOccurrences?.let { + if (lastNOccurrences < 0) { + throw IllegalArgumentException("last N occurrences must be a positive integer") + } + resourceResolutionRepository.deleteLastNOccurences( blueprintName, blueprintVersion, artifactName, - resolutionKey + resolutionKey, + it ) - } + } ?: resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey( + blueprintName, + blueprintVersion, + artifactName, + resolutionKey + ) + + /** + * This method to deletes resources associated to a specific resourceType and resourceId + * + * @param blueprintName name of the CBA + * @param blueprintVersion version of the CBA + * @param artifactName name of the artifact + * @param resourceType value of the resourceType + * @param resourceId value of the resourceId + * @param lastNOccurrences number of occurrences to delete starting from the last, + * all occurrences will be deleted when null + * + * @return number of deleted rows + */ + fun deleteResources( + blueprintName: String, + blueprintVersion: String, + artifactName: String, + resourceType: String, + resourceId: String, + lastNOccurrences: Int? + ): Int = lastNOccurrences?.let { + if (lastNOccurrences < 0) { + throw IllegalArgumentException("last N occurrences must be a positive integer") + } + resourceResolutionRepository.deleteLastNOccurences( + blueprintName, + blueprintVersion, + artifactName, + resourceType, + resourceId, + it + ) + } ?: resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceTypeAndResourceId( + blueprintName, + blueprintVersion, + artifactName, + resourceType, + resourceId + ) suspend fun deleteResourceResolutionList(listResourceResolution: List) = withContext(Dispatchers.IO) { try { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionRepository.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionRepository.kt index 4b707b04e..30969f1f6 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionRepository.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionRepository.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 @@ -155,9 +156,65 @@ interface ResourceResolutionRepository : JpaRepository ( + SELECT max(occurrence) - :lastN FROM RESOURCE_RESOLUTION + WHERE resolution_key = :resolutionKey AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName) + """, + nativeQuery = true + ) + fun deleteLastNOccurences( + @Param("blueprintName") blueprintName: String, + @Param("blueprintVersion") blueprintVersion: String, + @Param("artifactName") artifactName: String, + @Param("resolutionKey") resolutionKey: String, + @Param("lastN") lastN: Int + ): Int + + @Transactional + @Modifying + @Query( + value = """ + DELETE FROM RESOURCE_RESOLUTION + WHERE resource_type = :resourceType AND resource_id = :resourceId + AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName + AND occurrence > ( + SELECT max(occurrence) - :lastN FROM RESOURCE_RESOLUTION + WHERE resource_type = :resourceType AND resource_id = :resourceId + AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName) + """, + nativeQuery = true ) + fun deleteLastNOccurences( + @Param("blueprintName") blueprintName: String, + @Param("blueprintVersion") blueprintVersion: String, + @Param("artifactName") artifactName: String, + @Param("resourceType") resourceType: String, + @Param("resourceId") resourceId: String, + @Param("lastN") lastN: Int + ): Int } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt index 8d4109fcf..69e7a646a 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt @@ -321,15 +321,54 @@ open class ResourceResolutionDBServiceTest { } @Test - fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyTest() { + fun deleteResourcesResolutionKeyAll() { every { - resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(any(), any(), any(), any()) - } returns Unit + resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(blueprintName, blueprintVersion, artifactPrefix, resolutionKey) + } returns 3 runBlocking { - val res = resourceResolutionDBService.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey( - blueprintName, blueprintVersion, artifactPrefix, resolutionKey + val res = resourceResolutionDBService.deleteResources( + blueprintName, blueprintVersion, artifactPrefix, resolutionKey, null ) - assertEquals(Unit, res) + assertEquals(3, res) + } + } + + @Test + fun deleteResourcesResolutionKeyLastN() { + every { + resourceResolutionRepository.deleteLastNOccurences(blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1) + } returns 4 + runBlocking { + val res = resourceResolutionDBService.deleteResources( + blueprintName, blueprintVersion, artifactPrefix, resolutionKey, 1 + ) + assertEquals(4, res) + } + } + + @Test + fun deleteResourcesResourceIdAndTypeAll() { + every { + resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceTypeAndResourceId(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId) + } returns 3 + runBlocking { + val res = resourceResolutionDBService.deleteResources( + blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, null + ) + assertEquals(3, res) + } + } + + @Test + fun deleteResourcesResourceIdAndTypeLastN() { + every { + resourceResolutionRepository.deleteLastNOccurences(blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, 2) + } returns 6 + runBlocking { + val res = resourceResolutionDBService.deleteResources( + blueprintName, blueprintVersion, artifactPrefix, resourceType, resourceId, 2 + ) + assertEquals(6, res) } } } -- cgit 1.2.3-korg