Add functions for retrieving multiple artifacts 63/126463/5
authorArne Chrestin <arne.chrestin@telekom.de>
Mon, 20 Dec 2021 12:16:57 +0000 (13:16 +0100)
committerArne Chrestin <arne.chrestin@telekom.de>
Mon, 17 Jan 2022 14:35:20 +0000 (14:35 +0000)
Add the functions AbstractComponentFunction
.storedArtifactNamesAndResolutionKeysNB()
to retrieve all artifact_names and resolution_keys for a given
blueprint and AbstractComponentFunction
.storedResolutionKeysForArtifactNameNB(artifactName)
to retrieve all resolution_keys for a given blueprint and artifact_name.
To support the required JPA query, an interface
TemplateResolutionSelector was added in resource-resolution.

Issue-ID: CCSDK-3543
Signed-off-by: arne.chrestin@telekom.de
Change-Id: I1907949e49134ec13d28a876474f08502759762f

ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionExtensions.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionRepository.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionSelector.kt [new file with mode: 0644]
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionService.kt

index 4774ad5..f7d41bc 100644 (file)
@@ -39,6 +39,18 @@ suspend fun AbstractComponentFunction.storedContentFromResolvedArtifactNB(
         .resolveFromDatabase(bluePrintRuntimeService, artifactName, resolutionKey)
 }
 
+suspend fun AbstractComponentFunction.storedResolutionKeysForArtifactNameNB(
+    artifactName: String
+): List<String> {
+    return BluePrintDependencyService.resourceResolutionService()
+        .resolveResolutionKeysFromDatabase(bluePrintRuntimeService, artifactName)
+}
+
+suspend fun AbstractComponentFunction.storedArtifactNamesAndResolutionKeysNB(): Map<String,List<String>> {
+    return BluePrintDependencyService.resourceResolutionService()
+        .resolveArtifactNamesAndResolutionKeysFromDatabase(bluePrintRuntimeService)
+}
+
 suspend fun AbstractComponentFunction.storedResourceResolutionsNB(
     resolutionKey: String,
     artifactName: String,
@@ -72,5 +84,5 @@ fun AbstractComponentFunction.storedContentFromResolvedArtifact(resolutionKey: S
     }
 
 fun AbstractComponentFunction.contentFromResolvedArtifact(artifactPrefix: String): String = runBlocking {
-    contentFromResolvedArtifactNB(artifactPrefix)
-}
+        contentFromResolvedArtifactNB(artifactPrefix)
+    }
\ No newline at end of file
index 6b4260a..2352c7d 100644 (file)
@@ -61,6 +61,15 @@ interface ResourceResolutionService {
         resolutionKey: String
     ): String
 
+    suspend fun resolveResolutionKeysFromDatabase(
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
+        artifactTemplate: String
+    ): List<String>
+
+    suspend fun resolveArtifactNamesAndResolutionKeysFromDatabase(
+        bluePrintRuntimeService: BluePrintRuntimeService<*>
+    ): Map<String, List<String>>
+
     suspend fun resolveResources(
         bluePrintRuntimeService: BluePrintRuntimeService<*>,
         nodeTemplateName: String,
@@ -125,6 +134,23 @@ open class ResourceResolutionServiceImpl(
         )
     }
 
+    override suspend fun resolveResolutionKeysFromDatabase(
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
+        artifactTemplate: String
+    ): List<String> {
+        return templateResolutionDBService.findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactName(
+            bluePrintRuntimeService,
+            artifactTemplate
+        )
+    }
+
+    override suspend fun resolveArtifactNamesAndResolutionKeysFromDatabase(
+        bluePrintRuntimeService: BluePrintRuntimeService<*>): Map<String, List<String>> {
+        return templateResolutionDBService.findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersion(
+            bluePrintRuntimeService
+        )
+    }
+
     override suspend fun resolveResources(
         bluePrintRuntimeService: BluePrintRuntimeService<*>,
         nodeTemplateName: String,
@@ -472,4 +498,4 @@ open class ResourceResolutionServiceImpl(
             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()
         )
     }
