55eb42f89458f605b55ce8406c70aaecfb5bc8ae
[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.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
32
33 /**
34  * ResourceAssignmentEnhancerService.
35  *
36  * @author Brinda Santh
37  */
38 interface ResourceAssignmentEnhancerService {
39
40     @Throws(BluePrintException::class)
41     fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
42                          bluePrintRuntimeService: BluePrintRuntimeService<*>,
43                          resourceAssignments: List<ResourceAssignment>)
44 }
45
46 /**
47  * ResourceAssignmentEnhancerDefaultService.
48  *
49  * @author Brinda Santh
50  */
51 @Service
52 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
53 open class ResourceAssignmentEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService)
54     : ResourceAssignmentEnhancerService {
55     private val log= logger(ResourceAssignmentEnhancerServiceImpl::class)
56
57     /**
58      * Get the defined source instance from the ResourceAssignment,
59      * then get the NodeType of the Sources assigned
60      */
61     override fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
62                                   bluePrintRuntimeService: BluePrintRuntimeService<*>,
63                                   resourceAssignments: List<ResourceAssignment>) {
64
65         val uniqueSourceNodeTypeNames = hashSetOf<String>()
66
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)
73
74             // Add Unique Node Types
75             uniqueSourceNodeTypeNames.add(sourceNodeTypeName)
76
77             // TODO("Candidate for Optimisation")
78             if (checkResourceDefinitionNeeded(resourceAssignment)) {
79
80                 bluePrintTypeEnhancerService.enhancePropertyDefinition(bluePrintRuntimeService, resourceAssignment.name,
81                         resourceAssignment.property!!);
82
83                 // Get the Resource Definition from Repo
84                 val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)
85
86                 val sourceNodeTemplate = resourceDefinition.sources[dictionarySource]
87                         ?: throw BluePrintException("failed to get assigned dictionarySource($dictionarySource) from resourceDefinition($dictionaryName)")
88
89                 // Enrich as NodeTemplate
90                 bluePrintTypeEnhancerService.enhanceNodeTemplate(bluePrintRuntimeService, dictionarySource, sourceNodeTemplate)
91             }
92         }
93         // Enrich the ResourceSource NodeTypes
94         uniqueSourceNodeTypeNames.map { nodeTypeName ->
95             val nodeType = resourceDefinitionRepoService.getNodeType(nodeTypeName)
96             bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
97         }
98
99     }
100
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!!))
105     }
106
107     private fun getResourceDefinition(name: String): ResourceDefinition {
108         return resourceDefinitionRepoService.getResourceDefinition(name)
109     }
110 }