Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / enhancer / ResourceAssignmentEnhancerService.kt
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.enhancer
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service.ResourceDefinitionRepoService
20 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
21 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTypeEnhancerService
23 import org.onap.ccsdk.cds.controllerblueprints.core.logger
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
25 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
26 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
27 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants
28 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.annotation.Scope
31 import org.springframework.stereotype.Service
32
33 /**
34  * ResourceAssignmentEnhancerService.
35  *
36  * @author Brinda Santh
37  */
38 interface ResourceAssignmentEnhancerService {
39
40     @Throws(BlueprintException::class)
41     fun enhanceBlueprint(
42         bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
43         bluePrintRuntimeService: BlueprintRuntimeService<*>,
44         resourceAssignments: List<ResourceAssignment>
45     )
46 }
47
48 /**
49  * ResourceAssignmentEnhancerDefaultService.
50  *
51  * @author Brinda Santh
52  */
53 @Service
54 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
55 open class ResourceAssignmentEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
56     ResourceAssignmentEnhancerService {
57
58     private val log = logger(ResourceAssignmentEnhancerServiceImpl::class)
59
60     /**
61      * Get the defined source instance from the ResourceAssignment,
62      * then get the NodeType of the Sources assigned
63      */
64     override fun enhanceBlueprint(
65         bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
66         bluePrintRuntimeService: BlueprintRuntimeService<*>,
67         resourceAssignments: List<ResourceAssignment>
68     ) {
69
70         val uniqueSourceNodeTypeNames = hashSetOf<String>()
71
72         // Iterate the Resource Assignment and
73         resourceAssignments.map { resourceAssignment ->
74             val dictionaryName = resourceAssignment.dictionaryName!!
75             val dictionarySource = resourceAssignment.dictionarySource!!
76             log.debug("Enriching assignment name(${resourceAssignment.name}), dictionary name($dictionaryName), source($dictionarySource)")
77             val sourceNodeTypeName = ResourceSourceMappingFactory.getRegisterSourceMapping(dictionarySource)
78
79             // Add Unique Node Types
80             uniqueSourceNodeTypeNames.add(sourceNodeTypeName)
81
82             // TODO("Candidate for Optimisation")
83             if (checkResourceDefinitionNeeded(resourceAssignment)) {
84
85                 bluePrintTypeEnhancerService.enhancePropertyDefinition(
86                     bluePrintRuntimeService, resourceAssignment.name,
87                     resourceAssignment.property!!
88                 )
89
90                 // Get the Resource Definition from Repo
91                 val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)
92
93                 val sourceNodeTemplate = resourceDefinition.sources[dictionarySource]
94                     ?: throw BlueprintException("failed to get assigned dictionarySource($dictionarySource) from resourceDefinition($dictionaryName)")
95
96                 // Enrich as NodeTemplate
97                 bluePrintTypeEnhancerService.enhanceNodeTemplate(bluePrintRuntimeService, dictionarySource, sourceNodeTemplate)
98             }
99         }
100         // Enrich the ResourceSource NodeTypes
101         uniqueSourceNodeTypeNames.map { nodeTypeName ->
102             val nodeType = resourceDefinitionRepoService.getNodeType(nodeTypeName)
103             bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
104         }
105     }
106
107     private fun checkResourceDefinitionNeeded(resourceAssignment: ResourceAssignment): Boolean {
108         return !(
109             (
110                 resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_INPUT) ||
111                     resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_DEFAULT)
112                 ) &&
113                 BlueprintTypes.validPrimitiveOrCollectionPrimitive(resourceAssignment.property!!)
114             )
115     }
116
117     private fun getResourceDefinition(name: String): ResourceDefinition {
118         return resourceDefinitionRepoService.getResourceDefinition(name)
119     }
120 }