a3c1378073cb8101723896275747d93cfeb8cf4b
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceResolutionService.kt
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *  Modifications Copyright © 2018-2019 IBM, Bell Canada
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import kotlinx.coroutines.async
22 import kotlinx.coroutines.awaitAll
23 import kotlinx.coroutines.coroutineScope
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
29 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceDefinitionUtils.createResourceAssignments
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
35 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
36 import org.onap.ccsdk.cds.controllerblueprints.core.common.ApplicationConstants.LOG_REDACTED
37 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
38 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.PropertyDefinitionUtils.Companion.hasLogProtect
41 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
42 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
43 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
44 import org.slf4j.LoggerFactory
45 import org.springframework.context.ApplicationContext
46 import org.springframework.stereotype.Service
47 import java.util.UUID
48
49 data class ResourceResolutionResult(
50     val templateMap: MutableMap<String, String>,
51     val assignmentMap: MutableMap<String, JsonNode>
52 )
53
54 interface ResourceResolutionService {
55
56     fun registeredResourceSources(): List<String>
57
58     suspend fun resolveFromDatabase(
59         bluePrintRuntimeService: BluePrintRuntimeService<*>,
60         artifactTemplate: String,
61         resolutionKey: String
62     ): String
63
64     suspend fun resolveResolutionKeysFromDatabase(
65         bluePrintRuntimeService: BluePrintRuntimeService<*>,
66         artifactTemplate: String
67     ): List<String>
68
69     suspend fun resolveArtifactNamesAndResolutionKeysFromDatabase(
70         bluePrintRuntimeService: BluePrintRuntimeService<*>
71     ): Map<String, List<String>>
72
73     suspend fun resolveResources(
74         bluePrintRuntimeService: BluePrintRuntimeService<*>,
75         nodeTemplateName: String,
76         artifactNames: List<String>,
77         properties: Map<String, Any>,
78         stepName: String
79     ): ResourceResolutionResult
80
81     suspend fun resolveResources(
82         bluePrintRuntimeService: BluePrintRuntimeService<*>,
83         nodeTemplateName: String,
84         artifactPrefix: String,
85         properties: Map<String, Any>
86     ): Pair<String, MutableList<ResourceAssignment>>
87
88     /** Resolve resources for all the sources defined in a particular resource Definition[resolveDefinition]
89      * with other [resourceDefinitions] dependencies for the sources [sources]
90      * Used to get the same resource values from multiple sources. **/
91     suspend fun resolveResourceDefinition(
92         blueprintRuntimeService: BluePrintRuntimeService<*>,
93         resourceDefinitions: MutableMap<String, ResourceDefinition>,
94         resolveDefinition: String,
95         sources: List<String>
96     ):
97         MutableMap<String, JsonNode>
98
99     suspend fun resolveResourceAssignments(
100         blueprintRuntimeService: BluePrintRuntimeService<*>,
101         resourceDefinitions: MutableMap<String, ResourceDefinition>,
102         resourceAssignments: MutableList<ResourceAssignment>,
103         artifactPrefix: String,
104         properties: Map<String, Any>
105     )
106 }
107
108 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
109 open class ResourceResolutionServiceImpl(
110     private var applicationContext: ApplicationContext,
111     private var templateResolutionDBService: TemplateResolutionService,
112     private var blueprintTemplateService: BluePrintTemplateService,
113     private var resourceResolutionDBService: ResourceResolutionDBService
114 ) :
115     ResourceResolutionService {
116
117     private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
118
119     override fun registeredResourceSources(): List<String> {
120         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
121             .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
122             .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
123     }
124
125     override suspend fun resolveFromDatabase(
126         bluePrintRuntimeService: BluePrintRuntimeService<*>,
127         artifactTemplate: String,
128         resolutionKey: String
129     ): String {
130         return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
131             bluePrintRuntimeService,
132             artifactTemplate,
133             resolutionKey
134         )
135     }
136
137     override suspend fun resolveResolutionKeysFromDatabase(
138         bluePrintRuntimeService: BluePrintRuntimeService<*>,
139         artifactTemplate: String
140     ): List<String> {
141         return templateResolutionDBService.findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactName(
142             bluePrintRuntimeService,
143             artifactTemplate
144         )
145     }
146
147     override suspend fun resolveArtifactNamesAndResolutionKeysFromDatabase(
148         bluePrintRuntimeService: BluePrintRuntimeService<*>
149     ): Map<String, List<String>> {
150         return templateResolutionDBService.findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersion(
151             bluePrintRuntimeService
152         )
153     }
154
155     override suspend fun resolveResources(
156         bluePrintRuntimeService: BluePrintRuntimeService<*>,
157         nodeTemplateName: String,
158         artifactNames: List<String>,
159         properties: Map<String, Any>,
160         stepName: String
161     ): ResourceResolutionResult {
162
163         val resourceAssignmentRuntimeService =
164             ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
165
166         val templateMap: MutableMap<String, String> = hashMapOf()
167         val assignmentMap: MutableMap<String, JsonNode> = hashMapOf()
168         artifactNames.forEach { artifactName ->
169             val (resolvedStringContent, resourceAssignmentList) = resolveResources(
170                 resourceAssignmentRuntimeService, nodeTemplateName,
171                 artifactName, properties
172             )
173             val resolvedJsonContent = resourceAssignmentList
174                 .associateBy({ it.name }, { it.property?.value })
175                 .asJsonNode()
176
177             templateMap[artifactName] = resolvedStringContent
178             assignmentMap[artifactName] = resolvedJsonContent
179
180             val failedResolution = resourceAssignmentList.filter { it.status != "success" && it.property?.required == true }.map { it.name }
181             if (failedResolution.isNotEmpty()) {
182                 val errorMessages = mutableListOf("Failed to resolve required values: $failedResolution").apply {
183                     this.addAll(resourceAssignmentRuntimeService.getBluePrintError().allErrors())
184                 }
185                 bluePrintRuntimeService.getBluePrintError().addErrors(stepName, errorMessages)
186             }
187         }
188         return ResourceResolutionResult(templateMap, assignmentMap)
189     }
190
191     override suspend fun resolveResources(
192         bluePrintRuntimeService: BluePrintRuntimeService<*>,
193         nodeTemplateName: String,
194         artifactPrefix: String,
195         properties: Map<String, Any>
196     ): Pair<String, MutableList<ResourceAssignment>> {
197
198         // Template Artifact Definition Name
199         val artifactTemplate = "$artifactPrefix-template"
200         // Resource Assignment Artifact Definition Name
201         val artifactMapping = "$artifactPrefix-mapping"
202         val forceResolution = isForceResolution(properties)
203
204         val propertiesMutableMap = properties.toMutableMap()
205         log.info("Resolving resource with resource assignment artifact($artifactMapping)")
206
207         val resourceAssignmentContent =
208             bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
209
210         val resourceAssignments: MutableList<ResourceAssignment> =
211             JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
212                 as? MutableList<ResourceAssignment>
213                 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
214
215         if (isToStore(properties)) {
216             val alwaysPerformNewResolution = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int <= 0
217             val existingResourceResolution = if (alwaysPerformNewResolution) {
218                 val occurrence = findNextOccurrence(bluePrintRuntimeService, properties, artifactPrefix)
219                 log.info("Always perform new resolutions  - next occurrence: $occurrence")
220                 propertiesMutableMap[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
221                 // Since we are performing new resolution, simply pass empty list.
222                 emptyList()
223             } else {
224                 isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
225             }
226             if (existingResourceResolution.isNotEmpty()) {
227                 if (forceResolution) {
228                     resourceResolutionDBService.deleteResourceResolutionList(existingResourceResolution)
229                     log.info("Force resolution is enabled - will resolve all resources.")
230                 } else {
231                     updateResourceAssignmentWithExisting(
232                         bluePrintRuntimeService as ResourceAssignmentRuntimeService,
233                         existingResourceResolution, resourceAssignments
234                     )
235                     log.info("Force resolution is disabled - will resolve all resources not already resolved.")
236                 }
237             }
238         }
239
240         // Get the Resource Dictionary Name
241         val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
242             .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
243
244         // Resolve resources
245         resolveResourceAssignments(
246             bluePrintRuntimeService,
247             resourceDefinitions,
248             resourceAssignments,
249             artifactPrefix,
250             propertiesMutableMap
251         )
252
253         val resolutionSummary = properties.getOrDefault(
254             ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY,
255             false
256         ) as Boolean
257
258         val resolvedParamJsonContent =
259             ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
260         val artifactTemplateDefinition =
261             bluePrintRuntimeService.bluePrintContext().checkNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
262
263         val resolvedContent = when {
264             artifactTemplateDefinition != null -> {
265                 blueprintTemplateService.generateContent(
266                     bluePrintRuntimeService, nodeTemplateName,
267                     artifactTemplate, resolvedParamJsonContent, false,
268                     mutableMapOf(
269                         ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
270                             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE]
271                                 .asJsonPrimitive()
272                     )
273                 )
274             }
275             resolutionSummary -> {
276                 ResourceAssignmentUtils.generateResolutionSummaryData(resourceAssignments, resourceDefinitions)
277             }
278             else -> {
279                 resolvedParamJsonContent
280             }
281         }
282
283         if (isToStore(properties)) {
284             templateResolutionDBService.write(propertiesMutableMap, resolvedContent, bluePrintRuntimeService, artifactPrefix)
285             log.info("Template resolution saved into database successfully : ($properties)")
286         }
287
288         return Pair(resolvedContent, resourceAssignments)
289     }
290
291     override suspend fun resolveResourceDefinition(
292         blueprintRuntimeService: BluePrintRuntimeService<*>,
293         resourceDefinitions: MutableMap<String, ResourceDefinition>,
294         resolveDefinition: String,
295         sources: List<String>
296     ): MutableMap<String, JsonNode> {
297
298         // Populate Dummy Resource Assignments
299         val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
300
301         resolveResourceAssignments(
302             blueprintRuntimeService, resourceDefinitions, resourceAssignments,
303             UUID.randomUUID().toString(), hashMapOf()
304         )
305
306         // Get the data from Resource Assignments
307         return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
308     }
309
310     /**
311      * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
312      * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
313      * request.
314      */
315     override suspend fun resolveResourceAssignments(
316         blueprintRuntimeService: BluePrintRuntimeService<*>,
317         resourceDefinitions: MutableMap<String, ResourceDefinition>,
318         resourceAssignments: MutableList<ResourceAssignment>,
319         artifactPrefix: String,
320         properties: Map<String, Any>
321     ) {
322
323         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
324
325         // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
326         val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
327             ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
328         } else {
329             blueprintRuntimeService
330         }
331
332         exposeOccurrencePropertyInResourceAssignments(resourceAssignmentRuntimeService, properties)
333
334         coroutineScope {
335             bulkSequenced.forEach { batchResourceAssignments ->
336                 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
337                 val deferred = batchResourceAssignments
338                     .filter { it.name != "*" && it.name != "start" }
339                     .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
340                     .map { resourceAssignment ->
341                         async {
342                             val dictionaryName = resourceAssignment.dictionaryName
343                             val dictionarySource = resourceAssignment.dictionarySource
344
345                             val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
346
347                             val resourceAssignmentProcessor =
348                                 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
349                                     ?: throw BluePrintProcessorException(
350                                         "failed to get resource processor ($processorName) " +
351                                             "for resource assignment(${resourceAssignment.name})"
352                                     )
353                             try {
354                                 // Set BluePrint Runtime Service
355                                 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
356                                 // Set Resource Dictionaries
357                                 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
358
359                                 resourceAssignmentProcessor.resourceAssignments = resourceAssignments
360
361                                 // Invoke Apply Method
362                                 resourceAssignmentProcessor.applyNB(resourceAssignment)
363
364                                 if (isToStore(properties)) {
365                                     resourceResolutionDBService.write(
366                                         properties,
367                                         blueprintRuntimeService,
368                                         artifactPrefix,
369                                         resourceAssignment
370                                     )
371                                     log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})")
372                                 }
373
374                                 // Set errors from RA
375                                 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
376                             } catch (e: RuntimeException) {
377                                 log.error("Fail in processing ${resourceAssignment.name}", e)
378                                 throw BluePrintProcessorException(e)
379                             }
380                         }
381                     }
382                 log.debug("Resolving (${deferred.size})resources parallel.")
383                 deferred.awaitAll()
384             }
385         }
386     }
387
388     /**
389      * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
390      *  derive the default input processor.
391      */
392     private fun processorName(
393         dictionaryName: String,
394         dictionarySource: String,
395         resourceDefinitions: MutableMap<String, ResourceDefinition>
396     ): String {
397         val processorName: String = when (dictionarySource) {
398             "input" -> {
399                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
400             }
401             "default" -> {
402                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
403             }
404             else -> {
405                 val resourceDefinition = resourceDefinitions[dictionaryName]
406                     ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
407
408                 val resourceSource = resourceDefinition.sources[dictionarySource]
409                     ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
410
411                 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
412             }
413         }
414         checkNotEmpty(processorName) {
415             "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
416         }
417
418         return processorName
419     }
420
421     // Check whether to store or not the resolution of resource and template
422     private fun isToStore(properties: Map<String, Any>): Boolean {
423         return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT) &&
424             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
425     }
426
427     private fun isForceResolution(properties: Map<String, Any>): Boolean =
428         properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_FORCE_RESOLUTION) &&
429             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_FORCE_RESOLUTION] as Boolean
430
431     // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
432     private suspend fun isNewResolution(
433         bluePrintRuntimeService: BluePrintRuntimeService<*>,
434         properties: Map<String, Any>,
435         artifactPrefix: String
436     ): List<ResourceResolution> {
437         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
438         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
439         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
440         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
441
442         if (resolutionKey.isNotEmpty()) {
443             val existingResourceAssignments =
444                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
445                     bluePrintRuntimeService,
446                     resolutionKey,
447                     occurrence,
448                     artifactPrefix
449                 )
450             if (existingResourceAssignments.isNotEmpty()) {
451                 log.info(
452                     "Resolution with resolutionKey=($resolutionKey) already exist",
453                     resolutionKey
454                 )
455             }
456             return existingResourceAssignments
457         } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
458             val existingResourceAssignments =
459                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
460                     bluePrintRuntimeService,
461                     resourceId,
462                     resourceType,
463
464                     occurrence,
465                     artifactPrefix
466                 )
467             if (existingResourceAssignments.isNotEmpty()) {
468                 log.info(
469                     "Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist"
470                 )
471             }
472             return existingResourceAssignments
473         }
474         return emptyList()
475     }
476
477     // Update the resource assignment list with the status of the resource that have already been resolved
478     private fun updateResourceAssignmentWithExisting(
479         raRuntimeService: ResourceAssignmentRuntimeService,
480         resourceResolutionList: List<ResourceResolution>,
481         resourceAssignmentList: MutableList<ResourceAssignment>
482     ) {
483         resourceResolutionList.forEach { resourceResolution ->
484             if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
485                 resourceAssignmentList.forEach {
486                     if (compareOne(resourceResolution, it)) {
487                         log.info(
488                             "Resource ({}) already resolved: value=({})", it.name,
489                             if (hasLogProtect(it.property)) LOG_REDACTED else resourceResolution.value
490                         )
491
492                         // Make sure to recreate value as per the defined type.
493                         val value = resourceResolution.value!!.asJsonType(it.property!!.type)
494                         it.property!!.value = value
495                         it.status = resourceResolution.status
496                         ResourceAssignmentUtils.setResourceDataValue(it, raRuntimeService, value)
497                     }
498                 }
499             }
500         }
501     }
502
503     // Comparison between what we have in the database vs what we have to assign.
504     private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
505         return (
506             resourceResolution.name == resourceAssignment.name &&
507                 resourceResolution.dictionaryName == resourceAssignment.dictionaryName &&
508                 resourceResolution.dictionarySource == resourceAssignment.dictionarySource &&
509                 resourceResolution.dictionaryVersion == resourceAssignment.version
510             )
511     }
512
513     private fun exposeOccurrencePropertyInResourceAssignments(
514         raRuntimeService: ResourceAssignmentRuntimeService,
515         properties: Map<String, Any>
516     ) {
517         raRuntimeService.putResolutionStore(
518             ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE,
519             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()
520         )
521     }
522
523     /**
524      * This method returns 'occurrence' required to persist new resource resolution.
525      *
526      * @param bluePrintRuntimeService
527      * @param properties
528      * @param artifactPrefix
529      */
530     private suspend fun findNextOccurrence(
531         bluePrintRuntimeService: BluePrintRuntimeService<*>,
532         properties: Map<String, Any>,
533         artifactPrefix: String
534     ): Int {
535         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
536         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
537         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
538         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
539         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
540         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
541
542         // This should not happen since the request has already been validated but worth to check it here as well.
543         if (resourceType.isEmpty() && resourceId.isEmpty() && resolutionKey.isEmpty()) {
544             throw BluePrintProcessorException(
545                 "Can't proceed to get next occurrence: " +
546                     "Either provide a resolution-key OR combination of resource-id and resource-type"
547             )
548         }
549
550         if (resolutionKey.isNotEmpty()) {
551             return resourceResolutionDBService.findNextOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
552                 resolutionKey,
553                 blueprintName,
554                 blueprintVersion,
555                 artifactPrefix
556             )
557         } else {
558             return resourceResolutionDBService.findNextOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
559                 blueprintName,
560                 blueprintVersion,
561                 resourceId,
562                 resourceType
563             )
564         }
565     }
566 }