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