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 kotlinx.coroutines.async
21 import kotlinx.coroutines.awaitAll
22 import kotlinx.coroutines.coroutineScope
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionResultService
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
34 import org.slf4j.LoggerFactory
35 import org.springframework.context.ApplicationContext
36 import org.springframework.stereotype.Service
38 interface ResourceResolutionService {
40 fun registeredResourceSources(): List<String>
42 suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String,
43 resolutionKey: String): String
45 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
46 artifactNames: List<String>, properties: Map<String, Any>): MutableMap<String, String>
48 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
49 artifactPrefix: String, properties: Map<String, Any>): String
51 suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
52 artifactMapping: String, artifactTemplate: String?): String
54 suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
55 resourceDefinitions: MutableMap<String, ResourceDefinition>,
56 resourceAssignments: MutableList<ResourceAssignment>,
57 identifierName: String)
60 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
61 open class ResourceResolutionServiceImpl(private var applicationContext: ApplicationContext,
62 private var resolutionResultService: ResourceResolutionResultService) :
63 ResourceResolutionService {
65 private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
67 override fun registeredResourceSources(): List<String> {
68 return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
69 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
70 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
73 override suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>,
74 artifactTemplate: String,
75 resolutionKey: String): String {
76 return resolutionResultService.read(bluePrintRuntimeService, artifactTemplate, resolutionKey)
79 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
80 artifactNames: List<String>, properties: Map<String, Any>): MutableMap<String, String> {
82 val resolvedParams: MutableMap<String, String> = hashMapOf()
83 artifactNames.forEach { artifactName ->
84 val resolvedContent = resolveResources(bluePrintRuntimeService, nodeTemplateName,
85 artifactName, properties)
86 resolvedParams[artifactName] = resolvedContent
91 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
92 artifactPrefix: String, properties: Map<String, Any>): String {
94 // Velocity Artifact Definition Name
95 val artifactTemplate = "$artifactPrefix-template"
96 // Resource Assignment Artifact Definition Name
97 val artifactMapping = "$artifactPrefix-mapping"
99 val result = resolveResources(bluePrintRuntimeService, nodeTemplateName,
100 artifactMapping, artifactTemplate)
102 if (properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
103 && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean) {
104 resolutionResultService.write(properties, result, bluePrintRuntimeService, artifactPrefix)
105 log.info("resolution saved into database successfully : ($properties)")
112 override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
113 artifactMapping: String, artifactTemplate: String?): String {
115 val resolvedContent: String
116 log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
118 val identifierName = artifactTemplate ?: "no-template"
120 val resourceAssignmentContent =
121 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
123 val resourceAssignments: MutableList<ResourceAssignment> =
124 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
125 as? MutableList<ResourceAssignment>
126 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
128 // Get the Resource Dictionary Name
129 val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
130 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
133 resolveResourceAssignments(bluePrintRuntimeService, resourceDefinitions, resourceAssignments, identifierName)
135 val resolvedParamJsonContent =
136 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
138 // Check Template is there
139 if (artifactTemplate != null) {
140 val blueprintTemplateService = BluePrintTemplateService(bluePrintRuntimeService, nodeTemplateName, artifactTemplate)
141 val templateContent =
142 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
144 resolvedContent = blueprintTemplateService.generateContent(templateContent, resolvedParamJsonContent)
147 resolvedContent = resolvedParamJsonContent
150 return resolvedContent
154 * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
155 * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
158 override suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
159 resourceDefinitions: MutableMap<String, ResourceDefinition>,
160 resourceAssignments: MutableList<ResourceAssignment>,
161 identifierName: String) {
163 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
164 val resourceAssignmentRuntimeService =
165 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, identifierName)
168 bulkSequenced.forEach { batchResourceAssignments ->
169 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
170 val deferred = batchResourceAssignments.filter { it.name != "*" && it.name != "start" }
171 .map { resourceAssignment ->
173 val dictionaryName = resourceAssignment.dictionaryName
174 val dictionarySource = resourceAssignment.dictionarySource
176 * Get the Processor name
178 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
180 val resourceAssignmentProcessor =
181 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
182 ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
183 "for resource assignment(${resourceAssignment.name})")
185 // Set BluePrint Runtime Service
186 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
187 // Set Resource Dictionaries
188 resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
189 // Invoke Apply Method
190 resourceAssignmentProcessor.applyNB(resourceAssignment)
191 // Set errors from RA
192 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
193 } catch (e: RuntimeException) {
194 throw BluePrintProcessorException(e)
198 log.debug("Resolving (${deferred.size})resources parallel.")
207 * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
208 * derive the default input processor.
210 private fun processorName(dictionaryName: String, dictionarySource: String,
211 resourceDefinitions: MutableMap<String, ResourceDefinition>): String {
212 val processorName: String = when (dictionarySource) {
214 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
217 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
220 val resourceDefinition = resourceDefinitions[dictionaryName]
221 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
223 val resourceSource = resourceDefinition.sources[dictionarySource]
224 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
226 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
229 checkNotEmpty(processorName) {
230 "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"