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