efb695b2281df09f530d565993aca37cf5d78a2b
[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(private val bluePrintRepoService: BluePrintRepoService,
38                                          private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService) : BluePrintNodeTypeEnhancer {
39
40     private val log= logger(BluePrintNodeTypeEnhancerImpl::class)
41
42     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
43     lateinit var bluePrintContext: BluePrintContext
44
45
46     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeType: NodeType) {
47         this.bluePrintRuntimeService = bluePrintRuntimeService
48         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
49
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
76     open fun enrichNodeTypeAttributes(nodeTypeName: String, nodeType: NodeType) {
77         nodeType.attributes?.let {
78             bluePrintTypeEnhancerService.enhanceAttributeDefinitions(bluePrintRuntimeService, nodeType.attributes!!)
79         }
80     }
81
82     open fun enrichNodeTypeProperties(nodeTypeName: String, nodeType: NodeType) {
83         nodeType.properties?.let {
84             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, nodeType.properties!!)
85         }
86     }
87
88     open fun enrichNodeTypeRequirements(nodeTypeName: String, nodeType: NodeType) {
89
90         nodeType.requirements?.forEach { requirementName, requirementDefinition ->
91             // Populate Requirement Node
92             requirementDefinition.node?.let { requirementNodeTypeName ->
93                 // Get Requirement NodeType from Repo and Update Service Template
94                 val requirementNodeType = BluePrintEnhancerUtils.populateNodeType(bluePrintContext,
95                         bluePrintRepoService, requirementNodeTypeName)
96                 // Enhance Node Type
97                 enhance(bluePrintRuntimeService, requirementNodeTypeName, requirementNodeType)
98
99                 // Enhance Relationship Type
100                 val relationShipTypeName = requirementDefinition.relationship
101                         ?: throw BluePrintException("couldn't get relationship name for the NodeType($nodeTypeName) " +
102                                 "Requirement($requirementName)")
103                 enrichRelationShipType(relationShipTypeName)
104             }
105         }
106     }
107
108     open fun enrichNodeTypeCapabilityProperties(nodeTypeName: String, nodeType: NodeType) {
109         nodeType.capabilities?.forEach { _, capabilityDefinition ->
110             capabilityDefinition.properties?.let { properties ->
111                 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, properties)
112             }
113         }
114     }
115
116     open fun enrichNodeTypeInterfaces(nodeTypeName: String, nodeType: NodeType) {
117         nodeType.interfaces?.forEach { interfaceName, interfaceObj ->
118             // Populate Node type Interface Operation
119             log.debug("Enriching NodeType({}) Interface({})", nodeTypeName, interfaceName)
120             populateNodeTypeInterfaceOperation(nodeTypeName, interfaceName, interfaceObj)
121
122         }
123     }
124
125     open fun populateNodeTypeInterfaceOperation(nodeTypeName: String, interfaceName: String, interfaceObj: InterfaceDefinition) {
126
127         interfaceObj.operations?.forEach { operationName, operation ->
128             enrichNodeTypeInterfaceOperationInputs(nodeTypeName, operationName, operation)
129             enrichNodeTypeInterfaceOperationOutputs(nodeTypeName, operationName, operation)
130         }
131     }
132
133     open fun enrichNodeTypeInterfaceOperationInputs(nodeTypeName: String, operationName: String, operation: OperationDefinition) {
134         operation.inputs?.let { inputs ->
135             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
136         }
137     }
138
139     open fun enrichNodeTypeInterfaceOperationOutputs(nodeTypeName: String, operationName: String,
140                                                      operation: OperationDefinition) {
141         operation.outputs?.let { inputs ->
142             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
143         }
144     }
145
146     /**
147      * Get the Relationship Type from database and add to Blueprint Context
148      */
149     open fun enrichRelationShipType(relationshipName: String) {
150         BluePrintEnhancerUtils.populateRelationshipType(bluePrintContext, bluePrintRepoService, relationshipName)
151     }
152
153 }