Merge "Enable dynamic remote python executor"
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintTypeDSLBuilder.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 com.fasterxml.jackson.databind.node.ArrayNode
21 import org.onap.ccsdk.cds.controllerblueprints.core.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
23
24
25 open class EntityTypeBuilder(private val id: String,
26                              private val version: String,
27                              private val derivedFrom: String,
28                              private val description: String? = "") {
29     var metadata: MutableMap<String, String>? = null
30     var properties: MutableMap<String, PropertyDefinition>? = null
31     var attributes: MutableMap<String, AttributeDefinition>? = null
32
33     fun metadata(key: String, value: String) {
34         if (metadata == null)
35             metadata = hashMapOf()
36         metadata!![key] = value
37     }
38
39     fun attribute(id: String, type: String, required: Boolean, description: String? = "") {
40         if (attributes == null)
41             attributes = hashMapOf()
42         val attribute = AttributeDefinitionBuilder(id, type, required, description).build()
43         attributes!![id] = attribute
44     }
45
46     fun attribute(id: String, type: String, required: Boolean, description: String? = "",
47                   block: AttributeDefinitionBuilder.() -> Unit) {
48         if (attributes == null)
49             attributes = hashMapOf()
50         val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
51         attributes!![id] = attribute
52     }
53
54     fun property(id: String, type: String, required: Boolean, description: String? = "") {
55         if (properties == null)
56             properties = hashMapOf()
57         val property = PropertyDefinitionBuilder(id, type, required, description).build()
58         properties!![id] = property
59     }
60
61     fun property(id: String, type: String, required: Boolean, description: String? = "",
62                  block: PropertyDefinitionBuilder.() -> Unit) {
63         if (properties == null)
64             properties = hashMapOf()
65         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
66         properties!![id] = property
67     }
68
69     fun buildEntityType(entity: EntityType) {
70         entity.id = id
71         entity.description = description
72         entity.version = version
73         entity.derivedFrom = derivedFrom
74         entity.metadata = metadata
75         entity.properties = properties
76         entity.attributes = attributes
77     }
78 }
79
80 class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
81                       description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
82     private var nodeType = NodeType()
83     private var capabilities: MutableMap<String, CapabilityDefinition>? = null
84     private var requirements: MutableMap<String, RequirementDefinition>? = null
85     private var interfaces: MutableMap<String, InterfaceDefinition>? = null
86     private var artifacts: MutableMap<String, ArtifactDefinition>? = null
87
88     fun capability(id: String, type: String, description: String, block: CapabilityDefinitionBuilder.() -> Unit) {
89         if (capabilities == null)
90             capabilities = hashMapOf()
91         capabilities!![id] = CapabilityDefinitionBuilder(id, type, description).apply(block).build()
92     }
93
94     fun requirement(id: String, capability: String, node: String, relationship: String, description: String) {
95         if (requirements == null)
96             requirements = hashMapOf()
97         requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description).build()
98     }
99
100     fun requirement(id: String, capability: String, node: String, relationship: String, description: String,
101                     block: RequirementDefinitionBuilder.() -> Unit) {
102         if (requirements == null)
103             requirements = hashMapOf()
104         requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description)
105                 .apply(block).build()
106     }
107
108     fun artifact(id: String, type: String, file: String) {
109         if (artifacts == null)
110             artifacts = hashMapOf()
111         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
112     }
113
114     private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) {
115         if (interfaces == null)
116             interfaces = hashMapOf()
117         interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build()
118     }
119
120     fun operation(interfaceName: String, description: String?, block: OperationDefinitionBuilder.() -> Unit) {
121         if (interfaces == null)
122             interfaces = hashMapOf()
123
124         val interfaceDefinition = InterfaceDefinition()
125         val defaultOperationName = "process"
126         interfaceDefinition.operations = hashMapOf()
127         interfaceDefinition.operations!![defaultOperationName] =
128                 OperationDefinitionBuilder(defaultOperationName, description).apply(block).build()
129         interfaces!![interfaceName] = interfaceDefinition
130     }
131
132     fun build(): NodeType {
133         buildEntityType(nodeType)
134         nodeType.capabilities = capabilities
135         nodeType.requirements = requirements
136         nodeType.interfaces = interfaces
137         nodeType.artifacts = artifacts
138         return nodeType
139     }
140 }
141
142 class ArtifactTypeBuilder(id: String, version: String, derivedFrom: String,
143                           description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
144     private var artifactType = ArtifactType()
145     private var fileExt: MutableList<String>? = null
146
147     fun fileExt(vararg fileExts: String) {
148         if (fileExt == null)
149             fileExt = arrayListOf()
150         fileExts.forEach {
151             fileExt!!.add(it)
152         }
153     }
154
155     fun build(): ArtifactType {
156         buildEntityType(artifactType)
157         artifactType.fileExt = fileExt
158         return artifactType
159     }
160 }
161
162 class PolicyTypeBuilder(id: String, version: String, derivedFrom: String,
163                         description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
164     private var policyType = PolicyType()
165
166     fun targets(targetsStr: String) {
167         val arrayNode = targetsStr.jsonAsJsonType() as ArrayNode
168         targets(arrayNode.asListOfString())
169     }
170
171     fun targets(target: List<String>) {
172         policyType.targets = target.toMutableList()
173     }
174
175     fun build(): PolicyType {
176         buildEntityType(policyType)
177         return policyType
178     }
179 }
180
181 class RelationshipTypeBuilder(private val id: String, private val version: String,
182                               derivedFrom: String, private val description: String?)
183     : EntityTypeBuilder(id, version, derivedFrom, description) {
184
185     private var relationshipType = RelationshipType()
186
187     fun validTargetTypes(validTargetTypesStr: String) {
188         val arrayNode = validTargetTypesStr.jsonAsJsonType() as ArrayNode
189         validTargetTypes(arrayNode.asListOfString())
190     }
191
192     fun validTargetTypes(validTargetTypes: List<String>) {
193         relationshipType.validTargetTypes = validTargetTypes.toMutableList()
194     }
195
196     fun build(): RelationshipType {
197         buildEntityType(relationshipType)
198         relationshipType.id = id
199         relationshipType.version = version
200         relationshipType.description = description
201         return relationshipType
202     }
203 }
204
205 class DataTypeBuilder(id: String, version: String, derivedFrom: String,
206                       description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
207     private var dataType = DataType()
208
209     fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
210         if (dataType.constraints == null) {
211             dataType.constraints = mutableListOf()
212         }
213         val constraintClause = ConstraintClauseBuilder().apply(block).build()
214         dataType.constraints!!.add(constraintClause)
215     }
216
217     fun build(): DataType {
218         buildEntityType(dataType)
219         return dataType
220     }
221 }
222
223 class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") {
224
225     private var capabilityDefinition = CapabilityDefinition()
226     private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
227
228     fun property(id: String, type: String? = BluePrintConstants.DATA_TYPE_STRING, required: Boolean? = false, description: String? = "") {
229         val property = PropertyDefinitionBuilder(id, type, required, description).build()
230         properties[id] = property
231     }
232
233     fun build(): CapabilityDefinition {
234         capabilityDefinition.id = id
235         capabilityDefinition.description = description
236         capabilityDefinition.type = type
237         capabilityDefinition.properties = properties
238         return capabilityDefinition
239     }
240 }
241
242 class RequirementDefinitionBuilder(private val id: String, private val capability: String, private val node: String,
243                                    private val relationship: String, private val description: String? = "") {
244     private var requirementDefinition = RequirementDefinition()
245
246     fun build(): RequirementDefinition {
247         requirementDefinition.id = id
248         requirementDefinition.description = description
249         requirementDefinition.capability = capability
250         requirementDefinition.node = node
251         requirementDefinition.relationship = relationship
252         return requirementDefinition
253     }
254 }
255
256 class InterfaceDefinitionBuilder(private val id: String) {
257
258     private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
259     private var operations: MutableMap<String, OperationDefinition>? = null
260
261     fun operation(id: String, description: String? = "", block: OperationDefinitionBuilder.() -> Unit) {
262         if (operations == null)
263             operations = hashMapOf()
264         operations!![id] = OperationDefinitionBuilder(id, description).apply(block).build()
265     }
266
267     fun build(): InterfaceDefinition {
268         interfaceDefinition.id = id
269         interfaceDefinition.operations = operations
270         return interfaceDefinition
271     }
272 }
273
274 class OperationDefinitionBuilder(private val id: String,
275                                  private val description: String? = "") {
276     private var operationDefinition: OperationDefinition = OperationDefinition()
277
278     fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
279         operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
280     }
281
282     fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
283         operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
284     }
285
286     fun build(): OperationDefinition {
287         operationDefinition.id = id
288         operationDefinition.description = description
289         return operationDefinition
290     }
291 }
292
293 class AttributeDefinitionBuilder(private val id: String,
294                                  private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
295                                  private val required: Boolean? = false,
296                                  private val description: String? = "") {
297
298     private var attributeDefinition: AttributeDefinition = AttributeDefinition()
299
300     fun entrySchema(entrySchemaType: String) {
301         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
302     }
303
304     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
305         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
306     }
307
308     fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
309         if (attributeDefinition.constraints == null) {
310             attributeDefinition.constraints = mutableListOf()
311         }
312         val constraintClause = ConstraintClauseBuilder().apply(block).build()
313         attributeDefinition.constraints!!.add(constraintClause)
314     }
315
316     fun defaultValue(defaultValue: Any) {
317         defaultValue(defaultValue.asJsonType())
318     }
319
320     fun defaultValue(defaultValue: JsonNode) {
321         attributeDefinition.defaultValue = defaultValue
322     }
323
324     fun build(): AttributeDefinition {
325         attributeDefinition.id = id
326         attributeDefinition.type = type!!
327         attributeDefinition.required = required
328         attributeDefinition.description = description
329         return attributeDefinition
330     }
331 }
332
333 class PropertiesDefinitionBuilder {
334     private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
335
336     fun property(id: String, property: PropertyDefinition) {
337         properties[id] = property
338     }
339
340     fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
341         val property = PropertyDefinitionBuilder(id, type, required, description).build()
342         properties[id] = property
343     }
344
345     fun property(id: String, type: String?, required: Boolean?, description: String? = "",
346                  block: PropertyDefinitionBuilder.() -> Unit) {
347         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
348         properties[id] = property
349     }
350
351     fun build(): MutableMap<String, PropertyDefinition> {
352         return properties
353     }
354 }
355
356 class PropertyDefinitionBuilder(private val id: String,
357                                 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
358                                 private val required: Boolean? = false,
359                                 private val description: String? = "") {
360
361     private var propertyDefinition: PropertyDefinition = PropertyDefinition()
362
363     fun entrySchema(entrySchemaType: String) {
364         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
365     }
366
367     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
368         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
369     }
370
371     fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
372         if (propertyDefinition.constraints == null) {
373             propertyDefinition.constraints = mutableListOf()
374         }
375         val constraintClause = ConstraintClauseBuilder().apply(block).build()
376         propertyDefinition.constraints!!.add(constraintClause)
377     }
378
379     fun defaultValue(defaultValue: Any) {
380         defaultValue(defaultValue.asJsonType())
381     }
382
383     fun defaultValue(defaultValue: JsonNode) {
384         propertyDefinition.defaultValue = defaultValue
385     }
386
387     fun value(value: JsonNode) {
388         propertyDefinition.value = value
389     }
390
391     fun build(): PropertyDefinition {
392         propertyDefinition.id = id
393         propertyDefinition.type = type!!
394         propertyDefinition.required = required
395         propertyDefinition.description = description
396         return propertyDefinition
397     }
398 }
399
400 class ConstraintsClauseBuilder {
401     val constraints: MutableList<ConstraintClause> = mutableListOf()
402
403     fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
404         val constraintClause = ConstraintClauseBuilder().apply(block).build()
405         constraints.add(constraintClause)
406     }
407
408     fun build(): MutableList<ConstraintClause> {
409         return constraints
410     }
411 }
412
413 class ConstraintClauseBuilder {
414     private val constraintClause = ConstraintClause()
415
416     fun equal(equal: Any) = equal(equal.asJsonType())
417
418     fun equal(equal: JsonNode) {
419         constraintClause.equal = equal
420     }
421
422     fun greaterOrEqual(greaterOrEqual: Any) {
423         constraintClause.greaterOrEqual = greaterOrEqual.asJsonPrimitive()
424     }
425
426     fun greaterThan(greaterThan: Any) {
427         constraintClause.greaterThan = greaterThan.asJsonPrimitive()
428     }
429
430     fun lessOrEqual(lessOrEqual: Any) {
431         constraintClause.lessOrEqual = lessOrEqual.asJsonPrimitive()
432     }
433
434     fun lessThan(lessThan: Any) {
435         constraintClause.lessThan = lessThan.asJsonPrimitive()
436     }
437
438     fun inRange(inRangeStr: String) = inRange(inRangeStr.jsonAsJsonType() as ArrayNode)
439
440     fun inRange(inRangeNode: ArrayNode) {
441         constraintClause.inRange = inRangeNode.toMutableList()
442     }
443
444     fun validValues(validValuesStr: String) = validValues(validValuesStr.jsonAsJsonType() as ArrayNode)
445
446     fun validValues(validValuesNode: ArrayNode) = validValues(validValuesNode.toMutableList())
447
448     fun validValues(validValues: List<JsonNode>) {
449         constraintClause.validValues = validValues.toMutableList()
450     }
451
452     fun length(length: Any) {
453         constraintClause.length = length.asJsonPrimitive()
454     }
455
456     fun minLength(minLength: Any) {
457         constraintClause.minLength = minLength.asJsonPrimitive()
458     }
459
460     fun maxLength(maxLength: Any) {
461         constraintClause.maxLength = maxLength.asJsonPrimitive()
462     }
463
464     fun pattern(pattern: String) {
465         constraintClause.pattern = pattern
466     }
467
468     fun schema(schema: String) {
469         constraintClause.schema = schema
470     }
471
472     fun build(): ConstraintClause {
473         return constraintClause
474     }
475 }
476
477
478 class EntrySchemaBuilder(private val type: String) {
479     private var entrySchema: EntrySchema = EntrySchema()
480
481     fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
482         if (entrySchema.constraints == null) {
483             entrySchema.constraints = mutableListOf()
484         }
485         val constraintClause = ConstraintClauseBuilder().apply(block).build()
486         entrySchema.constraints!!.add(constraintClause)
487     }
488
489     fun build(): EntrySchema {
490         entrySchema.type = type
491         return entrySchema
492     }
493 }