941be07f1d869375a72f9a2bf170db4547201790
[ccsdk/cds.git] /
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.apps.controllerblueprints.service.enhancer
18
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.InterfaceDefinition
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationDefinition
26 import org.onap.ccsdk.apps.controllerblueprints.core.format
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintNodeTypeEnhancer
28 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
29 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
31 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
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 = populateNodeType(name)
56             // Enrich NodeType
57             enhance(bluePrintRuntimeService, derivedFrom, derivedFromNodeType)
58         }
59
60         // NodeType Property Definitions
61         enrichNodeTypeProperties(name, nodeType)
62
63         //NodeType Requirement
64         enrichNodeTypeRequirements(name, nodeType)
65
66         //NodeType Capability
67         enrichNodeTypeCapabilityProperties(name, nodeType)
68
69         //NodeType Interface
70         enrichNodeTypeInterfaces(name, nodeType)
71
72     }
73
74     open fun enrichNodeTypeProperties(nodeTypeName: String, nodeType: NodeType) {
75         nodeType.properties?.let {
76             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, nodeType.properties!!)
77         }
78     }
79
80     open fun enrichNodeTypeRequirements(nodeTypeName: String, nodeType: NodeType) {
81
82         nodeType.requirements?.forEach { _, requirementDefinition ->
83             // Populate Requirement Node
84             requirementDefinition.node?.let { requirementNodeTypeName ->
85                 // Get Requirement NodeType from Repo and Update Service Template
86                 val requirementNodeType = populateNodeType(requirementNodeTypeName)
87                 // Enhanypece Node T
88                 enhance(bluePrintRuntimeService, requirementNodeTypeName, requirementNodeType)
89             }
90         }
91     }
92
93     open fun enrichNodeTypeCapabilityProperties(nodeTypeName: String, nodeType: NodeType) {
94         nodeType.capabilities?.forEach { _, capabilityDefinition ->
95             capabilityDefinition.properties?.let { properties ->
96                 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, properties)
97             }
98         }
99     }
100
101     open fun enrichNodeTypeInterfaces(nodeTypeName: String, nodeType: NodeType) {
102         nodeType.interfaces?.forEach { interfaceName, interfaceObj ->
103             // Populate Node type Interface Operation
104             log.debug("Enriching NodeType({}) Interface({})", nodeTypeName, interfaceName)
105             populateNodeTypeInterfaceOperation(nodeTypeName, interfaceName, interfaceObj)
106
107         }
108     }
109
110     open fun populateNodeTypeInterfaceOperation(nodeTypeName: String, interfaceName: String, interfaceObj: InterfaceDefinition) {
111
112         interfaceObj.operations?.forEach { operationName, operation ->
113             enrichNodeTypeInterfaceOperationInputs(nodeTypeName, operationName, operation)
114             enrichNodeTypeInterfaceOperationOputputs(nodeTypeName, operationName, operation)
115         }
116     }
117
118     open fun enrichNodeTypeInterfaceOperationInputs(nodeTypeName: String, operationName: String, operation: OperationDefinition) {
119         operation.inputs?.let { inputs ->
120             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
121         }
122     }
123
124     open fun enrichNodeTypeInterfaceOperationOputputs(nodeTypeName: String, operationName: String, operation: OperationDefinition) {
125         operation.outputs?.let { inputs ->
126             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
127         }
128     }
129
130     open fun populateNodeType(nodeTypeName: String): NodeType {
131
132         val nodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(nodeTypeName)
133                 ?: bluePrintRepoService.getNodeType(nodeTypeName)
134                 ?: throw BluePrintException(format("Couldn't get NodeType({}) from repo.", nodeTypeName))
135         bluePrintContext.serviceTemplate.nodeTypes?.put(nodeTypeName, nodeType)
136         return nodeType
137     }
138
139 }