Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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
342     private val dslWorkflow = DSLWorkflow()
343     private var steps: MutableMap<String, Step>? = null
344     private var inputs: MutableMap<String, PropertyDefinition>? = null
345     private var outputs: MutableMap<String, PropertyDefinition>? = null
346
347     fun input(id: String, type: String, required: Boolean, description: String? = "") {
348         if (inputs == null)
349             inputs = hashMapOf()
350         val property = PropertyDefinitionBuilder(id, type, required, description)
351         inputs!![id] = property.build()
352     }
353
354     fun input(
355         id: String,
356         type: String,
357         required: Boolean,
358         description: String,
359         defaultValue: Any?,
360         block: PropertyDefinitionBuilder.() -> Unit
361     ) {
362         if (inputs == null)
363             inputs = hashMapOf()
364         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
365         if (defaultValue != null)
366             property.defaultValue = defaultValue.asJsonType()
367         inputs!![id] = property
368     }
369
370     fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
371         if (outputs == null)
372             outputs = hashMapOf()
373         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
374         outputs!![id] = property.build()
375     }
376
377     fun output(
378         id: String,
379         type: String,
380         required: Boolean,
381         expression: Any,
382         description: String? = "",
383         block: DSLPropertyDefinitionBuilder.() -> Unit
384     ) {
385         if (outputs == null)
386             outputs = hashMapOf()
387         val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
388             .apply(block).build()
389         outputs!![id] = property
390     }
391
392     fun step(id: String, target: String, description: String) {
393         if (steps == null)
394             steps = hashMapOf()
395         steps!![id] = StepBuilder(id, target, description).build()
396     }
397
398     fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
399         if (steps == null)
400             steps = hashMapOf()
401         steps!![id] = StepBuilder(id, target, description).apply(block).build()
402     }
403
404     fun build(): DSLWorkflow {
405         dslWorkflow.actionName = actionName
406         dslWorkflow.description = description
407         dslWorkflow.inputs = inputs
408         dslWorkflow.outputs = outputs
409         dslWorkflow.steps = steps!!
410         return dslWorkflow
411     }
412 }
413
414 class DSLAttributeDefinitionBuilder(
415     private val id: String,
416     private val type: String? = BlueprintConstants.DATA_TYPE_STRING,
417     private val required: Boolean? = false,
418     private val expression: JsonNode,
419     private val description: String? = ""
420 ) {
421
422     private var attributeDefinition = AttributeDefinition()
423
424     fun entrySchema(entrySchemaType: String) {
425         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
426     }
427
428     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
429         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
430     }
431     // TODO("Constrains")
432
433     fun defaultValue(defaultValue: JsonNode) {
434         attributeDefinition.defaultValue = defaultValue
435     }
436
437     fun build(): AttributeDefinition {
438         attributeDefinition.id = id
439         attributeDefinition.type = type!!
440         attributeDefinition.required = required
441         attributeDefinition.value = expression
442         attributeDefinition.description = description
443         return attributeDefinition
444     }
445 }
446
447 class DSLPropertyDefinitionBuilder(
448     private val id: String,
449     private val type: String? = BlueprintConstants.DATA_TYPE_STRING,
450     private val required: Boolean? = false,
451     private val expression: JsonNode,
452     private val description: String? = ""
453 ) {
454
455     private var propertyDefinition: PropertyDefinition = PropertyDefinition()
456
457     fun entrySchema(entrySchemaType: String) {
458         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
459     }
460
461     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
462         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
463     }
464     // TODO("Constrains")
465
466     fun defaultValue(defaultValue: JsonNode) {
467         propertyDefinition.defaultValue = defaultValue
468     }
469
470     fun build(): PropertyDefinition {
471         propertyDefinition.id = id
472         propertyDefinition.type = type!!
473         propertyDefinition.required = required
474         propertyDefinition.value = expression
475         propertyDefinition.description = description
476         return propertyDefinition
477     }
478 }