a4a5e05ec0a9593561eac7aaaebdba73deb01171
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
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.core.dsl
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.NullNode
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.bpClone
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
24
25 /**
26  * Generate Service Template for the simplified DSL.
27  * @author Brinda Santh
28  */
29 class BluePrintServiceTemplateGenerator(private val dslBluePrint: DSLBluePrint) {
30
31     private var serviceTemplate = ServiceTemplate()
32
33     private val nodeTypes: MutableMap<String, NodeType> = hashMapOf()
34     private val artifactTypes: MutableMap<String, ArtifactType> = hashMapOf()
35     private val dataTypes: MutableMap<String, DataType> = hashMapOf()
36
37     fun serviceTemplate(): ServiceTemplate {
38         serviceTemplate.metadata = dslBluePrint.metadata
39         serviceTemplate.dslDefinitions = dslBluePrint.properties
40
41         dataTypes.putAll(dslBluePrint.dataTypes)
42         artifactTypes.putAll(dslBluePrint.artifactTypes)
43
44         serviceTemplate.dataTypes = dataTypes
45         serviceTemplate.artifactTypes = artifactTypes
46         serviceTemplate.nodeTypes = nodeTypes
47
48         serviceTemplate.topologyTemplate = populateTopologyTemplate()
49
50         return serviceTemplate
51     }
52
53     private fun populateTopologyTemplate(): TopologyTemplate {
54         val topologyTemplate = TopologyTemplate()
55         topologyTemplate.nodeTemplates = populateNodeTemplates()
56         topologyTemplate.workflows = populateWorkflow()
57         return topologyTemplate
58     }
59
60     private fun populateNodeTemplates(): MutableMap<String, NodeTemplate> {
61
62         val nodeTemplates: MutableMap<String, NodeTemplate> = hashMapOf()
63
64         // For New or Dynamic Components
65         val components = dslBluePrint.components
66         components.forEach { (dslCompName, dslComp) ->
67             val nodeTemplate = NodeTemplate()
68             nodeTemplate.type = dslComp.type
69             nodeTemplate.properties = propertyAssignments(dslComp.properties)
70             nodeTemplate.artifacts = dslComp.artifacts
71             nodeTemplate.interfaces = populateInterfaceAssignments(dslComp)
72             nodeTemplates[dslCompName] = nodeTemplate
73
74             /** Populate Type **/
75             nodeTypes[dslComp.type] = populateNodeType(dslComp)
76         }
77
78         // For Registry Components
79         val registryComponents = dslBluePrint.registryComponents
80         registryComponents.forEach { (dslCompName, dslComp) ->
81             val nodeTemplate = NodeTemplate()
82             nodeTemplate.type = dslComp.type
83             nodeTemplate.properties = dslComp.properties
84             nodeTemplate.artifacts = dslComp.artifacts
85             nodeTemplate.interfaces = populateInterfaceAssignments(dslComp)
86             nodeTemplates[dslCompName] = nodeTemplate
87         }
88         return nodeTemplates
89     }
90
91
92     private fun populateWorkflow(): MutableMap<String, Workflow>? {
93         var workflows: MutableMap<String, Workflow>? = null
94         if (dslBluePrint.workflows.isNotEmpty()) {
95             workflows = hashMapOf()
96
97             dslBluePrint.workflows.forEach { (dslWorkflowName, dslWorkflow) ->
98                 val workflow = Workflow()
99                 workflow.description = dslWorkflow.description
100                 workflow.steps = dslWorkflow.steps
101                 workflow.inputs = dslWorkflow.inputs
102                 workflow.outputs = dslWorkflow.outputs
103                 workflows[dslWorkflowName] = workflow
104             }
105         }
106         return workflows
107     }
108
109     private fun populateNodeType(dslComponent: DSLComponent): NodeType {
110         val nodeType = NodeType()
111         nodeType.derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT
112         nodeType.version = dslComponent.version
113         nodeType.description = dslComponent.description
114         nodeType.interfaces = populateInterfaceDefinitions(dslComponent, nodeType)
115         return nodeType
116     }
117
118     private fun populateInterfaceDefinitions(dslComponent: DSLComponent, nodeType: NodeType): MutableMap<String, InterfaceDefinition> {
119
120         // Populate Node Type Attribute
121         nodeType.attributes = attributeDefinitions(dslComponent.attributes)
122
123         val operationDefinition = OperationDefinition()
124         operationDefinition.inputs = propertyDefinitions(dslComponent.inputs)
125         operationDefinition.outputs = propertyDefinitions(dslComponent.outputs)
126
127         val operations: MutableMap<String, OperationDefinition> = hashMapOf()
128         operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationDefinition
129
130         val interfaceDefinition = InterfaceDefinition()
131         interfaceDefinition.operations = operations
132
133         val interfaces: MutableMap<String, InterfaceDefinition> = hashMapOf()
134         interfaces[BluePrintConstants.DEFAULT_STEP_INTERFACE] = interfaceDefinition
135         return interfaces
136     }
137
138     private fun populateInterfaceAssignments(dslComponent: DSLRegistryComponent): MutableMap<String, InterfaceAssignment> {
139         val operationAssignment = OperationAssignment()
140         operationAssignment.implementation = dslComponent.implementation
141         operationAssignment.inputs = dslComponent.inputs
142         operationAssignment.outputs = dslComponent.outputs
143
144         val operations: MutableMap<String, OperationAssignment> = hashMapOf()
145         operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationAssignment
146
147         val interfaceAssignment = InterfaceAssignment()
148         interfaceAssignment.operations = operations
149
150         val interfaces: MutableMap<String, InterfaceAssignment> = hashMapOf()
151         interfaces[dslComponent.interfaceName] = interfaceAssignment
152         return interfaces
153     }
154
155     private fun populateInterfaceAssignments(dslComponent: DSLComponent): MutableMap<String, InterfaceAssignment> {
156         val operationAssignment = OperationAssignment()
157         operationAssignment.implementation = dslComponent.implementation
158         operationAssignment.inputs = propertyAssignments(dslComponent.inputs)
159         operationAssignment.outputs = propertyAssignments(dslComponent.outputs)
160
161         val operations: MutableMap<String, OperationAssignment> = hashMapOf()
162         operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationAssignment
163
164         val interfaceAssignment = InterfaceAssignment()
165         interfaceAssignment.operations = operations
166
167         val interfaces: MutableMap<String, InterfaceAssignment> = hashMapOf()
168         interfaces[BluePrintConstants.DEFAULT_STEP_INTERFACE] = interfaceAssignment
169         return interfaces
170     }
171
172     private fun propertyDefinitions(propertyDefinitions: Map<String, PropertyDefinition>?): MutableMap<String, PropertyDefinition>? {
173         val definitions: MutableMap<String, PropertyDefinition>? = propertyDefinitions?.bpClone()?.toMutableMap()
174
175         definitions?.forEach { (_, prop) ->
176             prop.value = null
177         }
178         return definitions
179     }
180
181     private fun attributeDefinitions(attributeDefinitions: Map<String, AttributeDefinition>?): MutableMap<String, AttributeDefinition>? {
182         val definitions: MutableMap<String, AttributeDefinition>? = attributeDefinitions?.bpClone()?.toMutableMap()
183
184         definitions?.forEach { (_, prop) ->
185             prop.value = null
186         }
187         return definitions
188     }
189
190     private fun propertyAssignments(propertyDefinitions: Map<String, PropertyDefinition>?): MutableMap<String, JsonNode>? {
191         var assignments: MutableMap<String, JsonNode>? = null
192         if (propertyDefinitions != null) {
193             assignments = hashMapOf()
194             propertyDefinitions.forEach { (propertyName, property) ->
195                 assignments[propertyName] = property.value ?: NullNode.instance
196             }
197         }
198         return assignments
199     }
200 }