Clean restconf duplicate models and Implementation.
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintTemplateDSLBuilder.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 org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
23
24 class TopologyTemplateBuilder {
25     private var topologyTemplate = TopologyTemplate()
26     private var nodeTemplates: MutableMap<String, NodeTemplate>? = null
27     private var workflows: MutableMap<String, Workflow>? = null
28
29     fun nodeTemplate(id: String, type: String, description: String, block: NodeTemplateBuilder.() -> Unit) {
30         if (nodeTemplates == null)
31             nodeTemplates = hashMapOf()
32         nodeTemplates!![id] = NodeTemplateBuilder(id, type, description).apply(block).build()
33     }
34
35     fun nodeTemplateOperation(nodeTemplateName: String, type: String, interfaceName: String, description: String,
36                               operationBlock: OperationAssignmentBuilder.() -> Unit) {
37         if (nodeTemplates == null)
38             nodeTemplates = hashMapOf()
39
40         val nodeTemplateBuilder = NodeTemplateBuilder(nodeTemplateName, type, description)
41         nodeTemplateBuilder.operation(interfaceName, "$description operation", operationBlock)
42         nodeTemplates!![nodeTemplateName] = nodeTemplateBuilder.build()
43     }
44
45     fun workflow(id: String, description: String, block: WorkflowBuilder.() -> Unit) {
46         if (workflows == null)
47             workflows = hashMapOf()
48         workflows!![id] = WorkflowBuilder(id, description).apply(block).build()
49     }
50
51     //TODO("populate inputs, outputs")
52     fun workflowNodeTemplate(actionName: String,
53                              nodeTemplateType: String, description: String, block: NodeTemplateBuilder.() -> Unit) {
54         if (nodeTemplates == null)
55             nodeTemplates = hashMapOf()
56
57         if (workflows == null)
58             workflows = hashMapOf()
59
60         val workflowBuilder = WorkflowBuilder(actionName, description)
61         workflowBuilder.nodeTemplateStep(actionName, description)
62         // Workflow name is NodeTemplate name
63         workflows!![actionName] = workflowBuilder.build()
64
65         nodeTemplates!![actionName] = NodeTemplateBuilder(actionName, nodeTemplateType, description).apply(block).build()
66     }
67
68     fun build(): TopologyTemplate {
69         topologyTemplate.nodeTemplates = nodeTemplates
70         topologyTemplate.workflows = workflows
71         return topologyTemplate
72     }
73 }
74
75 class NodeTemplateBuilder(private val id: String,
76                           private val type: String,
77                           private val description: String? = "") {
78     private var nodeTemplate: NodeTemplate = NodeTemplate()
79     private var properties: MutableMap<String, JsonNode>? = null
80     private var interfaces: MutableMap<String, InterfaceAssignment>? = null
81     private var artifacts: MutableMap<String, ArtifactDefinition>? = null
82     private var capabilities: MutableMap<String, CapabilityAssignment>? = null
83     private var requirements: MutableMap<String, RequirementAssignment>? = null
84
85     fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
86         if (properties == null)
87             properties = hashMapOf()
88         properties = PropertiesAssignmentBuilder().apply(block).build()
89     }
90
91     fun operation(interfaceName: String, description: String? = "",
92                   block: OperationAssignmentBuilder.() -> Unit) {
93         if (interfaces == null)
94             interfaces = hashMapOf()
95
96         val interfaceAssignment = InterfaceAssignment()
97         val defaultOperationName = BluePrintConstants.DEFAULT_STEP_OPERATION
98         interfaceAssignment.operations = hashMapOf()
99         interfaceAssignment.operations!![defaultOperationName] =
100                 OperationAssignmentBuilder(defaultOperationName, description).apply(block).build()
101         interfaces!![interfaceName] = interfaceAssignment
102     }
103
104     fun artifact(id: String, type: String, file: String) {
105         if (artifacts == null)
106             artifacts = hashMapOf()
107         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
108     }
109
110     fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
111         if (artifacts == null)
112             artifacts = hashMapOf()
113         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
114     }
115
116     fun capability(id: String, block: CapabilityAssignmentBuilder.() -> Unit) {
117         if (capabilities == null)
118             capabilities = hashMapOf()
119         capabilities!![id] = CapabilityAssignmentBuilder(id).apply(block).build()
120     }
121
122     fun requirement(id: String, capability: String, node: String, relationship: String) {
123         if (requirements == null)
124             requirements = hashMapOf()
125         requirements!![id] = RequirementAssignmentBuilder(id, capability, node, relationship).build()
126     }
127
128     fun build(): NodeTemplate {
129         nodeTemplate.id = id
130         nodeTemplate.type = type
131         nodeTemplate.description = description
132         nodeTemplate.properties = properties
133         nodeTemplate.interfaces = interfaces
134         nodeTemplate.artifacts = artifacts
135         nodeTemplate.capabilities = capabilities
136         nodeTemplate.requirements = requirements
137         return nodeTemplate
138     }
139 }
140
141 class ArtifactDefinitionBuilder(private val id: String, private val type: String, private val file: String) {
142
143     private var artifactDefinition: ArtifactDefinition = ArtifactDefinition()
144     private var properties: MutableMap<String, JsonNode>? = null
145
146     fun repository(repository: String) {
147         artifactDefinition.repository = repository
148     }
149
150     fun deployPath(deployPath: String) {
151         artifactDefinition.deployPath = deployPath
152     }
153
154     fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
155         if (properties == null)
156             properties = hashMapOf()
157         properties = PropertiesAssignmentBuilder().apply(block).build()
158     }
159
160     fun build(): ArtifactDefinition {
161         artifactDefinition.id = id
162         artifactDefinition.type = type
163         artifactDefinition.file = file
164         artifactDefinition.properties = properties
165         return artifactDefinition
166     }
167 }
168
169 class CapabilityAssignmentBuilder(private val id: String) {
170     private var capabilityAssignment: CapabilityAssignment = CapabilityAssignment()
171     private var attributes: MutableMap<String, JsonNode>? = null
172     private var properties: MutableMap<String, JsonNode>? = null
173
174     fun attributes(block: AttributesAssignmentBuilder.() -> Unit) {
175         if (attributes == null)
176             attributes = hashMapOf()
177         attributes = AttributesAssignmentBuilder().apply(block).build()
178     }
179
180     fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
181         if (properties == null)
182             properties = hashMapOf()
183         properties = PropertiesAssignmentBuilder().apply(block).build()
184     }
185
186     fun build(): CapabilityAssignment {
187         capabilityAssignment.properties = properties
188         capabilityAssignment.attributes = attributes
189         return capabilityAssignment
190     }
191 }
192
193 class RequirementAssignmentBuilder(private val id: String, private val capability: String,
194                                    private val node: String,
195                                    private val relationship: String) {
196     private var requirementAssignment: RequirementAssignment = RequirementAssignment()
197
198     fun build(): RequirementAssignment {
199         requirementAssignment.id = id
200         requirementAssignment.capability = capability
201         requirementAssignment.node = node
202         requirementAssignment.relationship = relationship
203         return requirementAssignment
204     }
205 }
206
207 class InterfaceAssignmentBuilder(private val id: String) {
208
209     private var interfaceAssignment: InterfaceAssignment = InterfaceAssignment()
210     private var operations: MutableMap<String, OperationAssignment>? = null
211
212     fun operation(id: String, description: String? = "", block: OperationAssignmentBuilder.() -> Unit) {
213         if (operations == null)
214             operations = hashMapOf()
215         operations!![id] = OperationAssignmentBuilder(id, description).apply(block).build()
216     }
217
218     fun build(): InterfaceAssignment {
219         interfaceAssignment.id = id
220         interfaceAssignment.operations = operations
221         return interfaceAssignment
222     }
223 }
224
225 class OperationAssignmentBuilder(private val id: String,
226                                  private val description: String? = "") {
227
228     private var operationAssignment: OperationAssignment = OperationAssignment()
229
230     fun implementation(implementation: Implementation?) {
231         operationAssignment.implementation = implementation
232     }
233
234     fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
235         val implementation = Implementation().apply {
236             this.operationHost = operationHost!!
237             this.timeout = timeout
238         }
239         operationAssignment.implementation = implementation
240     }
241
242     fun inputs(inputs: MutableMap<String, JsonNode>?) {
243         operationAssignment.inputs = inputs
244     }
245
246     fun inputs(block: PropertiesAssignmentBuilder.() -> Unit) {
247         operationAssignment.inputs = PropertiesAssignmentBuilder().apply(block).build()
248     }
249
250     fun outputs(outputs: MutableMap<String, JsonNode>?) {
251         operationAssignment.outputs = outputs
252     }
253
254     fun outputs(block: PropertiesAssignmentBuilder.() -> Unit) {
255         operationAssignment.outputs = PropertiesAssignmentBuilder().apply(block).build()
256     }
257
258     fun build(): OperationAssignment {
259         operationAssignment.id = id
260         operationAssignment.description = description
261         return operationAssignment
262     }
263 }
264
265 class PropertiesAssignmentBuilder {
266     private var properties: MutableMap<String, JsonNode> = hashMapOf()
267
268     fun property(id: String, value: Any) {
269         property(id, value.asJsonType())
270     }
271
272     fun property(id: String, value: JsonNode) {
273         properties[id] = value
274     }
275
276     fun build(): MutableMap<String, JsonNode> {
277         return properties
278     }
279 }
280
281 class AttributesAssignmentBuilder {
282     private var attributes: MutableMap<String, JsonNode> = hashMapOf()
283
284     fun attribute(id: String, value: String) {
285         attribute(id, value.asJsonType())
286     }
287
288     fun attribute(id: String, value: JsonNode) {
289         attributes[id] = value
290     }
291
292     fun build(): MutableMap<String, JsonNode> {
293         return attributes
294     }
295 }