2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
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
42 open class EntityTypeBuilder(
43 private val id: String,
44 private val version: String,
45 private val derivedFrom: String,
46 private val description: String? = ""
49 var metadata: MutableMap<String, String>? = null
50 var properties: MutableMap<String, PropertyDefinition>? = null
51 var attributes: MutableMap<String, AttributeDefinition>? = null
53 fun metadata(key: String, value: String) {
55 metadata = hashMapOf()
56 metadata!![key] = value
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
70 description: String? = "",
71 block: AttributeDefinitionBuilder.() -> Unit
73 if (attributes == null)
74 attributes = hashMapOf()
75 val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
76 attributes!![id] = attribute
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
90 description: String? = "",
91 block: PropertyDefinitionBuilder.() -> Unit
93 if (properties == null)
94 properties = hashMapOf()
95 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
96 properties!![id] = property
99 fun buildEntityType(entity: EntityType) {
101 entity.description = description
102 entity.version = version
103 entity.derivedFrom = derivedFrom
104 entity.metadata = metadata
105 entity.properties = properties
106 entity.attributes = attributes
110 class NodeTypeBuilder(
115 ) : EntityTypeBuilder(id, version, derivedFrom, description) {
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
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()
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()
139 relationship: String,
141 block: RequirementDefinitionBuilder.() -> Unit
143 if (requirements == null)
144 requirements = hashMapOf()
145 requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description)
146 .apply(block).build()
149 fun artifact(id: String, type: String, file: String) {
150 if (artifacts == null)
151 artifacts = hashMapOf()
152 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
155 private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) {
156 if (interfaces == null)
157 interfaces = hashMapOf()
158 interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build()
161 fun operation(interfaceName: String, description: String?, block: OperationDefinitionBuilder.() -> Unit) {
162 if (interfaces == null)
163 interfaces = hashMapOf()
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
173 fun build(): NodeType {
174 buildEntityType(nodeType)
175 nodeType.capabilities = capabilities
176 nodeType.requirements = requirements
177 nodeType.interfaces = interfaces
178 nodeType.artifacts = artifacts
183 class ArtifactTypeBuilder(
188 ) : EntityTypeBuilder(id, version, derivedFrom, description) {
190 private var artifactType = ArtifactType()
191 private var fileExt: MutableList<String>? = null
193 fun fileExt(vararg fileExts: String) {
195 fileExt = arrayListOf()
201 fun build(): ArtifactType {
202 buildEntityType(artifactType)
203 artifactType.fileExt = fileExt
208 class PolicyTypeBuilder(
213 ) : EntityTypeBuilder(id, version, derivedFrom, description) {
215 private var policyType = PolicyType()
217 fun targets(targetsStr: String) {
218 val arrayNode = targetsStr.jsonAsJsonType() as ArrayNode
219 targets(arrayNode.asListOfString())
222 fun targets(target: List<String>) {
223 policyType.targets = target.toMutableList()
226 fun build(): PolicyType {
227 buildEntityType(policyType)
232 class RelationshipTypeBuilder(
233 private val id: String,
234 private val version: String,
236 private val description: String?
238 EntityTypeBuilder(id, version, derivedFrom, description) {
240 private var relationshipType = RelationshipType()
242 fun validTargetTypes(validTargetTypesStr: String) {
243 val arrayNode = validTargetTypesStr.jsonAsJsonType() as ArrayNode
244 validTargetTypes(arrayNode.asListOfString())
247 fun validTargetTypes(validTargetTypes: List<String>) {
248 relationshipType.validTargetTypes = validTargetTypes.toMutableList()
251 fun build(): RelationshipType {
252 buildEntityType(relationshipType)
253 relationshipType.id = id
254 relationshipType.version = version
255 relationshipType.description = description
256 return relationshipType
260 class DataTypeBuilder(
265 ) : EntityTypeBuilder(id, version, derivedFrom, description) {
267 private var dataType = DataType()
269 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
270 if (dataType.constraints == null) {
271 dataType.constraints = mutableListOf()
273 val constraintClause = ConstraintClauseBuilder().apply(block).build()
274 dataType.constraints!!.add(constraintClause)
277 fun build(): DataType {
278 buildEntityType(dataType)
283 class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") {
285 private var capabilityDefinition = CapabilityDefinition()
286 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
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
293 fun build(): CapabilityDefinition {
294 capabilityDefinition.id = id
295 capabilityDefinition.description = description
296 capabilityDefinition.type = type
297 capabilityDefinition.properties = properties
298 return capabilityDefinition
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? = ""
310 private var requirementDefinition = RequirementDefinition()
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
322 class InterfaceDefinitionBuilder(private val id: String) {
324 private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
325 private var operations: MutableMap<String, OperationDefinition>? = null
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()
333 fun build(): InterfaceDefinition {
334 interfaceDefinition.id = id
335 interfaceDefinition.operations = operations
336 return interfaceDefinition
340 class OperationDefinitionBuilder(
341 private val id: String,
342 private val description: String? = ""
345 private var operationDefinition: OperationDefinition = OperationDefinition()
347 fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
348 operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
351 fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
352 operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
355 fun build(): OperationDefinition {
356 operationDefinition.id = id
357 operationDefinition.description = description
358 return operationDefinition
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? = ""
369 private var attributeDefinition: AttributeDefinition = AttributeDefinition()
371 fun entrySchema(entrySchemaType: String) {
372 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
375 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
376 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
379 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
380 if (attributeDefinition.constraints == null) {
381 attributeDefinition.constraints = mutableListOf()
383 val constraintClause = ConstraintClauseBuilder().apply(block).build()
384 attributeDefinition.constraints!!.add(constraintClause)
387 fun defaultValue(defaultValue: Any) {
388 defaultValue(defaultValue.asJsonType())
391 fun defaultValue(defaultValue: JsonNode) {
392 attributeDefinition.defaultValue = defaultValue
395 fun build(): AttributeDefinition {
396 attributeDefinition.id = id
397 attributeDefinition.type = type!!
398 attributeDefinition.required = required
399 attributeDefinition.description = description
400 return attributeDefinition
404 class PropertiesDefinitionBuilder {
405 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
407 fun property(id: String, property: PropertyDefinition) {
408 properties[id] = property
411 fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
412 val property = PropertyDefinitionBuilder(id, type, required, description).build()
413 properties[id] = property
420 description: String? = "",
421 block: PropertyDefinitionBuilder.() -> Unit
423 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
424 properties[id] = property
427 fun build(): MutableMap<String, PropertyDefinition> {
432 class PropertyDefinitionBuilder(
433 private val id: String,
434 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
435 private val required: Boolean? = false,
436 private val description: String? = ""
439 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
441 fun entrySchema(entrySchemaType: String) {
442 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
445 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
446 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
449 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
450 if (propertyDefinition.constraints == null) {
451 propertyDefinition.constraints = mutableListOf()
453 val constraintClause = ConstraintClauseBuilder().apply(block).build()
454 propertyDefinition.constraints!!.add(constraintClause)
457 fun defaultValue(defaultValue: Any) {
458 defaultValue(defaultValue.asJsonType())
461 fun defaultValue(defaultValue: JsonNode) {
462 propertyDefinition.defaultValue = defaultValue
465 fun metadata(name: String, value: String) {
466 if (propertyDefinition.metadata == null) {
467 propertyDefinition.metadata = hashMapOf()
469 propertyDefinition.metadata!![name] = value
472 fun value(value: Any) {
473 value(value.asJsonType())
476 fun value(value: JsonNode) {
477 propertyDefinition.value = value
480 fun build(): PropertyDefinition {
481 propertyDefinition.id = id
482 propertyDefinition.type = type!!
483 propertyDefinition.required = required
484 propertyDefinition.description = description
485 return propertyDefinition
489 class ConstraintsClauseBuilder {
490 val constraints: MutableList<ConstraintClause> = mutableListOf()
492 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
493 val constraintClause = ConstraintClauseBuilder().apply(block).build()
494 constraints.add(constraintClause)
497 fun build(): MutableList<ConstraintClause> {
502 class ConstraintClauseBuilder {
503 private val constraintClause = ConstraintClause()
505 fun equal(equal: Any) = equal(equal.asJsonType())
507 fun equal(equal: JsonNode) {
508 constraintClause.equal = equal
511 fun greaterOrEqual(greaterOrEqual: Any) {
512 constraintClause.greaterOrEqual = greaterOrEqual.asJsonPrimitive()
515 fun greaterThan(greaterThan: Any) {
516 constraintClause.greaterThan = greaterThan.asJsonPrimitive()
519 fun lessOrEqual(lessOrEqual: Any) {
520 constraintClause.lessOrEqual = lessOrEqual.asJsonPrimitive()
523 fun lessThan(lessThan: Any) {
524 constraintClause.lessThan = lessThan.asJsonPrimitive()
527 fun inRange(inRangeStr: String) = inRange(inRangeStr.jsonAsJsonType() as ArrayNode)
529 fun inRange(inRangeNode: ArrayNode) {
530 constraintClause.inRange = inRangeNode.toMutableList()
533 fun validValues(validValuesStr: String) = validValues(validValuesStr.jsonAsJsonType() as ArrayNode)
535 fun validValues(validValuesNode: ArrayNode) = validValues(validValuesNode.toMutableList())
537 fun validValues(validValues: List<JsonNode>) {
538 constraintClause.validValues = validValues.toMutableList()
541 fun length(length: Any) {
542 constraintClause.length = length.asJsonPrimitive()
545 fun minLength(minLength: Any) {
546 constraintClause.minLength = minLength.asJsonPrimitive()
549 fun maxLength(maxLength: Any) {
550 constraintClause.maxLength = maxLength.asJsonPrimitive()
553 fun pattern(pattern: String) {
554 constraintClause.pattern = pattern
557 fun schema(schema: String) {
558 constraintClause.schema = schema
561 fun build(): ConstraintClause {
562 return constraintClause
566 class EntrySchemaBuilder(private val type: String) {
567 private var entrySchema: EntrySchema = EntrySchema()
569 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
570 if (entrySchema.constraints == null) {
571 entrySchema.constraints = mutableListOf()
573 val constraintClause = ConstraintClauseBuilder().apply(block).build()
574 entrySchema.constraints!!.add(constraintClause)
577 fun build(): EntrySchema {
578 entrySchema.type = type