Revert "Renaming Files having BluePrint to have Blueprint"
[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 resolveResources(
65         bluePrintRuntimeService: BluePrintRuntimeService<*>,
66         nodeTemplateName: String,
67         artifactNames: List<String>,
68         properties: Map<String, Any>,
69         stepName: String
70     ): ResourceResolutionResult
71
72     suspend fun resolveResources(
73         bluePrintRuntimeService: BluePrintRuntimeService<*>,
74         nodeTemplateName: String,
75         artifactPrefix: String,
76         properties: Map<String, Any>
77     ): Pair<String, MutableList<ResourceAssignment>>
78
79     /** Resolve resources for all the sources defined in a particular resource Definition[resolveDefinition]
80      * with other [resourceDefinitions] dependencies for the sources [sources]
81      * Used to get the same resource values from multiple sources. **/
82     suspend fun resolveResourceDefinition(
83         blueprintRuntimeService: BluePrintRuntimeService<*>,
84         resourceDefinitions: MutableMap<String, ResourceDefinition>,
85         resolveDefinition: String,
86         sources: List<String>
87     ):
88         MutableMap<String, JsonNode>
89
90     suspend fun resolveResourceAssignments(
91         blueprintRuntimeService: BluePrintRuntimeService<*>,
92         resourceDefinitions: MutableMap<String, ResourceDefinition>,
93         resourceAssignments: MutableList<ResourceAssignment>,
94         artifactPrefix: String,
95         properties: Map<String, Any>
96     )
97 }
98
99 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
100 open class ResourceResolutionServiceImpl(
101     private var applicationContext: ApplicationContext,
102     private var templateResolutionDBService: TemplateResolutionService,
103     private var blueprintTemplateService: BluePrintTemplateService,
104     private var resourceResolutionDBService: ResourceResolutionDBService
105 ) :
106     ResourceResolutionService {
107
108     private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
109
110     override fun registeredResourceSources(): List<String> {
111         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
112             .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
113             .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
114     }
115
116     override suspend fun resolveFromDatabase(
117         bluePrintRuntimeService: BluePrintRuntimeService<*>,
118         artifactTemplate: String,
119         resolutionKey: String
120     ): String {
121         return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
122             bluePrintRuntimeService,
123             artifactTemplate,
124             resolutionKey
125         )
126     }
127
128     override suspend fun resolveResources(
129         bluePrintRuntimeService: BluePrintRuntimeService<*>,
130         nodeTemplateName: String,
131         artifactNames: List<String>,
132         properties: Map<String, Any>,
133         stepName: String
134     ): ResourceResolutionResult {
135
136         val resourceAssignmentRuntimeService =
137             ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
138
139         val templateMap: MutableMap<String, String> = hashMapOf()
140         val assignmentMap: MutableMap<String, JsonNode> = hashMapOf()
141         artifactNames.forEach { artifactName ->
142             val (resolvedStringContent, resourceAssignmentList) = resolveResources(
143                 resourceAssignmentRuntimeService, nodeTemplateName,
144                 artifactName, properties
145             )
146             val resolvedJsonContent = resourceAssignmentList
147                 .associateBy({ it.name }, { it.property?.value })
148                 .asJsonNode()
149
150             templateMap[artifactName] = resolvedStringContent
151             assignmentMap[artifactName] = resolvedJsonContent
152
153             val failedResolution = resourceAssignmentList.filter { it.status != "success" && it.property?.required == true }.map { it.name }
154             if (failedResolution.isNotEmpty()) {
155                 val errorMessages = mutableListOf("Failed to resolve required values: $failedResolution").apply {
156                     this.addAll(resourceAssignmentRuntimeService.getBluePrintError().allErrors())
157                 }
158                 bluePrintRuntimeService.getBluePrintError().addErrors(stepName, errorMessages)
159             }
160         }
161         return ResourceResolutionResult(templateMap, assignmentMap)
162     }
163
164     override suspend fun resolveResources(
165         bluePrintRuntimeService: BluePrintRuntimeService<*>,
166         nodeTemplateName: String,
167         artifactPrefix: String,
168         properties: Map<String, Any>
169     ): Pair<String, MutableList<ResourceAssignment>> {
170
171         // Template Artifact Definition Name
172         val artifactTemplate = "$artifactPrefix-template"
173         // Resource Assignment Artifact Definition Name
174         val artifactMapping = "$artifactPrefix-mapping"
175
176         log.info("Resolving resource with resource assignment artifact($artifactMapping)")
177
178         val resourceAssignmentContent =
179             bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
180
181         val resourceAssignments: MutableList<ResourceAssignment> =
182             JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
183                 as? MutableList<ResourceAssignment>
184                 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
185
186         if (isToStore(properties)) {
187             val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
188             if (existingResourceResolution.isNotEmpty()) {
189                 updateResourceAssignmentWithExisting(
190                     bluePrintRuntimeService as ResourceAssignmentRuntimeService,
191                     existingResourceResolution, resourceAssignments
192                 )
193             }
194         }
195
196         // Get the Resource Dictionary Name
197         val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
198             .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
199
200         // Resolve resources
201         resolveResourceAssignments(
202             bluePrintRuntimeService,
203             resourceDefinitions,
204             resourceAssignments,
205             artifactPrefix,
206             properties
207         )
208
209         val resolutionSummary = properties.getOrDefault(
210             ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY,
211             false
212         ) as Boolean
213
214         val resolvedParamJsonContent =
215             ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
216         val artifactTemplateDefinition =
217             bluePrintRuntimeService.bluePrintContext().checkNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
218
219         val resolvedContent = when {
220             artifactTemplateDefinition != null -> {
221                 blueprintTemplateService.generateContent(
222                     bluePrintRuntimeService, nodeTemplateName,
223                     artifactTemplate, resolvedParamJsonContent, false,
224                     mutableMapOf(
225                         ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
226                             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE]
227                                 .asJsonPrimitive()
228                     )
229                 )
230             }
231             resolutionSummary -> {
232                 ResourceAssignmentUtils.generateResolutionSummaryData(resourceAssignments, resourceDefinitions)
233             }
234             else -> {
235                 resolvedParamJsonContent
236             }
237         }
238
239         if (isToStore(properties)) {
240             templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
241             log.info("Template resolution saved into database successfully : ($properties)")
242         }
243
244         return Pair(resolvedContent, resourceAssignments)
245     }
246
247     override suspend fun resolveResourceDefinition(
248         blueprintRuntimeService: BluePrintRuntimeService<*>,
249         resourceDefinitions: MutableMap<String, ResourceDefinition>,
250         resolveDefinition: String,
251         sources: List<String>
252     ): MutableMap<String, JsonNode> {
253
254         // Populate Dummy Resource Assignments
255         val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
256
257         resolveResourceAssignments(
258             blueprintRuntimeService, resourceDefinitions, resourceAssignments,
259             UUID.randomUUID().toString(), hashMapOf()
260         )
261
262         // Get the data from Resource Assignments
263         return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
264     }
265
266     /**
267      * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
268      * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
269      * request.
270      */
271     override suspend fun resolveResourceAssignments(
272         blueprintRuntimeService: BluePrintRuntimeService<*>,
273         resourceDefinitions: MutableMap<String, ResourceDefinition>,
274         resourceAssignments: MutableList<ResourceAssignment>,
275         artifactPrefix: String,
276         properties: Map<String, Any>
277     ) {
278
279         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
280
281         // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
282         val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
283             ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
284         } else {
285             blueprintRuntimeService
286         }
287
288         exposeOccurrencePropertyInResourceAssignments(resourceAssignmentRuntimeService, properties)
289
290         coroutineScope {
291             bulkSequenced.forEach { batchResourceAssignments ->
292                 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
293                 val deferred = batchResourceAssignments
294                     .filter { it.name != "*" && it.name != "start" }
295                     .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
296                     .map { resourceAssignment ->
297                         async {
298                             val dictionaryName = resourceAssignment.dictionaryName
299                             val dictionarySource = resourceAssignment.dictionarySource
300
301                             val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
302
303                             val resourceAssignmentProcessor =
304                                 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
305                                     ?: throw BluePrintProcessorException(
306                                         "failed to get resource processor ($processorName) " +
307                                             "for resource assignment(${resourceAssignment.name})"
308                                     )
309                             try {
310                                 // Set BluePrint Runtime Service
311                                 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
312                                 // Set Resource Dictionaries
313                                 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
314
315                                 resourceAssignmentProcessor.resourceAssignments = resourceAssignments
316
317                                 // Invoke Apply Method
318                                 resourceAssignmentProcessor.applyNB(resourceAssignment)
319
320                                 if (isToStore(properties)) {
321                                     resourceResolutionDBService.write(
322                                         properties,
323                                         blueprintRuntimeService,
324                                         artifactPrefix,
325                                         resourceAssignment
326                                     )
327                                     log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})")
328                                 }
329
330                                 // Set errors from RA
331                                 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
332                             } catch (e: RuntimeException) {
333                                 log.error("Fail in processing ${resourceAssignment.name}", e)
334                                 throw BluePrintProcessorException(e)
335                             }
336                         }
337                     }
338                 log.debug("Resolving (${deferred.size})resources parallel.")
339                 deferred.awaitAll()
340             }
341         }
342     }
343
344     /**
345      * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
346      *  derive the default input processor.
347      */
348     private fun processorName(
349         dictionaryName: String,
350         dictionarySource: String,
351         resourceDefinitions: MutableMap<String, ResourceDefinition>
352     ): String {
353         val processorName: String = when (dictionarySource) {
354             "input" -> {
355                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
356             }
357             "default" -> {
358                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
359             }
360             else -> {
361                 val resourceDefinition = resourceDefinitions[dictionaryName]
362                     ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
363
364                 val resourceSource = resourceDefinition.sources[dictionarySource]
365                     ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
366
367                 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
368             }
369         }
370         checkNotEmpty(processorName) {
371             "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
372         }
373
374         return processorName
375     }
376
377     // Check whether to store or not the resolution of resource and template
378     private fun isToStore(properties: Map<String, Any>): Boolean {
379         return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT) &&
380             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
381     }
382
383     // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
384     private suspend fun isNewResolution(
385         bluePrintRuntimeService: BluePrintRuntimeService<*>,
386         properties: Map<String, Any>,
387         artifactPrefix: String
388     ): List<ResourceResolution> {
389         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
390         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
391         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
392         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
393
394         if (resolutionKey.isNotEmpty()) {
395             val existingResourceAssignments =
396                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
397                     bluePrintRuntimeService,
398                     resolutionKey,
399                     occurrence,
400                     artifactPrefix
401                 )
402             if (existingResourceAssignments.isNotEmpty()) {
403                 log.info(
404                     "Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
405                     resolutionKey
406                 )
407             }
408             return existingResourceAssignments
409         } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
410             val existingResourceAssignments =
411                 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
412                     bluePrintRuntimeService,
413                     resourceId,
414                     resourceType,
415
416                     occurrence,
417                     artifactPrefix
418                 )
419             if (existingResourceAssignments.isNotEmpty()) {
420                 log.info(
421                     "Resolution with resourceId=($resourceId) and resourceType=($resourceType) already " +
422                         "exist - will resolve all resources not already resolved."
423                 )
424             }
425             return existingResourceAssignments
426         }
427         return emptyList()
428     }
429
430     // Update the resource assignment list with the status of the resource that have already been resolved
431     private fun updateResourceAssignmentWithExisting(
432         raRuntimeService: ResourceAssignmentRuntimeService,
433         resourceResolutionList: List<ResourceResolution>,
434         resourceAssignmentList: MutableList<ResourceAssignment>
435     ) {
436         resourceResolutionList.forEach { resourceResolution ->
437             if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
438                 resourceAssignmentList.forEach {
439                     if (compareOne(resourceResolution, it)) {
440                         log.info(
441                             "Resource ({}) already resolved: value=({})", it.name,
442                             if (hasLogProtect(it.property)) LOG_REDACTED else resourceResolution.value
443                         )
444
445                         // Make sure to recreate value as per the defined type.
446                         val value = resourceResolution.value!!.asJsonType(it.property!!.type)
447                         it.property!!.value = value
448                         it.status = resourceResolution.status
449                         ResourceAssignmentUtils.setResourceDataValue(it, raRuntimeService, value)
450                     }
451                 }
452             }
453         }
454     }
455
456     // Comparision between what we have in the database vs what we have to assign.
457     private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
458         return (
459             resourceResolution.name == resourceAssignment.name &&
460                 resourceResolution.dictionaryName == resourceAssignment.dictionaryName &&
461                 resourceResolution.dictionarySource == resourceAssignment.dictionarySource &&
462                 resourceResolution.dictionaryVersion == resourceAssignment.version
463             )
464     }
465
466     private fun exposeOccurrencePropertyInResourceAssignments(
467         raRuntimeService: ResourceAssignmentRuntimeService,
468         properties: Map<String, Any>
469     ) {
470         raRuntimeService.putResolutionStore(
471             ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE,
472             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()
473         )
474     }
475 }