d6f346ecddbde86b8e33f1afd3f6d0255c64f273
[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.BluePrintError\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.format\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext\r
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment\r
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition\r
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants\r
30 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory\r
31 import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDefinitionRepoService\r
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory\r
33 import org.springframework.context.annotation.Scope\r
34 import org.springframework.stereotype.Service\r
35 \r
36 /**\r
37  * ResourceAssignmentEnhancerService.\r
38  *\r
39  * @author Brinda Santh\r
40  */\r
41 interface ResourceAssignmentEnhancerService {\r
42 \r
43     @Throws(BluePrintException::class)\r
44     fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,\r
45                          bluePrintContext: BluePrintContext, error: BluePrintError,\r
46                          resourceAssignments: List<ResourceAssignment>)\r
47 }\r
48 \r
49 /**\r
50  * ResourceAssignmentEnhancerDefaultService.\r
51  *\r
52  * @author Brinda Santh\r
53  */\r
54 @Service\r
55 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)\r
56 open class ResourceAssignmentEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService)\r
57     : ResourceAssignmentEnhancerService {\r
58     private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentEnhancerServiceImpl::class.java)\r
59 \r
60     /**\r
61      * Get the defined source instance from the ResourceAssignment,\r
62      * then get the NodeType of the Sources assigned\r
63      */\r
64     override fun enhanceBluePrint(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,\r
65                                   bluePrintContext: BluePrintContext, error: BluePrintError,\r
66                                   resourceAssignments: List<ResourceAssignment>) {\r
67 \r
68         val uniqueSourceNodeTypeNames = hashSetOf<String>()\r
69 \r
70         // Iterate the Resource Assignment and\r
71         resourceAssignments.map { resourceAssignment ->\r
72             val dictionaryName = resourceAssignment.dictionaryName!!\r
73             val dictionarySource = resourceAssignment.dictionarySource!!\r
74             log.debug("Enriching Assignment name({}), dictionary name({}), source({})", resourceAssignment.name,\r
75                     dictionaryName, dictionarySource)\r
76             val sourceNodeTypeName = ResourceSourceMappingFactory.getRegisterSourceMapping(dictionarySource)\r
77 \r
78             // Add Unique Node Types\r
79             uniqueSourceNodeTypeNames.add(sourceNodeTypeName)\r
80 \r
81             // TODO("Candidate for Optimisation")\r
82             if (checkResourceDefinitionNeeded(resourceAssignment)) {\r
83 \r
84                 bluePrintTypeEnhancerService.enhancePropertyDefinition(bluePrintContext, error, resourceAssignment.name,\r
85                         resourceAssignment.property!!);\r
86 \r
87                 // Get the Resource Definition from Repo\r
88                 val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)\r
89 \r
90                 val sourceNodeTemplate = resourceDefinition.sources.get(dictionarySource)\r
91                         ?: throw BluePrintException(format("failed to get assigned dictionarySource({}) from resourceDefinition({})", dictionarySource, dictionaryName))\r
92 \r
93                 // Enrich as NodeTemplate\r
94                 bluePrintTypeEnhancerService.enhanceNodeTemplate(bluePrintContext, error, dictionarySource, sourceNodeTemplate)\r
95             }\r
96         }\r
97         // Enrich the ResourceSource NodeTypes\r
98         uniqueSourceNodeTypeNames.map { nodeTypeName ->\r
99             val nodeType = resourceDefinitionRepoService.getNodeType(nodeTypeName)\r
100             bluePrintTypeEnhancerService.enhanceNodeType(bluePrintContext, error, nodeTypeName, nodeType)\r
101         }\r
102 \r
103     }\r
104 \r
105     /*\r
106         override fun enhanceBluePrint(resourceAssignments: List<ResourceAssignment>): ServiceTemplate {\r
107             val bluePrintEnhancerService = BluePrintEnhancerServiceImpl(resourceDefinitionRepoService)\r
108             bluePrintEnhancerService.serviceTemplate = ServiceTemplate()\r
109             bluePrintEnhancerService.initialCleanUp()\r
110             enhanceBluePrint(bluePrintEnhancerService, resourceAssignments)\r
111             return bluePrintEnhancerService.serviceTemplate\r
112         }\r
113     */\r
114     private fun checkResourceDefinitionNeeded(resourceAssignment: ResourceAssignment): Boolean {\r
115         return !((resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_INPUT)\r
116                 || resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_DEFAULT))\r
117                 && BluePrintTypes.validPrimitiveOrCollectionPrimitive(resourceAssignment.property!!))\r
118     }\r
119 \r
120     private fun getResourceDefinition(name: String): ResourceDefinition {\r
121         return resourceDefinitionRepoService.getResourceDefinition(name)\r
122     }\r
123 }