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