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