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