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.controllerblueprints.core.*
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
35 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
36 import org.slf4j.LoggerFactory
37 import org.springframework.context.ApplicationContext
38 import org.springframework.stereotype.Service
40 interface ResourceResolutionService {
42 fun registeredResourceSources(): List<String>
44 suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String,
45 resolutionKey: String): String
47 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
48 artifactNames: List<String>, properties: Map<String, Any>): MutableMap<String, JsonNode>
50 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
51 artifactPrefix: String, properties: Map<String, Any>): String
53 suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
54 resourceDefinitions: MutableMap<String, ResourceDefinition>,
55 resourceAssignments: MutableList<ResourceAssignment>,
56 artifactPrefix: String,
57 properties: Map<String, Any>)
60 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
61 open class ResourceResolutionServiceImpl(private var applicationContext: ApplicationContext,
62 private var templateResolutionDBService: TemplateResolutionService,
63 private var blueprintTemplateService: BluePrintTemplateService,
64 private var resourceResolutionDBService: ResourceResolutionDBService) :
65 ResourceResolutionService {
67 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
69 override fun registeredResourceSources(): List<String> {
70 return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
71 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
72 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
75 override suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>,
76 artifactTemplate: String,
77 resolutionKey: String): String {
78 return templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
79 bluePrintRuntimeService,
84 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
85 artifactNames: List<String>,
86 properties: Map<String, Any>): MutableMap<String, JsonNode> {
88 val resourceAssignmentRuntimeService =
89 ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
91 val resolvedParams: MutableMap<String, JsonNode> = hashMapOf()
92 artifactNames.forEach { artifactName ->
93 val resolvedContent = resolveResources(resourceAssignmentRuntimeService, nodeTemplateName,
94 artifactName, properties)
96 resolvedParams[artifactName] = resolvedContent.asJsonType()
102 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
103 artifactPrefix: String, properties: Map<String, Any>): String {
105 // Velocity Artifact Definition Name
106 val artifactTemplate = "$artifactPrefix-template"
107 // Resource Assignment Artifact Definition Name
108 val artifactMapping = "$artifactPrefix-mapping"
110 val resolvedContent: String
111 log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
113 val resourceAssignmentContent =
114 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
116 val resourceAssignments: MutableList<ResourceAssignment> =
117 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
118 as? MutableList<ResourceAssignment>
119 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
121 if (isToStore(properties)) {
122 val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
123 if (existingResourceResolution.isNotEmpty()) {
124 updateResourceAssignmentWithExisting(bluePrintRuntimeService as ResourceAssignmentRuntimeService,
125 existingResourceResolution, resourceAssignments)
129 // Get the Resource Dictionary Name
130 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
131 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
134 resolveResourceAssignments(bluePrintRuntimeService,
140 val resolvedParamJsonContent =
141 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
143 resolvedContent = blueprintTemplateService.generateContent(bluePrintRuntimeService, nodeTemplateName,
144 artifactTemplate, resolvedParamJsonContent)
146 if (isToStore(properties)) {
147 templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
148 log.info("Template resolution saved into database successfully : ($properties)")
151 return resolvedContent
155 * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
156 * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
159 override suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
160 resourceDefinitions: MutableMap<String, ResourceDefinition>,
161 resourceAssignments: MutableList<ResourceAssignment>,
162 artifactPrefix: String,
163 properties: Map<String, Any>) {
165 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
167 // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
168 val resourceAssignmentRuntimeService = if (!(blueprintRuntimeService is ResourceAssignmentRuntimeService)) {
169 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
171 blueprintRuntimeService
176 bulkSequenced.forEach { batchResourceAssignments ->
177 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
178 val deferred = batchResourceAssignments
179 .filter { it.name != "*" && it.name != "start" }
180 .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
181 .map { resourceAssignment ->
183 val dictionaryName = resourceAssignment.dictionaryName
184 val dictionarySource = resourceAssignment.dictionarySource
186 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
188 val resourceAssignmentProcessor =
189 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
190 ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
191 "for resource assignment(${resourceAssignment.name})")
193 // Set BluePrint Runtime Service
194 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
195 // Set Resource Dictionaries
196 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
197 // Invoke Apply Method
198 resourceAssignmentProcessor.applyNB(resourceAssignment)
200 if (isToStore(properties)) {
201 resourceResolutionDBService.write(properties,
202 blueprintRuntimeService,
205 log.info("Resource resolution saved into database successfully : ($resourceAssignment)")
208 // Set errors from RA
209 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
210 } catch (e: RuntimeException) {
211 log.error("Fail in processing ${resourceAssignment.name}", e)
212 throw BluePrintProcessorException(e)
216 log.debug("Resolving (${deferred.size})resources parallel.")
224 * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
225 * derive the default input processor.
227 private fun processorName(dictionaryName: String, dictionarySource: String,
228 resourceDefinitions: MutableMap<String, ResourceDefinition>): String {
229 val processorName: String = when (dictionarySource) {
231 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
234 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
237 val resourceDefinition = resourceDefinitions[dictionaryName]
238 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
240 val resourceSource = resourceDefinition.sources[dictionarySource]
241 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
243 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
246 checkNotEmpty(processorName) {
247 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
254 // Check whether to store or not the resolution of resource and template
255 private fun isToStore(properties: Map<String, Any>): Boolean {
256 return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
257 && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
260 // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
261 private suspend fun isNewResolution(bluePrintRuntimeService: BluePrintRuntimeService<*>,
262 properties: Map<String, Any>,
263 artifactPrefix: String): List<ResourceResolution> {
264 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
265 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
266 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
267 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
269 if (resolutionKey.isNotEmpty()) {
270 val existingResourceAssignments =
271 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
272 bluePrintRuntimeService,
276 if (existingResourceAssignments.isNotEmpty()) {
277 log.info("Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
280 return existingResourceAssignments
281 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
282 val existingResourceAssignments =
283 resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
284 bluePrintRuntimeService,
290 if (existingResourceAssignments.isNotEmpty()) {
291 log.info("Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
292 "all resources not already resolved.")
294 return existingResourceAssignments
299 // Update the resource assignment list with the status of the resource that have already been resolved
300 private fun updateResourceAssignmentWithExisting(raRuntimeService : ResourceAssignmentRuntimeService,
301 resourceResolutionList: List<ResourceResolution>,
302 resourceAssignmentList: MutableList<ResourceAssignment>) {
303 resourceResolutionList.forEach { resourceResolution ->
304 if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
305 resourceAssignmentList.forEach {
306 if (compareOne(resourceResolution, it)) {
307 log.info("Resource ({}) already resolve: value=({})", it.name, resourceResolution.value)
308 val value = resourceResolution.value!!.asJsonPrimitive()
309 it.property!!.value = value
310 it.status = resourceResolution.status
311 ResourceAssignmentUtils.setResourceDataValue(it, raRuntimeService, value)
318 // Comparision between what we have in the database vs what we have to assign.
319 private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
320 return (resourceResolution.name == resourceAssignment.name
321 && resourceResolution.dictionaryName == resourceAssignment.dictionaryName
322 && resourceResolution.dictionarySource == resourceAssignment.dictionarySource
323 && resourceResolution.dictionaryVersion == resourceAssignment.version)