Formatting Code base with ktlint
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintDSLBuilder.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.ArtifactDefinition
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.Implementation
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
29
30 /**
31  * @author Brinda Santh
32  */
33 class DSLBluePrintBuilder(
34     private val name: String,
35     private val version: String,
36     private val author: String,
37     private val tags: String
38 ) {
39
40     private var dslBluePrint = DSLBluePrint()
41     private var metadata: MutableMap<String, String> = hashMapOf()
42     var properties: MutableMap<String, JsonNode>? = null
43     var dataTypes: MutableMap<String, DataType> = hashMapOf()
44     var artifactTypes: MutableMap<String, ArtifactType> = hashMapOf()
45     var components: MutableMap<String, DSLComponent> = hashMapOf()
46     private var registryComponents: MutableMap<String, DSLRegistryComponent> = hashMapOf()
47     var workflows: MutableMap<String, DSLWorkflow> = hashMapOf()
48
49     private fun initMetaData() {
50         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
51         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
52         metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
53         metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
54     }
55
56     fun metadata(id: String, value: String) {
57         metadata[id] = value
58     }
59
60     fun property(id: String, expression: Any) {
61         if (properties == null)
62             properties = hashMapOf()
63         properties!![id] = expression.asJsonType()
64     }
65
66     fun dataType(dataType: DataType) {
67         dataTypes[dataType.id!!] = dataType
68     }
69
70     fun dataType(
71         id: String,
72         version: String,
73         derivedFrom: String,
74         description: String,
75         block: DataTypeBuilder.() -> Unit
76     ) {
77         dataTypes[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
78     }
79
80     fun artifactType(artifactType: ArtifactType) {
81         artifactTypes[artifactType.id!!] = artifactType
82     }
83
84     fun artifactType(
85         id: String,
86         version: String,
87         derivedFrom: String,
88         description: String,
89         block: ArtifactTypeBuilder.() -> Unit
90     ) {
91         artifactTypes[id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
92     }
93
94     fun component(
95         id: String,
96         type: String,
97         version: String,
98         description: String,
99         block: DSLComponentBuilder.() -> Unit
100     ) {
101         components[id] = DSLComponentBuilder(id, type, version, description).apply(block).build()
102     }
103
104     fun registryComponent(
105         id: String,
106         type: String,
107         version: String,
108         interfaceName: String,
109         description: String,
110         block: DSLRegistryComponentBuilder.() -> Unit
111     ) {
112         registryComponents[id] = DSLRegistryComponentBuilder(id, type, version, interfaceName, description)
113             .apply(block).build()
114     }
115
116     fun workflow(id: String, description: String, block: DSLWorkflowBuilder.() -> Unit) {
117         workflows[id] = DSLWorkflowBuilder(id, description).apply(block).build()
118     }
119
120     fun build(): DSLBluePrint {
121         initMetaData()
122         dslBluePrint.metadata = metadata
123         dslBluePrint.properties = properties
124         dslBluePrint.dataTypes = dataTypes
125         dslBluePrint.artifactTypes = artifactTypes
126         dslBluePrint.components = components
127         dslBluePrint.registryComponents = registryComponents
128         dslBluePrint.workflows = workflows
129         return dslBluePrint
130     }
131 }
132
133 class DSLComponentBuilder(
134     private val id: String,
135     private val type: String,
136     private val version: String,
137     private val description: String
138 ) {
139
140     private val dslComponent = DSLComponent()
141     var properties: MutableMap<String, PropertyDefinition>? = null
142     var attributes: MutableMap<String, AttributeDefinition>? = null
143
144     var artifacts: MutableMap<String, ArtifactDefinition>? = null
145     var implementation: Implementation? = null
146     var inputs: MutableMap<String, PropertyDefinition>? = null
147     var outputs: MutableMap<String, PropertyDefinition>? = null
148
149     fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
150         if (attributes == null)
151             attributes = hashMapOf()
152         val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
153         attributes!![id] = attribute
154     }
155
156     fun attribute(
157         id: String,
158         type: String,
159         required: Boolean,
160         expression: Any,
161         description: String? = "",
162         block: DSLAttributeDefinitionBuilder.() -> Unit
163     ) {
164         if (attributes == null)
165             attributes = hashMapOf()
166         val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description)
167             .apply(block).build()
168         attributes!![id] = attribute
169     }
170
171     fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
172         if (properties == null)
173             properties = hashMapOf()
174         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
175         properties!![id] = property
176     }
177
178     fun property(
179         id: String,
180         type: String,
181         required: Boolean,
182         expression: Any,
183         description: String? = "",
184         block: DSLPropertyDefinitionBuilder.() -> Unit
185     ) {
186         if (properties == null)
187             properties = hashMapOf()
188         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
189             .apply(block).build()
190         properties!![id] = property
191     }
192
193     fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
194         implementation = Implementation().apply {
195             this.operationHost = operationHost!!
196             this.timeout = timeout
197         }
198     }
199
200     fun artifact(id: String, type: String, file: String) {
201         if (artifacts == null)
202             artifacts = hashMapOf()
203         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
204     }
205
206     fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
207         if (artifacts == null)
208             artifacts = hashMapOf()
209         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
210     }
211
212     fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
213         if (inputs == null)
214             inputs = hashMapOf()
215         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
216         inputs!![id] = property.build()
217     }
218
219     fun input(
220         id: String,
221         type: String,
222         required: Boolean,
223         expression: Any,
224         description: String? = "",
225         block: DSLPropertyDefinitionBuilder.() -> Unit
226     ) {
227         if (inputs == null)
228             inputs = hashMapOf()
229         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
230             .apply(block).build()
231         inputs!![id] = property
232     }
233
234     fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
235         if (outputs == null)
236             outputs = hashMapOf()
237         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
238         outputs!![id] = property.build()
239     }
240
241     fun output(
242         id: String,
243         type: String,
244         required: Boolean,
245         expression: Any,
246         description: String? = "",
247         block: DSLPropertyDefinitionBuilder.() -> Unit
248     ) {
249         if (outputs == null)
250             outputs = hashMapOf()
251         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
252             .apply(block).build()
253         outputs!![id] = property
254     }
255
256     fun build(): DSLComponent {
257         dslComponent.id = id
258         dslComponent.type = type
259         dslComponent.version = version
260         dslComponent.description = description
261         dslComponent.attributes = attributes
262         dslComponent.properties = properties
263         dslComponent.implementation = implementation
264         dslComponent.artifacts = artifacts
265         dslComponent.inputs = inputs
266         dslComponent.outputs = outputs
267
268         return dslComponent
269     }
270 }
271
272 class DSLRegistryComponentBuilder(
273     private val id: String,
274     private val type: String,
275     private val version: String,
276     private val interfaceName: String,
277     private val description: String
278 ) {
279
280     private val dslComponent = DSLRegistryComponent()
281     var properties: MutableMap<String, JsonNode>? = null
282
283     var artifacts: MutableMap<String, ArtifactDefinition>? = null
284     var implementation: Implementation? = null
285     var inputs: MutableMap<String, JsonNode>? = null
286     var outputs: MutableMap<String, JsonNode>? = null
287
288     fun property(id: String, expression: Any) {
289         if (properties == null)
290             properties = hashMapOf()
291         properties!![id] = expression.asJsonType()
292     }
293
294     fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
295         implementation = Implementation().apply {
296             this.operationHost = operationHost!!
297             this.timeout = timeout
298         }
299     }
300
301     fun artifact(id: String, type: String, file: String) {
302         if (artifacts == null)
303             artifacts = hashMapOf()
304         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
305     }
306
307     fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
308         if (artifacts == null)
309             artifacts = hashMapOf()
310         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
311     }
312
313     fun input(id: String, expression: Any) {
314         if (inputs == null)
315             inputs = hashMapOf()
316         inputs!![id] = expression.asJsonType()
317     }
318
319     fun output(id: String, expression: Any) {
320         if (outputs == null)
321             outputs = hashMapOf()
322         outputs!![id] = expression.asJsonType()
323     }
324
325     fun build(): DSLRegistryComponent {
326         dslComponent.id = id
327         dslComponent.type = type
328         dslComponent.version = version
329         dslComponent.interfaceName = interfaceName
330         dslComponent.description = description
331         dslComponent.properties = properties
332         dslComponent.implementation = implementation
333         dslComponent.artifacts = artifacts
334         dslComponent.inputs = inputs
335         dslComponent.outputs = outputs
336         return dslComponent
337     }
338 }
339
340 class DSLWorkflowBuilder(private val actionName: String, private val description: String) {
341     private val dslWorkflow = DSLWorkflow()
342     private var steps: MutableMap<String, Step>? = null
343     private var inputs: MutableMap<String, PropertyDefinition>? = null
344     private var outputs: MutableMap<String, PropertyDefinition>? = null
345
346     fun input(id: String, type: String, required: Boolean, description: String? = "") {
347         if (inputs == null)
348             inputs = hashMapOf()
349         val property = PropertyDefinitionBuilder(id, type, required, description)
350         inputs!![id] = property.build()
351     }
352
353     fun input(
354         id: String,
355         type: String,
356         required: Boolean,
357         description: String,
358         defaultValue: Any?,
359         block: PropertyDefinitionBuilder.() -> Unit
360     ) {
361         if (inputs == null)
362             inputs = hashMapOf()
363         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
364         if (defaultValue != null)
365             property.defaultValue = defaultValue.asJsonType()
366         inputs!![id] = property
367     }
368
369     fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
370         if (outputs == null)
371             outputs = hashMapOf()
372         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
373         outputs!![id] = property.build()
374     }
375
376     fun output(
377         id: String,
378         type: String,
379         required: Boolean,
380         expression: Any,
381         description: String? = "",
382         block: DSLPropertyDefinitionBuilder.() -> Unit
383     ) {
384         if (outputs == null)
385             outputs = hashMapOf()
386         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
387             .apply(block).build()
388         outputs!![id] = property
389     }
390
391     fun step(id: String, target: String, description: String) {
392         if (steps == null)
393             steps = hashMapOf()
394         steps!![id] = StepBuilder(id, target, description).build()
395     }
396
397     fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
398         if (steps == null)
399             steps = hashMapOf()
400         steps!![id] = StepBuilder(id, target, description).apply(block).build()
401     }
402
403     fun build(): DSLWorkflow {
404         dslWorkflow.actionName = actionName
405         dslWorkflow.description = description
406         dslWorkflow.inputs = inputs
407         dslWorkflow.outputs = outputs
408         dslWorkflow.steps = steps!!
409         return dslWorkflow
410     }
411 }
412
413 class DSLAttributeDefinitionBuilder(
414     private val id: String,
415     private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
416     private val required: Boolean? = false,
417     private val expression: JsonNode,
418     private val description: String? = ""
419 ) {
420
421     private var attributeDefinition = AttributeDefinition()
422
423     fun entrySchema(entrySchemaType: String) {
424         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
425     }
426
427     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
428         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
429     }
430     // TODO("Constrains")
431
432     fun defaultValue(defaultValue: JsonNode) {
433         attributeDefinition.defaultValue = defaultValue
434     }
435
436     fun build(): AttributeDefinition {
437         attributeDefinition.id = id
438         attributeDefinition.type = type!!
439         attributeDefinition.required = required
440         attributeDefinition.value = expression
441         attributeDefinition.description = description
442         return attributeDefinition
443     }
444 }
445
446 class DSLPropertyDefinitionBuilder(
447     private val id: String,
448     private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
449     private val required: Boolean? = false,
450     private val expression: JsonNode,
451     private val description: String? = ""
452 ) {
453
454     private var propertyDefinition: PropertyDefinition = PropertyDefinition()
455
456     fun entrySchema(entrySchemaType: String) {
457         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
458     }
459
460     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
461         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
462     }
463     // TODO("Constrains")
464
465     fun defaultValue(defaultValue: JsonNode) {
466         propertyDefinition.defaultValue = defaultValue
467     }
468
469     fun build(): PropertyDefinition {
470         propertyDefinition.id = id
471         propertyDefinition.type = type!!
472         propertyDefinition.required = required
473         propertyDefinition.value = expression
474         propertyDefinition.description = description
475         return propertyDefinition
476     }
477 }