-}
+}
\ No newline at end of file
index 642a41b..3df6137 100644 (file)
@@ -16,6 +16,8 @@
 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
 
 import org.springframework.data.jpa.repository.JpaRepository
+import org.springframework.data.jpa.repository.Query
+import org.springframework.data.repository.query.Param
 import org.springframework.stereotype.Repository
 import javax.transaction.Transactional
 
@@ -39,6 +41,25 @@ interface TemplateResolutionRepository : JpaRepository<TemplateResolution, Strin
         occurrence: Int
     ): TemplateResolution?
 
+    @Query(
+        "select tr.resolutionKey from TemplateResolution tr where tr.blueprintName = :blueprintName and tr.blueprintVersion = :blueprintVersion and tr.artifactName = :artifactName and tr.occurrence = :occurrence"
+    )
+    fun findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
+        @Param("blueprintName") blueprintName: String?,
+        @Param("blueprintVersion") blueprintVersion: String?,
+        @Param("artifactName") artifactName: String,
+        @Param("occurrence") occurrence: Int
+    ): List<String>?
+
+    @Query(
+        "select tr.artifactName as artifactName, tr.resolutionKey as resolutionKey from TemplateResolution tr where tr.blueprintName = :blueprintName and tr.blueprintVersion = :blueprintVersion and tr.occurrence = :occurrence"
+    )
+    fun findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersionAndOccurrence(
+        @Param("blueprintName") blueprintName: String?,
+        @Param("blueprintVersion") blueprintVersion: String?,
+        @Param("occurrence") occurrence: Int
+    ): List<TemplateResolutionSelector>?
+
     @Transactional
     fun deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
         resourceId: String,
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionSelector.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/TemplateResolutionSelector.kt
new file mode 100644 (file)
index 0000000..4fadf96
--- /dev/null
@@ -0,0 +1,6 @@
+package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
+
+interface TemplateResolutionSelector {
+    fun getArtifactName(): String
+    fun getResolutionKey(): String
+}
\ No newline at end of file
index 4bdd5d9..fd4cc8c 100644 (file)
@@ -70,6 +70,57 @@ class TemplateResolutionService(private val templateResolutionRepository: Templa
             )?.result ?: throw EmptyResultDataAccessException(1)
         }
 
+    suspend fun findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactName(
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
+        artifactPrefix: String,
+        occurrence: Int = 1
+    ): List<String> =
+        withContext(Dispatchers.IO) {
+
+            val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
+
+            val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
+            val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
+
+            templateResolutionRepository.findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
+                blueprintName,
+                blueprintVersion,
+                artifactPrefix,
+                occurrence
+            ) ?: throw EmptyResultDataAccessException(1)
+        }
+
+    suspend fun findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersion(
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
+        occurrence: Int = 1
+    ): Map<String,List<String>> =
+        withContext(Dispatchers.IO) {
+
+            val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
+
+            val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
+            val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
+
+            val resultList = templateResolutionRepository.findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersionAndOccurrence(
+                blueprintName,
+                blueprintVersion,
+                occurrence
+            ) ?: throw EmptyResultDataAccessException(1)
+
+            var resultMap: MutableMap<String, MutableList<String>> = mutableMapOf()
+
+            resultList.forEach { it ->
+                if (!resultMap.containsKey(it.getArtifactName())) {
+                    resultMap[it.getArtifactName()] = mutableListOf(it.getResolutionKey())
+                } else {
+                    resultMap[it.getArtifactName()]!!.add(it.getResolutionKey())
+                }
+            }
+            resultMap
+                .mapValues { it.value.toList() }
+                .toMap()
+        }
+
     suspend fun findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
         blueprintName: String,
         blueprintVersion: String,
@@ -190,4 +241,4 @@ class TemplateResolutionService(private val templateResolutionRepository: Templa
                 throw BluePrintException("Failed to store resource api result.", ex)
             }
         }
-}
+}
\ No newline at end of file