diff options
author | Juhi Arora <juhi.arora1@bell.ca> | 2022-06-06 13:30:03 -0400 |
---|---|---|
committer | kuldipr <kuldip.rai@amdocs.com> | 2022-09-01 08:48:15 -0400 |
commit | 89c17ae2e3222b98450e7c8d17566cd76ecf113b (patch) | |
tree | d8ca903df5dd3c189d87aef3dac6065f22e1b303 /ms/blueprintsprocessor/functions/resource-resolution/src/main | |
parent | e8e0087c6aea0bf9b2d3d17207574e8db84ba0f3 (diff) |
CDS max-occurrence feature
As part of occurrence feature, one or more version of the resource
resolution can be resolved. However, user did not have granular
control in case the user wants to resolve a specific value once and
never again.
Max-Occurrence feature implements the granular control to be give the
user an option to specify the max number of times a resource to be
resolved. It is specified as part of mapping in a cba. Max-occurrence
value of 0 or not specifying it explicitly denotes the current default
behaviour of unlimited resoltions. If a user specify a particular
max-occurrence value then the resource is resolved that many times in
subsquent requests and never again once we reached the max-occurrence
limit of resource resolutions.
Issue-ID: CCSDK-3736
Change-Id: Ie18764a313530e36be14531d8c7b93bf54f0b651
Signed-off-by: kuldipr <kuldip.rai@amdocs.com>
Diffstat (limited to 'ms/blueprintsprocessor/functions/resource-resolution/src/main')
3 files changed, 150 insertions, 1 deletions
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt index a3c137807..427d48f3c 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt @@ -218,7 +218,18 @@ open class ResourceResolutionServiceImpl( val occurrence = findNextOccurrence(bluePrintRuntimeService, properties, artifactPrefix) log.info("Always perform new resolutions - next occurrence: $occurrence") propertiesMutableMap[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence - // Since we are performing new resolution, simply pass empty list. + // If resolution has already been performed previously then may need to update some assignments + val resourceAssignmentFilteredMap: Map<String, ResourceAssignment> = + resourceAssignments.filter { it.maxOccurrence != null && it.maxOccurrence!! in 1 until occurrence } + .associateBy { it.name } + if (occurrence > 1 && resourceAssignmentFilteredMap.isNotEmpty()) + updateResourceAssignmentByOccurrence( + bluePrintRuntimeService as ResourceAssignmentRuntimeService, + propertiesMutableMap, + artifactPrefix, + resourceAssignmentFilteredMap as MutableMap<String, ResourceAssignment> + ) + // we may be performing new resolution for some resources, simply pass an empty list. emptyList() } else { isNewResolution(bluePrintRuntimeService, properties, artifactPrefix) @@ -500,6 +511,92 @@ open class ResourceResolutionServiceImpl( } } + /** + * Utility to update the resourceAssignments with existing resolutions if maxOccurrence is defined on it. + */ + private suspend fun updateResourceAssignmentByOccurrence( + raRuntimeService: ResourceAssignmentRuntimeService, + properties: Map<String, Any>, + artifactPrefix: String, + resourceAssignmentFilteredMap: MutableMap<String, ResourceAssignment> + ) { + val resourceResolutionMap = + getResourceResolutionByLastOccurrence(raRuntimeService, properties, artifactPrefix).associateBy { it.name } + + resourceAssignmentFilteredMap + .filter { + resourceResolutionMap.containsKey(it.key) && compareOne( + resourceResolutionMap[it.key]!!, + it.value + ) + } + .forEach { raMapEntry -> + val resourceResolution = resourceResolutionMap[raMapEntry.key] + val resourceAssignment = raMapEntry.value + + resourceResolution?.value?.let { + val value = it.asJsonType(resourceAssignment.property!!.type) + resourceAssignment.property!!.value = value + ResourceAssignmentUtils.setResourceDataValue( + resourceAssignment, + raRuntimeService, + value + ) + } + resourceAssignment.status = resourceResolution?.status + resourceResolutionDBService.write( + properties, + raRuntimeService, + artifactPrefix, + resourceAssignment + ) + } + } + + /** + * Utility to find resource resolution based on occurrence. + */ + private suspend fun getResourceResolutionByLastOccurrence( + bluePrintRuntimeService: BluePrintRuntimeService<*>, + properties: Map<String, Any>, + artifactPrefix: String + ): List<ResourceResolution> { + + val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String + val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String + val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String + + // This should not happen since the request has already been validated but worth to check it here as well. + if (resourceType.isEmpty() && resourceId.isEmpty() && resolutionKey.isEmpty()) { + throw BluePrintProcessorException( + "Can't proceed to get last occurrence: " + + "Either provide a resolution-key OR combination of resource-id and resource-type" + ) + } + val metadata = bluePrintRuntimeService.bluePrintContext().metadata!! + val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!! + val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!! + + return if (resolutionKey.isNotEmpty()) { + resourceResolutionDBService.findLastNOccurrences( + blueprintName, + blueprintVersion, + artifactPrefix, + resolutionKey, + 1 + ).takeIf { it.isNotEmpty() }?.values?.first() ?: emptyList() + } else { + resourceResolutionDBService.findLastNOccurrences( + blueprintName, + blueprintVersion, + artifactPrefix, + resourceId, + resourceType, + 1 + ).takeIf { it.isNotEmpty() }?.values?.first() ?: emptyList() + } + } + // Comparison between what we have in the database vs what we have to assign. private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean { return ( 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 aa7b61f6a..bd8f9d27d 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 @@ -170,6 +170,35 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso } /** + * This returns the resolutions of last N 'occurrences'. + * + * @param blueprintName + * @param blueprintVersion + * @param artifactPrefix + * @param resourceId + * @param resourceType + * @param lastN + */ + suspend fun findLastNOccurrences( + blueprintName: String, + blueprintVersion: String, + artifactPrefix: String, + resourceId: String, + resourceType: String, + lastN: Int + ): Map<Int, List<ResourceResolution>> = withContext(Dispatchers.IO) { + + resourceResolutionRepository.findLastNOccurrences( + resourceId, + resourceType, + blueprintName, + blueprintVersion, + artifactPrefix, + lastN + ).groupBy(ResourceResolution::occurrence).toSortedMap(reverseOrder()) + } + + /** * This returns the resolutions with 'occurrence' value between begin and end. * * @param blueprintName 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 9317a71cd..5861cf8cb 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 @@ -77,6 +77,29 @@ interface ResourceResolutionRepository : JpaRepository<ResourceResolution, Strin @Query( value = """ + SELECT * FROM RESOURCE_RESOLUTION WHERE resource_id = :resourceId + AND resource_type =:resourceType AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName + AND occurrence > ( + select max(occurrence) - :lastN from RESOURCE_RESOLUTION + WHERE resource_id = :resourceId + AND resource_type =:resourceType AND blueprint_name = :blueprintName + AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName) + ORDER BY occurrence DESC, creation_date DESC + """, + nativeQuery = true + ) + fun findLastNOccurrences( + @Param("resourceId")resourceId: String, + @Param("resourceType")resourceType: String, + @Param("blueprintName")blueprintName: String, + @Param("blueprintVersion")blueprintVersion: String, + @Param("artifactName")artifactName: String, + @Param("lastN")begin: Int + ): List<ResourceResolution> + + @Query( + value = """ SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName AND occurrence BETWEEN :begin AND :end |