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