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.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
48 interface ResourceResolutionService {
50 fun registeredResourceSources(): List<String>
52 suspend fun resolveFromDatabase(
53 bluePrintRuntimeService: BluePrintRuntimeService<*>,
54 artifactTemplate: String,
58 suspend fun resolveResources(
59 bluePrintRuntimeService: BluePrintRuntimeService<*>,
60 nodeTemplateName: String,
61 artifactNames: List<String>,
62 properties: Map<String, Any>
63 ): MutableMap<String, String>
65 suspend fun resolveResources(
66 bluePrintRuntimeService: BluePrintRuntimeService<*>,
67 nodeTemplateName: String,
68 artifactPrefix: String,
69 properties: Map<String, Any>
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,
81 MutableMap<String, JsonNode>
83 suspend fun resolveResourceAssignments(
84 blueprintRuntimeService: BluePrintRuntimeService<*>,
85 resourceDefinitions: MutableMap<String, ResourceDefinition>,
86 resourceAssignments: MutableList<ResourceAssignment>,
87 artifactPrefix: String,
88 properties: Map<String, Any>
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
99 ResourceResolutionService {
101 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
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) }
109 override suspend fun resolveFromDatabase(
110 bluePrintRuntimeService: BluePrintRuntimeService<*>,
111 artifactTemplate: String,
112 resolutionKey: String
114 return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
115 bluePrintRuntimeService,
121 override suspend fun resolveResources(
122 bluePrintRuntimeService: BluePrintRuntimeService<*>,
123 nodeTemplateName: String,
124 artifactNames: List<String>,
125 properties: Map<String, Any>
126 ): MutableMap<String, String> {
128 val resourceAssignmentRuntimeService =
129 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
131 val resolvedParams: MutableMap<String, String> = hashMapOf()
132 artifactNames.forEach { artifactName ->
133 val resolvedContent = resolveResources(
134 resourceAssignmentRuntimeService, nodeTemplateName,
135 artifactName, properties
138 resolvedParams[artifactName] = resolvedContent
140 return resolvedParams
143 override suspend fun resolveResources(
144 bluePrintRuntimeService: BluePrintRuntimeService<*>,
145 nodeTemplateName: String,
146 artifactPrefix: String,
147 properties: Map<String, Any>
150 // Template Artifact Definition Name
151 val artifactTemplate = "$artifactPrefix-template"
152 // Resource Assignment Artifact Definition Name
153 val artifactMapping = "$artifactPrefix-mapping"
155 log.info("Resolving resource with resource assignment artifact($artifactMapping)")
157 val resourceAssignmentContent =
158 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
160 val resourceAssignments: MutableList<ResourceAssignment> =
161 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
162 as? MutableList<ResourceAssignment>
163 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
165 if (isToStore(properties)) {
166 val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
167 if (existingResourceResolution.isNotEmpty()) {
168 updateResourceAssignmentWithExisting(
169 bluePrintRuntimeService as ResourceAssignmentRuntimeService,
170 existingResourceResolution, resourceAssignments
175 // Get the Resource Dictionary Name
176 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
177 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
180 resolveResourceAssignments(
181 bluePrintRuntimeService,
188 val resolutionSummary = properties.getOrDefault(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY, false) as Boolean
189 val resolvedParamJsonContent =
190 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
191 val artifactTemplateDefinition = bluePrintRuntimeService.bluePrintContext().checkNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
193 val resolvedContent = when {
194 artifactTemplateDefinition != null -> {
195 blueprintTemplateService.generateContent(
196 bluePrintRuntimeService, nodeTemplateName,
197 artifactTemplate, resolvedParamJsonContent, false,
199 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
200 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()
204 resolutionSummary -> {
205 ResourceAssignmentUtils.generateResolutionSummaryData(resourceAssignments, resourceDefinitions)
208 resolvedParamJsonContent
212 if (isToStore(properties)) {
213 templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
214 log.info("Template resolution saved into database successfully : ($properties)")
217 return resolvedContent
220 override suspend fun resolveResourceDefinition(
221 blueprintRuntimeService: BluePrintRuntimeService<*>,
222 resourceDefinitions: MutableMap<String, ResourceDefinition>,
223 resolveDefinition: String,
224 sources: List<String>
225 ): MutableMap<String, JsonNode> {
227 // Populate Dummy Resource Assignments
228 val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
230 resolveResourceAssignments(
231 blueprintRuntimeService, resourceDefinitions, resourceAssignments,
232 UUID.randomUUID().toString(), hashMapOf()
235 // Get the data from Resource Assignments
236 return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
240 * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
241 * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
244 override suspend fun resolveResourceAssignments(
245 blueprintRuntimeService: BluePrintRuntimeService<*>,
246 resourceDefinitions: MutableMap<String, ResourceDefinition>,
247 resourceAssignments: MutableList<ResourceAssignment>,
248 artifactPrefix: String,
249 properties: Map<String, Any>
252 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
254 // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
255 val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
256 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
258 blueprintRuntimeService
261 exposeOccurrencePropertyInResourceAssignments(resourceAssignmentRuntimeService, properties)
264 bulkSequenced.forEach { batchResourceAssignments ->
265 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
266 val deferred = batchResourceAssignments
267 .filter { it.name != "*" && it.name != "start" }
268 .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
269 .map { resourceAssignment ->
271 val dictionaryName = resourceAssignment.dictionaryName
272 val dictionarySource = resourceAssignment.dictionarySource
274 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
276 val resourceAssignmentProcessor =
277 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
278 ?: throw BluePrintProcessorException(
279 "failed to get resource processor ($processorName) " +
280 "for resource assignment(${resourceAssignment.name})"
283 // Set BluePrint Runtime Service
284 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
285 // Set Resource Dictionaries
286 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
287 // Invoke Apply Method
288 resourceAssignmentProcessor.applyNB(resourceAssignment)
290 if (isToStore(properties)) {
291 resourceResolutionDBService.write(
293 blueprintRuntimeService,
297 log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})")
300 // Set errors from RA
301 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
302 } catch (e: RuntimeException) {
303 log.error("Fail in processing ${resourceAssignment.name}", e)
304 throw BluePrintProcessorException(e)
308 log.debug("Resolving (${deferred.size})resources parallel.")
315 * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
316 * derive the default input processor.
318 private fun processorName(
319 dictionaryName: String,
320 dictionarySource: String,
321 resourceDefinitions: MutableMap<String, ResourceDefinition>
323 val processorName: String = when (dictionarySource) {
325 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
328 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
331 val resourceDefinition = resourceDefinitions[dictionaryName]
332 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
334 val resourceSource = resourceDefinition.sources[dictionarySource]
335 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
337 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
340 checkNotEmpty(processorName) {
341 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
347 // Check whether to store or not the resolution of resource and template
348 private fun isToStore(properties: Map<String, Any>): Boolean {
349 return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT) &&
350 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
353 // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
354 private suspend fun isNewResolution(
355 bluePrintRuntimeService: BluePrintRuntimeService<*>,
356 properties: Map<String, Any>,
357 artifactPrefix: String
358 ): List<ResourceResolution> {
359 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
360 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
361 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
362 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
364 if (resolutionKey.isNotEmpty()) {
365 val existingResourceAssignments =
366 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
367 bluePrintRuntimeService,
372 if (existingResourceAssignments.isNotEmpty()) {
374 "Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
378 return existingResourceAssignments
379 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
380 val existingResourceAssignments =
381 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
382 bluePrintRuntimeService,
389 if (existingResourceAssignments.isNotEmpty()) {
391 "Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
392 "all resources not already resolved."
395 return existingResourceAssignments
400 // Update the resource assignment list with the status of the resource that have already been resolved
401 private fun updateResourceAssignmentWithExisting(
402 raRuntimeService: ResourceAssignmentRuntimeService,
403 resourceResolutionList: List<ResourceResolution>,
404 resourceAssignmentList: MutableList<ResourceAssignment>
406 resourceResolutionList.forEach { resourceResolution ->
407 if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
408 resourceAssignmentList.forEach {
409 if (compareOne(resourceResolution, it)) {
411 "Resource ({}) already resolved: value=({})", it.name,
412 if (hasLogProtect(it.property)) LOG_REDACTED else resourceResolution.value
415 // Make sure to recreate value as per the defined type.
416 val value = resourceResolution.value!!.asJsonType(it.property!!.type)
417 it.property!!.value = value
418 it.status = resourceResolution.status
419 ResourceAssignmentUtils.setResourceDataValue(it, raRuntimeService, value)
426 // Comparision between what we have in the database vs what we have to assign.
427 private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
428 return (resourceResolution.name == resourceAssignment.name &&
429 resourceResolution.dictionaryName == resourceAssignment.dictionaryName &&
430 resourceResolution.dictionarySource == resourceAssignment.dictionarySource &&
431 resourceResolution.dictionaryVersion == resourceAssignment.version)
434 private fun exposeOccurrencePropertyInResourceAssignments(
435 raRuntimeService: ResourceAssignmentRuntimeService,
436 properties: Map<String, Any>
438 raRuntimeService.putResolutionStore(
439 ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE,
440 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()