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.*
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
33 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
35 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
36 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
37 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
38 import org.slf4j.LoggerFactory
39 import org.springframework.context.ApplicationContext
40 import org.springframework.stereotype.Service
43 interface ResourceResolutionService {
45 fun registeredResourceSources(): List<String>
47 suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String,
48 resolutionKey: String): String
50 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
51 artifactNames: List<String>, properties: Map<String, Any>): MutableMap<String, String>
53 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
54 artifactPrefix: String, properties: Map<String, Any>): String
56 /** Resolve resources for all the sources defined in a particular resource Definition[resolveDefinition]
57 * with other [resourceDefinitions] dependencies for the sources [sources]
58 * Used to get the same resource values from multiple sources. **/
59 suspend fun resolveResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>,
60 resourceDefinitions: MutableMap<String, ResourceDefinition>,
61 resolveDefinition: String, sources: List<String>)
62 : MutableMap<String, JsonNode>
64 suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
65 resourceDefinitions: MutableMap<String, ResourceDefinition>,
66 resourceAssignments: MutableList<ResourceAssignment>,
67 artifactPrefix: String,
68 properties: Map<String, Any>)
71 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
72 open class ResourceResolutionServiceImpl(private var applicationContext: ApplicationContext,
73 private var templateResolutionDBService: TemplateResolutionService,
74 private var blueprintTemplateService: BluePrintTemplateService,
75 private var resourceResolutionDBService: ResourceResolutionDBService) :
76 ResourceResolutionService {
78 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
80 override fun registeredResourceSources(): List<String> {
81 return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
82 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
83 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
86 override suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>,
87 artifactTemplate: String,
88 resolutionKey: String): String {
89 return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
90 bluePrintRuntimeService,
95 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
96 artifactNames: List<String>,
97 properties: Map<String, Any>): MutableMap<String, String> {
99 val resourceAssignmentRuntimeService =
100 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
102 val resolvedParams: MutableMap<String, String> = hashMapOf()
103 artifactNames.forEach { artifactName ->
104 val resolvedContent = resolveResources(resourceAssignmentRuntimeService, nodeTemplateName,
105 artifactName, properties)
107 resolvedParams[artifactName] = resolvedContent
109 return resolvedParams
113 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
114 artifactPrefix: String, properties: Map<String, Any>): String {
116 // Velocity Artifact Definition Name
117 val artifactTemplate = "$artifactPrefix-template"
118 // Resource Assignment Artifact Definition Name
119 val artifactMapping = "$artifactPrefix-mapping"
121 val resolvedContent: String
122 log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
124 val resourceAssignmentContent =
125 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
127 val resourceAssignments: MutableList<ResourceAssignment> =
128 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
129 as? MutableList<ResourceAssignment>
130 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
132 if (isToStore(properties)) {
133 val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
134 if (existingResourceResolution.isNotEmpty()) {
135 updateResourceAssignmentWithExisting(bluePrintRuntimeService as ResourceAssignmentRuntimeService,
136 existingResourceResolution, resourceAssignments)
140 // Get the Resource Dictionary Name
141 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
142 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
145 resolveResourceAssignments(bluePrintRuntimeService,
151 val resolvedParamJsonContent =
152 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
154 resolvedContent = blueprintTemplateService.generateContent(bluePrintRuntimeService, nodeTemplateName,
155 artifactTemplate, resolvedParamJsonContent, false,
156 mutableMapOf(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE to
157 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE].asJsonPrimitive()))
159 if (isToStore(properties)) {
160 templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
161 log.info("Template resolution saved into database successfully : ($properties)")
164 return resolvedContent
167 override suspend fun resolveResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>,
168 resourceDefinitions: MutableMap<String, ResourceDefinition>,
169 resolveDefinition: String, sources: List<String>)
170 : MutableMap<String, JsonNode> {
172 // Populate Dummy Resource Assignments
173 val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources)
175 resolveResourceAssignments(blueprintRuntimeService, resourceDefinitions, resourceAssignments,
176 UUID.randomUUID().toString(), hashMapOf())
178 // Get the data from Resource Assignments
179 return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments)
183 * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
184 * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
187 override suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
188 resourceDefinitions: MutableMap<String, ResourceDefinition>,
189 resourceAssignments: MutableList<ResourceAssignment>,
190 artifactPrefix: String,
191 properties: Map<String, Any>) {
193 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
195 // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
196 val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) {
197 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
199 blueprintRuntimeService
204 bulkSequenced.forEach { batchResourceAssignments ->
205 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
206 val deferred = batchResourceAssignments
207 .filter { it.name != "*" && it.name != "start" }
208 .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
209 .map { resourceAssignment ->
211 val dictionaryName = resourceAssignment.dictionaryName
212 val dictionarySource = resourceAssignment.dictionarySource
214 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
216 val resourceAssignmentProcessor =
217 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
218 ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
219 "for resource assignment(${resourceAssignment.name})")
221 // Set BluePrint Runtime Service
222 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
223 // Set Resource Dictionaries
224 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
225 // Invoke Apply Method
226 resourceAssignmentProcessor.applyNB(resourceAssignment)
228 if (isToStore(properties)) {
229 resourceResolutionDBService.write(properties,
230 blueprintRuntimeService,
233 log.info("Resource resolution saved into database successfully : ($resourceAssignment)")
236 // Set errors from RA
237 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
238 } catch (e: RuntimeException) {
239 log.error("Fail in processing ${resourceAssignment.name}", e)
240 throw BluePrintProcessorException(e)
244 log.debug("Resolving (${deferred.size})resources parallel.")
252 * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
253 * derive the default input processor.
255 private fun processorName(dictionaryName: String, dictionarySource: String,
256 resourceDefinitions: MutableMap<String, ResourceDefinition>): String {
257 val processorName: String = when (dictionarySource) {
259 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
262 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
265 val resourceDefinition = resourceDefinitions[dictionaryName]
266 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
268 val resourceSource = resourceDefinition.sources[dictionarySource]
269 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
271 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
274 checkNotEmpty(processorName) {
275 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
282 // Check whether to store or not the resolution of resource and template
283 private fun isToStore(properties: Map<String, Any>): Boolean {
284 return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
285 && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
288 // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
289 private suspend fun isNewResolution(bluePrintRuntimeService: BluePrintRuntimeService<*>,
290 properties: Map<String, Any>,
291 artifactPrefix: String): List<ResourceResolution> {
292 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
293 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
294 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
295 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
297 if (resolutionKey.isNotEmpty()) {
298 val existingResourceAssignments =
299 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
300 bluePrintRuntimeService,
304 if (existingResourceAssignments.isNotEmpty()) {
305 log.info("Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
308 return existingResourceAssignments
309 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
310 val existingResourceAssignments =
311 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
312 bluePrintRuntimeService,
318 if (existingResourceAssignments.isNotEmpty()) {
319 log.info("Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
320 "all resources not already resolved.")
322 return existingResourceAssignments
327 // Update the resource assignment list with the status of the resource that have already been resolved
328 private fun updateResourceAssignmentWithExisting(raRuntimeService : ResourceAssignmentRuntimeService,
329 resourceResolutionList: List<ResourceResolution>,
330 resourceAssignmentList: MutableList<ResourceAssignment>) {
331 resourceResolutionList.forEach { resourceResolution ->
332 if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
333 resourceAssignmentList.forEach {
334 if (compareOne(resourceResolution, it)) {
335 log.info("Resource ({}) already resolve: value=({})", it.name, resourceResolution.value)
337 // Make sure to recreate value as per the defined type.
338 val value = resourceResolution.value!!.asJsonType(it.property!!.type)
339 it.property!!.value = value
340 it.status = resourceResolution.status
341 ResourceAssignmentUtils.setResourceDataValue(it, raRuntimeService, value)
348 // Comparision between what we have in the database vs what we have to assign.
349 private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
350 return (resourceResolution.name == resourceAssignment.name
351 && resourceResolution.dictionaryName == resourceAssignment.dictionaryName
352 && resourceResolution.dictionarySource == resourceAssignment.dictionarySource
353 && resourceResolution.dictionaryVersion == resourceAssignment.version)