Enable versioned resource resolution by using occurrence
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / db / ResourceResolutionDBService.kt
index 592408b..1f0171f 100644 (file)
@@ -18,9 +18,9 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
-import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
-import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
-import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
+import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
 import org.slf4j.LoggerFactory
@@ -34,7 +34,7 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
     private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
 
     suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
-        bluePrintRuntimeService: BlueprintRuntimeService<*>,
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
         key: String,
         occurrence: Int,
         artifactPrefix: String
@@ -42,8 +42,8 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
         return try {
             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
 
-            val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
-            val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
+            val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
+            val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
 
             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
                 blueprintName,
@@ -58,7 +58,7 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
     }
 
     suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
-        bluePrintRuntimeService: BlueprintRuntimeService<*>,
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
         resourceId: String,
         resourceType: String,
         occurrence: Int,
@@ -68,8 +68,8 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
 
             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
 
-            val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
-            val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
+            val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
+            val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
 
             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
                 blueprintName,
@@ -90,7 +90,7 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
         artifactPrefix: String,
         resolutionKey: String,
         name: String
-    ): ResourceResolution = withContext(Dispatchers.IO) {
+    ): ResourceResolution? = withContext(Dispatchers.IO) {
 
         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
             resolutionKey,
@@ -134,15 +134,15 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
 
     suspend fun write(
         properties: Map<String, Any>,
-        bluePrintRuntimeService: BlueprintRuntimeService<*>,
+        bluePrintRuntimeService: BluePrintRuntimeService<*>,
         artifactPrefix: String,
         resourceAssignment: ResourceAssignment
     ): ResourceResolution = withContext(Dispatchers.IO) {
 
         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
 
-        val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
-        val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
+        val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
+        val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
 
         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
@@ -182,7 +182,7 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
         resourceResolution.resourceType = resourceType
         resourceResolution.resourceId = resourceId
         resourceResolution.value = resourceAssignment.property?.value?.let {
-            if (BlueprintConstants.STATUS_SUCCESS == resourceAssignment.status)
+            if (BluePrintConstants.STATUS_SUCCESS == resourceAssignment.status)
                 JacksonUtils.getValue(it).toString()
             else ""
         } ?: ""
@@ -190,12 +190,12 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
         resourceResolution.dictionaryName = resourceAssignment.dictionaryName
         resourceResolution.dictionaryVersion = resourceAssignment.version
         resourceResolution.dictionarySource = resourceAssignment.dictionarySource
-        resourceResolution.status = resourceAssignment.status ?: BlueprintConstants.STATUS_FAILURE
+        resourceResolution.status = resourceAssignment.status ?: BluePrintConstants.STATUS_FAILURE
 
         try {
             resourceResolutionRepository.saveAndFlush(resourceResolution)
         } catch (ex: Exception) {
-            throw BlueprintException("Failed to store resource resolution result.", ex)
+            throw BluePrintException("Failed to store resource resolution result.", ex)
         }
     }
 
@@ -221,4 +221,60 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
             resolutionKey
         )
     }
+
+    suspend fun deleteResourceResolutionList(listResourceResolution: List<ResourceResolution>) = withContext(Dispatchers.IO) {
+        try {
+            resourceResolutionRepository.deleteInBatch(listResourceResolution)
+        } catch (ex: Exception) {
+            throw BluePrintException("Failed to batch delete resource resolution", ex)
+        }
+    }
+
+    /**
+     * This method returns the (highest occurrence + 1) of resource resolutions if present in DB, returns 1 otherwise.
+     * The 'occurrence' is used to persist new resource resolution in the DB.
+     *
+     * @param resolutionKey
+     * @param blueprintName
+     * @param blueprintVersion
+     * @param artifactPrefix
+     */
+    suspend fun findNextOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
+        resolutionKey: String,
+        blueprintName: String,
+        blueprintVersion: String,
+        artifactPrefix: String
+    ) = withContext(Dispatchers.IO) {
+        val maxOccurrence = resourceResolutionRepository.findMaxOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
+            resolutionKey,
+            blueprintName,
+            blueprintVersion,
+            artifactPrefix
+        )
+        maxOccurrence?.inc() ?: 1
+    }
+
+    /**
+     * This method returns the (highest occurrence + 1) of resource resolutions if present in DB, returns 1 otherwise.
+     * The 'occurrence' is used to persist new resource resolution in the DB.
+     *
+     * @param blueprintName
+     * @param blueprintVersion
+     * @param resourceId
+     * @param resourceType
+     */
+    suspend fun findNextOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
+        blueprintName: String,
+        blueprintVersion: String,
+        resourceId: String,
+        resourceType: String
+    ) = withContext(Dispatchers.IO) {
+        val maxOccurrence = resourceResolutionRepository.findMaxOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
+            blueprintName,
+            blueprintVersion,
+            resourceId,
+            resourceType
+        )
+        maxOccurrence?.inc() ?: 1
+    }
 }