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