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