Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / enhancer / BluePrintNodeTypeEnhancerImpl.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.data.InterfaceDefinition
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationDefinition
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintNodeTypeEnhancer
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.service.utils.BluePrintEnhancerUtils
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory
33 import org.springframework.context.annotation.Scope
34 import org.springframework.stereotype.Service
35
36 @Service
37 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
38 open class BluePrintNodeTypeEnhancerImpl(private val bluePrintRepoService: BluePrintRepoService,
39                                          private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService) : BluePrintNodeTypeEnhancer {
40
41     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintNodeTypeEnhancerImpl::class.toString())
42
43     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
44     lateinit var bluePrintContext: BluePrintContext
45
46
47     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeType: NodeType) {
48         this.bluePrintRuntimeService = bluePrintRuntimeService
49         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
50
51
52         val derivedFrom = nodeType.derivedFrom
53
54         if (!BluePrintTypes.rootNodeTypes().contains(derivedFrom)) {
55             val derivedFromNodeType = BluePrintEnhancerUtils.populateNodeType(bluePrintContext, bluePrintRepoService, name)
56             // Enrich NodeType
57             enhance(bluePrintRuntimeService, derivedFrom, derivedFromNodeType)
58         }
59
60         // NodeType Attribute Definitions
61         enrichNodeTypeAttributes(name, nodeType)
62
63         // NodeType Property Definitions
64         enrichNodeTypeProperties(name, nodeType)
65
66         //NodeType Requirement
67         enrichNodeTypeRequirements(name, nodeType)
68
69         //NodeType Capability
70         enrichNodeTypeCapabilityProperties(name, nodeType)
71
72         //NodeType Interface
73         enrichNodeTypeInterfaces(name, nodeType)
74
75     }
76
77     open fun enrichNodeTypeAttributes(nodeTypeName: String, nodeType: NodeType) {
78         nodeType.attributes?.let {
79             bluePrintTypeEnhancerService.enhanceAttributeDefinitions(bluePrintRuntimeService, nodeType.attributes!!)
80         }
81     }
82
83     open fun enrichNodeTypeProperties(nodeTypeName: String, nodeType: NodeType) {
84         nodeType.properties?.let {
85             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, nodeType.properties!!)
86         }
87     }
88
89     open fun enrichNodeTypeRequirements(nodeTypeName: String, nodeType: NodeType) {
90
91         nodeType.requirements?.forEach { requirementName, requirementDefinition ->
92             // Populate Requirement Node
93             requirementDefinition.node?.let { requirementNodeTypeName ->
94                 // Get Requirement NodeType from Repo and Update Service Template
95                 val requirementNodeType = BluePrintEnhancerUtils.populateNodeType(bluePrintContext,
96                         bluePrintRepoService, requirementNodeTypeName)
97                 // Enhance Node Type
98                 enhance(bluePrintRuntimeService, requirementNodeTypeName, requirementNodeType)
99
100                 // Enhance Relationship Type
101                 val relationShipTypeName = requirementDefinition.relationship
102                         ?: throw BluePrintException("couldn't get relationship name for the NodeType($nodeTypeName) " +
103                                 "Requirement($requirementName)")
104                 enrichRelationShipType(relationShipTypeName)
105             }
106         }
107     }
108
109     open fun enrichNodeTypeCapabilityProperties(nodeTypeName: String, nodeType: NodeType) {
110         nodeType.capabilities?.forEach { _, capabilityDefinition ->
111             capabilityDefinition.properties?.let { properties ->
112                 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, properties)
113             }
114         }
115     }
116
117     open fun enrichNodeTypeInterfaces(nodeTypeName: String, nodeType: NodeType) {
118         nodeType.interfaces?.forEach { interfaceName, interfaceObj ->
119             // Populate Node type Interface Operation
120             log.debug("Enriching NodeType({}) Interface({})", nodeTypeName, interfaceName)
121             populateNodeTypeInterfaceOperation(nodeTypeName, interfaceName, interfaceObj)
122
123         }
124     }
125
126     open fun populateNodeTypeInterfaceOperation(nodeTypeName: String, interfaceName: String, interfaceObj: InterfaceDefinition) {
127
128         interfaceObj.operations?.forEach { operationName, operation ->
129             enrichNodeTypeInterfaceOperationInputs(nodeTypeName, operationName, operation)
130             enrichNodeTypeInterfaceOperationOutputs(nodeTypeName, operationName, operation)
131         }
132     }
133
134     open fun enrichNodeTypeInterfaceOperationInputs(nodeTypeName: String, operationName: String, operation: OperationDefinition) {
135         operation.inputs?.let { inputs ->
136             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
137         }
138     }
139
140     open fun enrichNodeTypeInterfaceOperationOutputs(nodeTypeName: String, operationName: String,
141                                                      operation: OperationDefinition) {
142         operation.outputs?.let { inputs ->
143             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
144         }
145     }
146
147     /**
148      * Get the Relationship Type from database and add to Blueprint Context
149      */
150     open fun enrichRelationShipType(relationshipName: String) {
151         BluePrintEnhancerUtils.populateRelationshipType(bluePrintContext, bluePrintRepoService, relationshipName)
152     }
153
154 }