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 {
406 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
408 fun property(id: String, property: PropertyDefinition) {
409 properties[id] = property
412 fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
413 val property = PropertyDefinitionBuilder(id, type, required, description).build()
414 properties[id] = property
421 description: String? = "",
422 block: PropertyDefinitionBuilder.() -> Unit
424 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
425 properties[id] = property
428 fun build(): MutableMap<String, PropertyDefinition> {
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? = ""
440 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
442 fun entrySchema(entrySchemaType: String) {
443 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
446 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
447 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
450 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
451 if (propertyDefinition.constraints == null) {
452 propertyDefinition.constraints = mutableListOf()
454 val constraintClause = ConstraintClauseBuilder().apply(block).build()
455 propertyDefinition.constraints!!.add(constraintClause)
458 fun defaultValue(defaultValue: Any) {
459 defaultValue(defaultValue.asJsonType())
462 fun defaultValue(defaultValue: JsonNode) {
463 propertyDefinition.defaultValue = defaultValue
466 fun metadata(name: String, value: String) {
467 if (propertyDefinition.metadata == null) {
468 propertyDefinition.metadata = hashMapOf()
470 propertyDefinition.metadata!![name] = value
473 fun value(value: Any) {
474 value(value.asJsonType())
477 fun value(value: JsonNode) {
478 propertyDefinition.value = value
481 fun build(): PropertyDefinition {
482 propertyDefinition.id = id
483 propertyDefinition.type = type!!
484 propertyDefinition.required = required
485 propertyDefinition.description = description
486 return propertyDefinition
490 class ConstraintsClauseBuilder {
492 val constraints: MutableList<ConstraintClause> = mutableListOf()
494 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
495 val constraintClause = ConstraintClauseBuilder().apply(block).build()
496 constraints.add(constraintClause)
499 fun build(): MutableList<ConstraintClause> {
504 class ConstraintClauseBuilder {
506 private val constraintClause = ConstraintClause()
508 fun equal(equal: Any) = equal(equal.asJsonType())
510 fun equal(equal: JsonNode) {
511 constraintClause.equal = equal
514 fun greaterOrEqual(greaterOrEqual: Any) {
515 constraintClause.greaterOrEqual = greaterOrEqual.asJsonPrimitive()
518 fun greaterThan(greaterThan: Any) {
519 constraintClause.greaterThan = greaterThan.asJsonPrimitive()
522 fun lessOrEqual(lessOrEqual: Any) {
523 constraintClause.lessOrEqual = lessOrEqual.asJsonPrimitive()
526 fun lessThan(lessThan: Any) {
527 constraintClause.lessThan = lessThan.asJsonPrimitive()
530 fun inRange(inRangeStr: String) = inRange(inRangeStr.jsonAsJsonType() as ArrayNode)
532 fun inRange(inRangeNode: ArrayNode) {
533 constraintClause.inRange = inRangeNode.toMutableList()
536 fun validValues(validValuesStr: String) = validValues(validValuesStr.jsonAsJsonType() as ArrayNode)
538 fun validValues(validValuesNode: ArrayNode) = validValues(validValuesNode.toMutableList())
540 fun validValues(validValues: List<JsonNode>) {
541 constraintClause.validValues = validValues.toMutableList()
544 fun length(length: Any) {
545 constraintClause.length = length.asJsonPrimitive()
548 fun minLength(minLength: Any) {
549 constraintClause.minLength = minLength.asJsonPrimitive()
552 fun maxLength(maxLength: Any) {
553 constraintClause.maxLength = maxLength.asJsonPrimitive()
556 fun pattern(pattern: String) {
557 constraintClause.pattern = pattern
560 fun schema(schema: String) {
561 constraintClause.schema = schema
564 fun build(): ConstraintClause {
565 return constraintClause
569 class EntrySchemaBuilder(private val type: String) {
571 private var entrySchema: EntrySchema = EntrySchema()
573 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
574 if (entrySchema.constraints == null) {
575 entrySchema.constraints = mutableListOf()
577 val constraintClause = ConstraintClauseBuilder().apply(block).build()
578 entrySchema.constraints!!.add(constraintClause)
581 fun build(): EntrySchema {
582 entrySchema.type = type