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