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