2  *  Copyright © 2017-2018 AT&T Intellectual Property.
 
   4  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   5  *  you may not use this file except in compliance with the License.
 
   6  *  You may obtain a copy of the License at
 
   8  *      http://www.apache.org/licenses/LICENSE-2.0
 
  10  *  Unless required by applicable law or agreed to in writing, software
 
  11  *  distributed under the License is distributed on an "AS IS" BASIS,
 
  12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  *  See the License for the specific language governing permissions and
 
  14  *  limitations under the License.
 
  17 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.enhancer
 
  19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
 
  20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
 
  21 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
 
  22 import org.onap.ccsdk.cds.controllerblueprints.core.logger
 
  23 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
 
  24 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
 
  25 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
 
  26 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
 
  27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
 
  28 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service.ResourceDefinitionRepoService
 
  29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
 
  30 import org.springframework.context.annotation.Scope
 
  31 import org.springframework.stereotype.Service
 
  34  * ResourceAssignmentEnhancerService.
 
  36  * @author Brinda Santh
 
  38 interface ResourceAssignmentEnhancerService {
 
  40     @Throws(BluePrintException::class)
 
  41     fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
 
  42                          bluePrintRuntimeService: BluePrintRuntimeService<*>,
 
  43                          resourceAssignments: List<ResourceAssignment>)
 
  47  * ResourceAssignmentEnhancerDefaultService.
 
  49  * @author Brinda Santh
 
  52 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
 
  53 open class ResourceAssignmentEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService)
 
  54     : ResourceAssignmentEnhancerService {
 
  55     private val log= logger(ResourceAssignmentEnhancerServiceImpl::class)
 
  58      * Get the defined source instance from the ResourceAssignment,
 
  59      * then get the NodeType of the Sources assigned
 
  61     override fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
 
  62                                   bluePrintRuntimeService: BluePrintRuntimeService<*>,
 
  63                                   resourceAssignments: List<ResourceAssignment>) {
 
  65         val uniqueSourceNodeTypeNames = hashSetOf<String>()
 
  67         // Iterate the Resource Assignment and
 
  68         resourceAssignments.map { resourceAssignment ->
 
  69             val dictionaryName = resourceAssignment.dictionaryName!!
 
  70             val dictionarySource = resourceAssignment.dictionarySource!!
 
  71             log.debug("Enriching assignment name(${resourceAssignment.name}), dictionary name($dictionaryName), source($dictionarySource)")
 
  72             val sourceNodeTypeName = ResourceSourceMappingFactory.getRegisterSourceMapping(dictionarySource)
 
  74             // Add Unique Node Types
 
  75             uniqueSourceNodeTypeNames.add(sourceNodeTypeName)
 
  77             // TODO("Candidate for Optimisation")
 
  78             if (checkResourceDefinitionNeeded(resourceAssignment)) {
 
  80                 bluePrintTypeEnhancerService.enhancePropertyDefinition(bluePrintRuntimeService, resourceAssignment.name,
 
  81                         resourceAssignment.property!!);
 
  83                 // Get the Resource Definition from Repo
 
  84                 val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)
 
  86                 val sourceNodeTemplate = resourceDefinition.sources[dictionarySource]
 
  87                         ?: throw BluePrintException("failed to get assigned dictionarySource($dictionarySource) from resourceDefinition($dictionaryName)")
 
  89                 // Enrich as NodeTemplate
 
  90                 bluePrintTypeEnhancerService.enhanceNodeTemplate(bluePrintRuntimeService, dictionarySource, sourceNodeTemplate)
 
  93         // Enrich the ResourceSource NodeTypes
 
  94         uniqueSourceNodeTypeNames.map { nodeTypeName ->
 
  95             val nodeType = resourceDefinitionRepoService.getNodeType(nodeTypeName)
 
  96             bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
 
 101     private fun checkResourceDefinitionNeeded(resourceAssignment: ResourceAssignment): Boolean {
 
 102         return !((resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_INPUT)
 
 103                 || resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_DEFAULT))
 
 104                 && BluePrintTypes.validPrimitiveOrCollectionPrimitive(resourceAssignment.property!!))
 
 107     private fun getResourceDefinition(name: String): ResourceDefinition {
 
 108         return resourceDefinitionRepoService.getResourceDefinition(name)