2  *  Copyright © 2017-2018 AT&T Intellectual Property.
\r 
   3  *  Modifications Copyright © 2018 IBM.
\r 
   5  *  Licensed under the Apache License, Version 2.0 (the "License");
\r 
   6  *  you may not use this file except in compliance with the License.
\r 
   7  *  You may obtain a copy of the License at
\r 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  11  *  Unless required by applicable law or agreed to in writing, software
\r 
  12  *  distributed under the License is distributed on an "AS IS" BASIS,
\r 
  13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  14  *  See the License for the specific language governing permissions and
\r 
  15  *  limitations under the License.
\r 
  18 package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution
\r 
  20 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
\r 
  21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
\r 
  22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
\r 
  23 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
\r 
  24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
\r 
  25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
\r 
  26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
\r 
  27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
\r 
  28 import org.slf4j.LoggerFactory
\r 
  29 import org.springframework.beans.factory.annotation.Autowired
\r 
  30 import org.springframework.context.ApplicationContext
\r 
  31 import org.springframework.stereotype.Service
\r 
  35  * ResourceResolutionService
\r 
  36  * @author Brinda Santh
\r 
  41 class ResourceResolutionService {
\r 
  43     private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
\r 
  46     private lateinit var applicationContext: ApplicationContext
\r 
  48     fun registeredResourceSources(): List<String> {
\r 
  49         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
\r 
  50                 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }
\r 
  51                 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }
\r 
  55     fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
\r 
  56                          artifactNames: List<String>): MutableMap<String, String> {
\r 
  58         val resolvedParams: MutableMap<String, String> = hashMapOf()
\r 
  59         artifactNames.forEach { artifactName ->
\r 
  60             val resolvedContent = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactName)
\r 
  61             resolvedParams[artifactName] = resolvedContent
\r 
  63         return resolvedParams
\r 
  67     fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String, artifactName: String): String {
\r 
  69         var resolvedContent = ""
\r 
  70         // Velocity Artifact Definition Name
\r 
  71         val templateArtifactName = "$artifactName-template"
\r 
  72         // Resource Assignment Artifact Definition Name
\r 
  73         val mappingArtifactName = "$artifactName-mapping"
\r 
  75         log.info("Resolving resource for template artifact($templateArtifactName) with resource assignment artifact($mappingArtifactName)")
\r 
  77         val resourceAssignmentContent = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, mappingArtifactName)
\r 
  79         val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
\r 
  80                 as? MutableList<ResourceAssignment>
\r 
  81                 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
\r 
  83         // Get the Resource Dictionary Name
\r 
  84         val dictionaryFile = bluePrintRuntimeService.bluePrintContext().rootPath.plus(File.separator)
\r 
  85                 .plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR).plus(File.separator)
\r 
  86                 .plus(ResourceResolutionConstants.FILE_NAME_RESOURCE_DEFINITION_TYPES)
\r 
  88         val resourceDictionaries: MutableMap<String, ResourceDefinition> = JacksonUtils.getMapFromFile(dictionaryFile, ResourceDefinition::class.java)
\r 
  89                 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
\r 
  91         executeProcessors(bluePrintRuntimeService, resourceDictionaries, resourceAssignments)
\r 
  93         // Check Template is there
\r 
  94         val templateContent = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, mappingArtifactName)
\r 
  96         // TODO ("Generate Param JSON from Resource Assignment")
\r 
  97         val resolvedParamJsonContent = "{}"
\r 
  99         if (templateContent.isNotEmpty()) {
\r 
 100             // TODO ( "Mash Data and Content")
\r 
 101             resolvedContent = "Mashed Content"
\r 
 104             resolvedContent = resolvedParamJsonContent
\r 
 106         return resolvedContent
\r 
 110     fun executeProcessors(bluePrintRuntimeService: BluePrintRuntimeService<*>,
\r 
 111                           resourceDictionaries: MutableMap<String, ResourceDefinition>,
\r 
 112                           resourceAssignments: MutableList<ResourceAssignment>) {
\r 
 114         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
\r 
 116         bulkSequenced.map { batchResourceAssignments ->
\r 
 117             batchResourceAssignments.filter { it.name != "*" && it.name != "start" }
\r 
 118                     .map { resourceAssignment ->
\r 
 119                         val dictionarySource = resourceAssignment.dictionarySource
\r 
 120                         val processorInstanceName = ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR.plus(dictionarySource)
\r 
 122                         val resourceAssignmentProcessor = applicationContext.getBean(processorInstanceName) as? ResourceAssignmentProcessor
\r 
 123                                 ?: throw BluePrintProcessorException("failed to get resource processor for instance name($processorInstanceName) " +
\r 
 124                                         "for resource assignment(${resourceAssignment.name})")
\r 
 126                             // Set BluePrint Runtime Service
\r 
 127                             resourceAssignmentProcessor.bluePrintRuntimeService = bluePrintRuntimeService
\r 
 128                             // Set Resource Dictionaries
\r 
 129                             resourceAssignmentProcessor.resourceDictionaries = resourceDictionaries
\r 
 130                             // Invoke Apply Method
\r 
 131                             resourceAssignmentProcessor.apply(resourceAssignment)
\r 
 132                         } catch (e: RuntimeException) {
\r 
 133                             resourceAssignmentProcessor.recover(e, resourceAssignment)
\r 
 134                             throw BluePrintProcessorException(e)
\r