2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018-2019 IBM, Bell Canada
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
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
49 data class ResourceResolutionResult(
50 val templateMap: MutableMap<String, String>,
51 val assignmentMap: MutableMap<String, JsonNode>
54 interface ResourceResolutionService {
56 fun registeredResourceSources(): List<String>
58 suspend fun resolveFromDatabase(
59 bluePrintRuntimeService: BluePrintRuntimeService<*>,
60 artifactTemplate: String,
64 suspend fun resolveResources(
65 bluePrintRuntimeService: BluePrintRuntimeService<*>,
66 nodeTemplateName: String,
67 artifactNames: List<String>,
68 properties: Map<String, Any>,
70 ): ResourceResolutionResult
72 suspend fun resolveResources(
73 bluePrintRuntimeService: BluePrintRuntimeService<*>,
74 nodeTemplateName: String,
75 artifactPrefix: String,
76 properties: Map<String, Any>
77 ): Pair<String, MutableList<ResourceAssignment>>
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,
88 MutableMap<String, JsonNode>
90 suspend fun resolveResourceAssignments(
91 blueprintRuntimeService: BluePrintRuntimeService<*>,
92 resourceDefinitions: MutableMap<String, ResourceDefinition>,
93 resourceAssignments: MutableList<ResourceAssignment>,
94 artifactPrefix: String,
95 properties: Map<String, Any>
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
106 ResourceResolutionService {
108 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
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) }
116 override suspend fun resolveFromDatabase(
117 bluePrintRuntimeService: BluePrintRuntimeService<*>,
118 artifactTemplate: String,
119 resolutionKey: String
121 return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
122 bluePrintRuntimeService,
128 override suspend fun resolveResources(
129 bluePrintRuntimeService: BluePrintRuntimeService<*>,
130 nodeTemplateName: String,
131 artifactNames: List<String>,
132 properties: Map<String, Any>,
134 ): ResourceResolutionResult {
136 val resourceAssignmentRuntimeService =
137 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
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
146 val resolvedJsonContent = resourceAssignmentList
147 .associateBy({ it.name }, { it.property?.value })
150 templateMap[artifactName] = resolvedStringContent
151 assignmentMap[artifactName] = resolvedJsonContent
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())
158 bluePrintRuntimeService.getBluePrintError().addErrors(stepName, errorMessages)
161 return ResourceResolutionResult(templateMap, assignmentMap)
164 override suspend fun resolveResources(
165 bluePrintRuntimeService: BluePrintRuntimeService<*>,
166 nodeTemplateName: String,
167 artifactPrefix: String,
168 properties: Map<String, Any>
169 ): Pair<String, MutableList<ResourceAssignment>> {
171 // Template Artifact Definition Name
172 val artifactTemplate = "$artifactPrefix-template"
173 // Resource Assignment Artifact Definition Name
174 val artifactMapping = "$artifactPrefix-mapping"
176 log.info("Resolving resource with resource assignment artifact($artifactMapping)")
178 val resourceAssignmentContent =
179 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
181 val resourceAssignments: MutableList<ResourceAssignment> =
182 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
183 as? MutableList<ResourceAssignment>
184 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
186 if (isToStore(properties)) {
187 val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
188 if (existingResourceResolution.isNotEmpty()) {
189 updateResourceAssignmentWithExisting(
190 bluePrintRuntimeService as ResourceAssignmentRuntimeService,
191 existingResourceResolution, resourceAssignments
196 // Get the Resource Dictionary Name
197 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
198 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
201 resolveResourceAssignments(
202 bluePrintRuntimeService,
209 val resolutionSummary = properties.getOrDefault(
210 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY,
214 val resolvedParamJsonContent =
215 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
216 val artifactTemplateDefinition =
217 bluePrintRuntimeService.bluePrintContext().checkNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
219 val resolvedContent = when {
220 artifactTemplateDefinition != null -> {
221 blueprintTemplateService.generateContent(
222 bluePrintRuntimeService, nodeTemplateName,
223 artifactTemplate, resolvedParamJsonContent, false,
225 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
226 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE]
231 resolutionSummary -> {
232 ResourceAssignmentUtils.generateResolutionSummaryData(resourceAssignments, resourceDefinitions)
235 resolvedParamJsonContent
239 if (isToStore(properties)) {
240 templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
241 log.info("Template resolution saved into database successfully : ($properties)")
244 return Pair(resolvedContent, resourceAssignments)
247 override suspend fun resolveResourceDefinition(
248 blueprintRuntimeService: BluePrintRuntimeService<*>,
249 resourceDefinitions: MutableMap<String, ResourceDefinition>,
250 resolveDefinition: String,
251 sources: List<String>
252 ): MutableMap<String, JsonNode> {
254 // Populate Dummy Resource Assignments
255 val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
257 resolveResourceAssignments(
258 blueprintRuntimeService, resourceDefinitions, resourceAssignments,
259 UUID.randomUUID().toString(), hashMapOf()
262 // Get the data from Resource Assignments
263 return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
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
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>
279 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
281 // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
282 val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
283 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
285 blueprintRuntimeService
288 exposeOccurrencePropertyInResourceAssignments(resourceAssignmentRuntimeService, properties)
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 ->
298 val dictionaryName = resourceAssignment.dictionaryName
299 val dictionarySource = resourceAssignment.dictionarySource
301 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
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})"
310 // Set BluePrint Runtime Service
311 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
312 // Set Resource Dictionaries
313 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
315 resourceAssignmentProcessor.resourceAssignments = resourceAssignments
317 // Invoke Apply Method
318 resourceAssignmentProcessor.applyNB(resourceAssignment)
320 if (isToStore(properties)) {
321 resourceResolutionDBService.write(
323 blueprintRuntimeService,
327 log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})")
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)
338 log.debug("Resolving (${deferred.size})resources parallel.")
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.
348 private fun processorName(
349 dictionaryName: String,
350 dictionarySource: String,
351 resourceDefinitions: MutableMap<String, ResourceDefinition>
353 val processorName: String = when (dictionarySource) {
355 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
358 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
361 val resourceDefinition = resourceDefinitions[dictionaryName]
362 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
364 val resourceSource = resourceDefinition.sources[dictionarySource]
365 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
367 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
370 checkNotEmpty(processorName) {
371 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
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
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
394 if (resolutionKey.isNotEmpty()) {
395 val existingResourceAssignments =
396 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
397 bluePrintRuntimeService,
402 if (existingResourceAssignments.isNotEmpty()) {
404 "Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
408 return existingResourceAssignments
409 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
410 val existingResourceAssignments =
411 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
412 bluePrintRuntimeService,
419 if (existingResourceAssignments.isNotEmpty()) {
421 "Resolution with resourceId=($resourceId) and resourceType=($resourceType) already " +
422 "exist - will resolve all resources not already resolved."
425 return existingResourceAssignments
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>
436 resourceResolutionList.forEach { resourceResolution ->
437 if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
438 resourceAssignmentList.forEach {
439 if (compareOne(resourceResolution, it)) {
441 "Resource ({}) already resolved: value=({})", it.name,
442 if (hasLogProtect(it.property)) LOG_REDACTED else resourceResolution.value
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)
456 // Comparision between what we have in the database vs what we have to assign.
457 private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
459 resourceResolution.name == resourceAssignment.name &&
460 resourceResolution.dictionaryName == resourceAssignment.dictionaryName &&
461 resourceResolution.dictionarySource == resourceAssignment.dictionarySource &&
462 resourceResolution.dictionaryVersion == resourceAssignment.version
466 private fun exposeOccurrencePropertyInResourceAssignments(
467 raRuntimeService: ResourceAssignmentRuntimeService,
468 properties: Map<String, Any>
470 raRuntimeService.putResolutionStore(
471 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE,
472 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()