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.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
46 interface ResourceResolutionService {
48 fun registeredResourceSources(): List<String>
50 suspend fun resolveFromDatabase(
51 bluePrintRuntimeService: BluePrintRuntimeService<*>,
52 artifactTemplate: String,
56 suspend fun resolveResources(
57 bluePrintRuntimeService: BluePrintRuntimeService<*>,
58 nodeTemplateName: String,
59 artifactNames: List<String>,
60 properties: Map<String, Any>
61 ): MutableMap<String, String>
63 suspend fun resolveResources(
64 bluePrintRuntimeService: BluePrintRuntimeService<*>,
65 nodeTemplateName: String,
66 artifactPrefix: String,
67 properties: Map<String, Any>
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,
79 MutableMap<String, JsonNode>
81 suspend fun resolveResourceAssignments(
82 blueprintRuntimeService: BluePrintRuntimeService<*>,
83 resourceDefinitions: MutableMap<String, ResourceDefinition>,
84 resourceAssignments: MutableList<ResourceAssignment>,
85 artifactPrefix: String,
86 properties: Map<String, Any>
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
97 ResourceResolutionService {
99 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
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) }
107 override suspend fun resolveFromDatabase(
108 bluePrintRuntimeService: BluePrintRuntimeService<*>,
109 artifactTemplate: String,
110 resolutionKey: String
112 return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
113 bluePrintRuntimeService,
119 override suspend fun resolveResources(
120 bluePrintRuntimeService: BluePrintRuntimeService<*>,
121 nodeTemplateName: String,
122 artifactNames: List<String>,
123 properties: Map<String, Any>
124 ): MutableMap<String, String> {
126 val resourceAssignmentRuntimeService =
127 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
129 val resolvedParams: MutableMap<String, String> = hashMapOf()
130 artifactNames.forEach { artifactName ->
131 val resolvedContent = resolveResources(
132 resourceAssignmentRuntimeService, nodeTemplateName,
133 artifactName, properties
136 resolvedParams[artifactName] = resolvedContent
138 return resolvedParams
141 override suspend fun resolveResources(
142 bluePrintRuntimeService: BluePrintRuntimeService<*>,
143 nodeTemplateName: String,
144 artifactPrefix: String,
145 properties: Map<String, Any>
148 // Velocity Artifact Definition Name
149 val artifactTemplate = "$artifactPrefix-template"
150 // Resource Assignment Artifact Definition Name
151 val artifactMapping = "$artifactPrefix-mapping"
153 val resolvedContent: String
154 log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
156 val resourceAssignmentContent =
157 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
159 val resourceAssignments: MutableList<ResourceAssignment> =
160 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
161 as? MutableList<ResourceAssignment>
162 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
164 if (isToStore(properties)) {
165 val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
166 if (existingResourceResolution.isNotEmpty()) {
167 updateResourceAssignmentWithExisting(
168 bluePrintRuntimeService as ResourceAssignmentRuntimeService,
169 existingResourceResolution, resourceAssignments
174 // Get the Resource Dictionary Name
175 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
176 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
179 resolveResourceAssignments(
180 bluePrintRuntimeService,
187 val resolvedParamJsonContent =
188 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
190 resolvedContent = blueprintTemplateService.generateContent(
191 bluePrintRuntimeService, nodeTemplateName,
192 artifactTemplate, resolvedParamJsonContent, false,
194 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
195 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()
199 if (isToStore(properties)) {
200 templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
201 log.info("Template resolution saved into database successfully : ($properties)")
204 return resolvedContent
207 override suspend fun resolveResourceDefinition(
208 blueprintRuntimeService: BluePrintRuntimeService<*>,
209 resourceDefinitions: MutableMap<String, ResourceDefinition>,
210 resolveDefinition: String,
211 sources: List<String>
213 MutableMap<String, JsonNode> {
215 // Populate Dummy Resource Assignments
216 val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
218 resolveResourceAssignments(
219 blueprintRuntimeService, resourceDefinitions, resourceAssignments,
220 UUID.randomUUID().toString(), hashMapOf()
223 // Get the data from Resource Assignments
224 return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
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
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>
240 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
242 // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
243 val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
244 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
246 blueprintRuntimeService
249 exposeOccurrencePropertyInResourceAssignments(resourceAssignmentRuntimeService, properties)
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 ->
259 val dictionaryName = resourceAssignment.dictionaryName
260 val dictionarySource = resourceAssignment.dictionarySource
262 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
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})"
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)
278 if (isToStore(properties)) {
279 resourceResolutionDBService.write(
281 blueprintRuntimeService,
285 log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})")
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)
296 log.debug("Resolving (${deferred.size})resources parallel.")
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.
306 private fun processorName(
307 dictionaryName: String,
308 dictionarySource: String,
309 resourceDefinitions: MutableMap<String, ResourceDefinition>
311 val processorName: String = when (dictionarySource) {
313 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
316 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
319 val resourceDefinition = resourceDefinitions[dictionaryName]
320 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
322 val resourceSource = resourceDefinition.sources[dictionarySource]
323 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
325 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
328 checkNotEmpty(processorName) {
329 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
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
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
352 if (resolutionKey.isNotEmpty()) {
353 val existingResourceAssignments =
354 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
355 bluePrintRuntimeService,
360 if (existingResourceAssignments.isNotEmpty()) {
362 "Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
366 return existingResourceAssignments
367 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
368 val existingResourceAssignments =
369 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
370 bluePrintRuntimeService,
377 if (existingResourceAssignments.isNotEmpty()) {
379 "Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
380 "all resources not already resolved."
383 return existingResourceAssignments
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>
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)
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)
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)
419 private fun exposeOccurrencePropertyInResourceAssignments(
420 raRuntimeService: ResourceAssignmentRuntimeService,
421 properties: Map<String, Any>
423 raRuntimeService.putResolutionStore(
424 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE,
425 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()