2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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.runBlocking
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionResultService
22 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmptyOrThrow
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
33 import org.slf4j.LoggerFactory
34 import org.springframework.context.ApplicationContext
35 import org.springframework.stereotype.Service
38 interface ResourceResolutionService {
40 fun registeredResourceSources(): List<String>
42 fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String,
43 resolutionKey: String): String
45 fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
46 artifactNames: List<String>, properties: Map<String, Any>): MutableMap<String, String>
48 fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
49 artifactPrefix: String, properties: Map<String, Any>): String
51 fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
52 artifactMapping: String, artifactTemplate: String?): String
54 fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
55 resourceDictionaries: 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 fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
74 artifactNames: List<String>,
75 properties: Map<String, Any>): MutableMap<String, String> {
77 val resolvedParams: MutableMap<String, String> = hashMapOf()
78 artifactNames.forEach { artifactName ->
79 val resolvedContent = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactName, properties)
80 resolvedParams[artifactName] = resolvedContent
85 override fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
86 artifactPrefix: String, properties: Map<String, Any>): String {
88 // Velocity Artifact Definition Name
89 val artifactTemplate = "$artifactPrefix-template"
90 // Resource Assignment Artifact Definition Name
91 val artifactMapping = "$artifactPrefix-mapping"
93 val result = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactMapping, artifactTemplate)
95 if (properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
96 && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean) {
97 resolutionResultService.write(properties, result, bluePrintRuntimeService, artifactPrefix)
103 override fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String,
104 resolutionKey: String): String {
105 return resolutionResultService.read(bluePrintRuntimeService, artifactTemplate, resolutionKey)
108 override fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
109 artifactMapping: String, artifactTemplate: String?): String {
111 var resolvedContent = ""
112 log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
114 val identifierName = artifactTemplate ?: "no-template"
116 val resourceAssignmentContent =
117 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
119 val resourceAssignments: MutableList<ResourceAssignment> =
120 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
121 as? MutableList<ResourceAssignment>
122 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
124 // Get the Resource Dictionary Name
125 val dictionaryFile = bluePrintRuntimeService.bluePrintContext().rootPath.plus(File.separator)
126 .plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR).plus(File.separator)
127 .plus(ResourceResolutionConstants.FILE_NAME_RESOURCE_DEFINITION_TYPES)
129 val resourceDictionaries: MutableMap<String, ResourceDefinition> =
130 JacksonUtils.getMapFromFile(dictionaryFile, ResourceDefinition::class.java)
131 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
134 resolveResourceAssignments(bluePrintRuntimeService, resourceDictionaries, resourceAssignments, identifierName)
136 val resolvedParamJsonContent =
137 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
139 // Check Template is there
140 if (artifactTemplate != null) {
141 val templateContent =
142 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
143 resolvedContent = BluePrintTemplateService.generateContent(templateContent, resolvedParamJsonContent)
145 resolvedContent = resolvedParamJsonContent
148 return resolvedContent
152 * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
153 * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
156 override fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
157 resourceDictionaries: MutableMap<String, ResourceDefinition>,
158 resourceAssignments: MutableList<ResourceAssignment>,
159 identifierName: String) {
161 val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
162 val resourceAssignmentRuntimeService =
163 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, identifierName)
165 bulkSequenced.map { batchResourceAssignments ->
166 batchResourceAssignments.filter { it.name != "*" && it.name != "start" }
167 .forEach { resourceAssignment ->
168 val dictionaryName = resourceAssignment.dictionaryName
169 val dictionarySource = resourceAssignment.dictionarySource
171 * Get the Processor name
173 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDictionaries)
175 val resourceAssignmentProcessor =
176 applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
177 ?: throw BluePrintProcessorException("failed to get resource processor for name($processorName) " +
178 "for resource assignment(${resourceAssignment.name})")
180 // Set BluePrint Runtime Service
181 resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
182 // Set Resource Dictionaries
183 resourceAssignmentProcessor.resourceDictionaries = resourceDictionaries
184 // TODO ("Implement suspend function")
186 // Invoke Apply Method
187 resourceAssignmentProcessor.applyNB(resourceAssignment)
189 // Set errors from RA
190 blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
191 } catch (e: RuntimeException) {
192 throw BluePrintProcessorException(e)
200 * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
201 * derive the default input processor.
203 private fun processorName(dictionaryName: String, dictionarySource: String,
204 resourceDictionaries: MutableMap<String, ResourceDefinition>): String {
205 var processorName: String? = null
206 when (dictionarySource) {
208 processorName = "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
211 processorName = "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
214 val resourceDefinition = resourceDictionaries[dictionaryName]
215 ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
217 val resourceSource = resourceDefinition.sources[dictionarySource]
218 ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
220 processorName = ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR
221 .plus(resourceSource.type)
224 checkNotEmptyOrThrow(processorName,
225 "couldn't get processor name for resource dictionary definition($dictionaryName) source" +
226 "($dictionarySource)")