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