d6a00ae10ebfd15bd1cff602a587b8bbb07a2bcf
[ccsdk/cds.git] /
1 /*\r
2  *  Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *\r
4  *  Licensed under the Apache License, Version 2.0 (the "License");\r
5  *  you may not use this file except in compliance with the License.\r
6  *  You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  *  Unless required by applicable law or agreed to in writing, software\r
11  *  distributed under the License is distributed on an "AS IS" BASIS,\r
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  *  See the License for the specific language governing permissions and\r
14  *  limitations under the License.\r
15  */\r
16 \r
17 package org.onap.ccsdk.apps.controllerblueprints.service.enhancer\r
18 \r
19 import com.att.eelf.configuration.EELFLogger\r
20 import com.att.eelf.configuration.EELFManager\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.format\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService\r
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment\r
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition\r
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants\r
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory\r
30 import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDefinitionRepoService\r
31 import org.springframework.beans.factory.config.ConfigurableBeanFactory\r
32 import org.springframework.context.annotation.Scope\r
33 import org.springframework.stereotype.Service\r
34 \r
35 /**\r
36  * ResourceAssignmentEnhancerService.\r
37  *\r
38  * @author Brinda Santh\r
39  */\r
40 interface ResourceAssignmentEnhancerService {\r
41 \r
42     @Throws(BluePrintException::class)\r
43     fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,\r
44                          bluePrintRuntimeService: BluePrintRuntimeService<*>,\r
45                          resourceAssignments: List<ResourceAssignment>)\r
46 }\r
47 \r
48 /**\r
49  * ResourceAssignmentEnhancerDefaultService.\r
50  *\r
51  * @author Brinda Santh\r
52  */\r
53 @Service\r
54 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)\r
55 open class ResourceAssignmentEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService)\r
56     : ResourceAssignmentEnhancerService {\r
57     private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentEnhancerServiceImpl::class.java)\r
58 \r
59     /**\r
60      * Get the defined source instance from the ResourceAssignment,\r
61      * then get the NodeType of the Sources assigned\r
62      */\r
63     override fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,\r
64                                   bluePrintRuntimeService: BluePrintRuntimeService<*>,\r
65                                   resourceAssignments: List<ResourceAssignment>) {\r
66 \r
67         val uniqueSourceNodeTypeNames = hashSetOf<String>()\r
68 \r
69         // Iterate the Resource Assignment and\r
70         resourceAssignments.map { resourceAssignment ->\r
71             val dictionaryName = resourceAssignment.dictionaryName!!\r
72             val dictionarySource = resourceAssignment.dictionarySource!!\r
73             log.debug("Enriching Assignment name({}), dictionary name({}), source({})", resourceAssignment.name,\r
74                     dictionaryName, dictionarySource)\r
75             val sourceNodeTypeName = ResourceSourceMappingFactory.getRegisterSourceMapping(dictionarySource)\r
76 \r
77             // Add Unique Node Types\r
78             uniqueSourceNodeTypeNames.add(sourceNodeTypeName)\r
79 \r
80             // TODO("Candidate for Optimisation")\r
81             if (checkResourceDefinitionNeeded(resourceAssignment)) {\r
82 \r
83                 bluePrintTypeEnhancerService.enhancePropertyDefinition(bluePrintRuntimeService, resourceAssignment.name,\r
84                         resourceAssignment.property!!);\r
85 \r
86                 // Get the Resource Definition from Repo\r
87                 val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)\r
88 \r
89                 val sourceNodeTemplate = resourceDefinition.sources.get(dictionarySource)\r
90                         ?: throw BluePrintException(format("failed to get assigned dictionarySource({}) from resourceDefinition({})", dictionarySource, dictionaryName))\r
91 \r
92                 // Enrich as NodeTemplate\r
93                 bluePrintTypeEnhancerService.enhanceNodeTemplate(bluePrintRuntimeService, dictionarySource, sourceNodeTemplate)\r
94             }\r
95         }\r
96         // Enrich the ResourceSource NodeTypes\r
97         uniqueSourceNodeTypeNames.map { nodeTypeName ->\r
98             val nodeType = resourceDefinitionRepoService.getNodeType(nodeTypeName)\r
99             bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)\r
100         }\r
101 \r
102     }\r
103 \r
104     /*\r
105         override fun enhanceBluePrint(resourceAssignments: List<ResourceAssignment>): ServiceTemplate {\r
106             val bluePrintEnhancerService = BluePrintEnhancerServiceImpl(resourceDefinitionRepoService)\r
107             bluePrintEnhancerService.serviceTemplate = ServiceTemplate()\r
108             bluePrintEnhancerService.initialCleanUp()\r
109             enhanceBluePrint(bluePrintEnhancerService, resourceAssignments)\r
110             return bluePrintEnhancerService.serviceTemplate\r
111         }\r
112     */\r
113     private fun checkResourceDefinitionNeeded(resourceAssignment: ResourceAssignment): Boolean {\r
114         return !((resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_INPUT)\r
115                 || resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_DEFAULT))\r
116                 && BluePrintTypes.validPrimitiveOrCollectionPrimitive(resourceAssignment.property!!))\r
117     }\r
118 \r
119     private fun getResourceDefinition(name: String): ResourceDefinition {\r
120         return resourceDefinitionRepoService.getResourceDefinition(name)\r
121     }\r
122 }