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.ResourceResolutionConstants.OUTPUT_ASSIGNMENT_MAP
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
29 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceDefinitionUtils.createResourceAssignments
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
32 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
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 interface ResourceResolutionService {
51 fun registeredResourceSources(): List<String>
53 suspend fun resolveFromDatabase(
54 bluePrintRuntimeService: BluePrintRuntimeService<*>,
55 artifactTemplate: String,
59 suspend fun resolveResources(
60 bluePrintRuntimeService: BluePrintRuntimeService<*>,
61 nodeTemplateName: String,
62 artifactNames: List<String>,
63 properties: Map<String, Any>
64 ): MutableMap<String, String>
66 suspend fun resolveResources(
67 bluePrintRuntimeService: BluePrintRuntimeService<*>,
68 nodeTemplateName: String,
69 artifactPrefix: String,
70 properties: Map<String, Any>
73 /** Resolve resources for all the sources defined in a particular resource Definition[resolveDefinition]
74 * with other [resourceDefinitions] dependencies for the sources [sources]
75 * Used to get the same resource values from multiple sources. **/
76 suspend fun resolveResourceDefinition(
77 blueprintRuntimeService: BluePrintRuntimeService<*>,
78 resourceDefinitions: MutableMap<String, ResourceDefinition>,
79 resolveDefinition: String,
82 MutableMap<String, JsonNode>
84 suspend fun resolveResourceAssignments(
85 blueprintRuntimeService: BluePrintRuntimeService<*>,
86 resourceDefinitions: MutableMap<String, ResourceDefinition>,
87 resourceAssignments: MutableList<ResourceAssignment>,
88 artifactPrefix: String,
89 properties: Map<String, Any>
93 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
94 open class ResourceResolutionServiceImpl(
95 private var applicationContext: ApplicationContext,
96 private var templateResolutionDBService: TemplateResolutionService,
97 private var blueprintTemplateService: BluePrintTemplateService,
98 private var resourceResolutionDBService: ResourceResolutionDBService
100 ResourceResolutionService {
102 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
104 override fun registeredResourceSources(): List<String> {
105 return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
106 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
107 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
110 override suspend fun resolveFromDatabase(
111 bluePrintRuntimeService: BluePrintRuntimeService<*>,
112 artifactTemplate: String,
113 resolutionKey: String
115 return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
116 bluePrintRuntimeService,
122 override suspend fun resolveResources(
123 bluePrintRuntimeService: BluePrintRuntimeService<*>,
124 nodeTemplateName: String,
125 artifactNames: List<String>,
126 properties: Map<String, Any>
127 ): MutableMap<String, String> {
129 val resourceAssignmentRuntimeService =
130 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
132 val resolvedParams: MutableMap<String, String> = hashMapOf()
133 artifactNames.forEach { artifactName ->
134 val resolvedContent = resolveResources(
135 resourceAssignmentRuntimeService, nodeTemplateName,
136 artifactName, properties
139 resolvedParams[artifactName] = resolvedContent
141 return resolvedParams
144 override suspend fun resolveResources(
145 bluePrintRuntimeService: BluePrintRuntimeService<*>,
146 nodeTemplateName: String,
147 artifactPrefix: String,
148 properties: Map<String, Any>
151 // Template Artifact Definition Name
152 val artifactTemplate = "$artifactPrefix-template"
153 // Resource Assignment Artifact Definition Name
154 val artifactMapping = "$artifactPrefix-mapping"
156 log.info("Resolving resource with resource assignment artifact($artifactMapping)")
158 val resourceAssignmentContent =
159 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
161 val resourceAssignments: MutableList<ResourceAssignment> =
162 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
163 as? MutableList<ResourceAssignment>
164 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
166 if (isToStore(properties)) {
167 val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
168 if (existingResourceResolution.isNotEmpty()) {
169 updateResourceAssignmentWithExisting(
170 bluePrintRuntimeService as ResourceAssignmentRuntimeService,
171 existingResourceResolution, resourceAssignments
176 // Get the Resource Dictionary Name
177 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
178 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
181 resolveResourceAssignments(
182 bluePrintRuntimeService,
189 bluePrintRuntimeService.setNodeTemplateAttributeValue(
191 OUTPUT_ASSIGNMENT_MAP,
192 ResourceAssignmentUtils.generateAssignmentMap(artifactPrefix, resourceAssignments)
195 val resolutionSummary = properties.getOrDefault(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY, false) as Boolean
196 val resolvedParamJsonContent =
197 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
198 val artifactTemplateDefinition = bluePrintRuntimeService.bluePrintContext().checkNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
200 val resolvedContent = when {
201 artifactTemplateDefinition != null -> {
202 blueprintTemplateService.generateContent(
203 bluePrintRuntimeService, nodeTemplateName,
204 artifactTemplate, resolvedParamJsonContent, false,
206 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
207 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()
211 resolutionSummary -> {
212 ResourceAssignmentUtils.generateResolutionSummaryData(resourceAssignments, resourceDefinitions)
215 resolvedParamJsonContent
219 if (isToStore(properties)) {
220 templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
221 log.info("Template resolution saved into database successfully : ($properties)")
224 return resolvedContent
227 override suspend fun resolveResourceDefinition(
228 blueprintRuntimeService: BluePrintRuntimeService<*>,
229 resourceDefinitions: MutableMap<String, ResourceDefinition>,
230 resolveDefinition: String,
231 sources: List<String>
232 ): MutableMap<String, JsonNode> {
234 // Populate Dummy Resource Assignments
235 val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
237 resolveResourceAssignments(
238 blueprintRuntimeService, resourceDefinitions, resourceAssignments,
239 UUID.randomUUID().toString(), hashMapOf()
242 // Get the data from Resource Assignments
243 return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
247 * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
248 * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
251 override suspend fun resolveResourceAssignments(
252 blueprintRuntimeService: BluePrintRuntimeService<*>,
253 resourceDefinitions: MutableMap<String, ResourceDefinition>,
254 resourceAssignments: MutableList<ResourceAssignment>,
255 artifactPrefix: String,
256 properties: Map<String, Any>
259 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
261 // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
262 val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
263 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
265 blueprintRuntimeService
268 exposeOccurrencePropertyInResourceAssignments(resourceAssignmentRuntimeService, properties)
271 bulkSequenced.forEach { batchResourceAssignments ->
272 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
273 val deferred = batchResourceAssignments
274 .filter { it.name != "*" && it.name != "start" }
275 .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
276 .map { resourceAssignment ->
278 val dictionaryName = resourceAssignment.dictionaryName
279 val dictionarySource = resourceAssignment.dictionarySource
281 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
283 val resourceAssignmentProcessor =
284 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
285 ?: throw BluePrintProcessorException(
286 "failed to get resource processor ($processorName) " +
287 "for resource assignment(${resourceAssignment.name})"
290 // Set BluePrint Runtime Service
291 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
292 // Set Resource Dictionaries
293 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
294 // Invoke Apply Method
295 resourceAssignmentProcessor.applyNB(resourceAssignment)
297 if (isToStore(properties)) {
298 resourceResolutionDBService.write(
300 blueprintRuntimeService,
304 log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})")
307 // Set errors from RA
308 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
309 } catch (e: RuntimeException) {
310 log.error("Fail in processing ${resourceAssignment.name}", e)
311 throw BluePrintProcessorException(e)
315 log.debug("Resolving (${deferred.size})resources parallel.")
322 * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
323 * derive the default input processor.
325 private fun processorName(
326 dictionaryName: String,
327 dictionarySource: String,
328 resourceDefinitions: MutableMap<String, ResourceDefinition>
330 val processorName: String = when (dictionarySource) {
332 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
335 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
338 val resourceDefinition = resourceDefinitions[dictionaryName]
339 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
341 val resourceSource = resourceDefinition.sources[dictionarySource]
342 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
344 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
347 checkNotEmpty(processorName) {
348 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
354 // Check whether to store or not the resolution of resource and template
355 private fun isToStore(properties: Map<String, Any>): Boolean {
356 return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT) &&
357 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
360 // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
361 private suspend fun isNewResolution(
362 bluePrintRuntimeService: BluePrintRuntimeService<*>,
363 properties: Map<String, Any>,
364 artifactPrefix: String
365 ): List<ResourceResolution> {
366 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
367 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
368 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
369 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
371 if (resolutionKey.isNotEmpty()) {
372 val existingResourceAssignments =
373 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
374 bluePrintRuntimeService,
379 if (existingResourceAssignments.isNotEmpty()) {
381 "Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
385 return existingResourceAssignments
386 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
387 val existingResourceAssignments =
388 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
389 bluePrintRuntimeService,
396 if (existingResourceAssignments.isNotEmpty()) {
398 "Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
399 "all resources not already resolved."
402 return existingResourceAssignments
407 // Update the resource assignment list with the status of the resource that have already been resolved
408 private fun updateResourceAssignmentWithExisting(
409 raRuntimeService: ResourceAssignmentRuntimeService,
410 resourceResolutionList: List<ResourceResolution>,
411 resourceAssignmentList: MutableList<ResourceAssignment>
413 resourceResolutionList.forEach { resourceResolution ->
414 if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
415 resourceAssignmentList.forEach {
416 if (compareOne(resourceResolution, it)) {
418 "Resource ({}) already resolved: value=({})", it.name,
419 if (hasLogProtect(it.property)) LOG_REDACTED else resourceResolution.value
422 // Make sure to recreate value as per the defined type.
423 val value = resourceResolution.value!!.asJsonType(it.property!!.type)
424 it.property!!.value = value
425 it.status = resourceResolution.status
426 ResourceAssignmentUtils.setResourceDataValue(it, raRuntimeService, value)
433 // Comparision between what we have in the database vs what we have to assign.
434 private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
435 return (resourceResolution.name == resourceAssignment.name &&
436 resourceResolution.dictionaryName == resourceAssignment.dictionaryName &&
437 resourceResolution.dictionarySource == resourceAssignment.dictionarySource &&
438 resourceResolution.dictionaryVersion == resourceAssignment.version)
441 private fun exposeOccurrencePropertyInResourceAssignments(
442 raRuntimeService: ResourceAssignmentRuntimeService,
443 properties: Map<String, Any>
445 raRuntimeService.putResolutionStore(
446 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE,
447 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()