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.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
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
33 fun metadata(key: String, value: String) {
35 metadata = hashMapOf()
36 metadata!![key] = value
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
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
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
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
69 fun buildEntityType(entity: EntityType) {
71 entity.description = description
72 entity.version = version
73 entity.derivedFrom = derivedFrom
74 entity.metadata = metadata
75 entity.properties = properties
76 entity.attributes = attributes
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
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()
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()
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()
108 fun artifact(id: String, type: String, file: String) {
109 if (artifacts == null)
110 artifacts = hashMapOf()
111 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
114 private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) {
115 if (interfaces == null)
116 interfaces = hashMapOf()
117 interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build()
120 fun operation(interfaceName: String, description: String?, block: OperationDefinitionBuilder.() -> Unit) {
121 if (interfaces == null)
122 interfaces = hashMapOf()
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
132 fun build(): NodeType {
133 buildEntityType(nodeType)
134 nodeType.capabilities = capabilities
135 nodeType.requirements = requirements
136 nodeType.interfaces = interfaces
137 nodeType.artifacts = artifacts
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
147 fun fileExt(vararg fileExts: String) {
149 fileExt = arrayListOf()
155 fun build(): ArtifactType {
156 buildEntityType(artifactType)
157 artifactType.fileExt = fileExt
162 class PolicyTypeBuilder(id: String, version: String, derivedFrom: String,
163 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
164 private var policyType = PolicyType()
166 fun targets(targetsStr: String) {
167 val arrayNode = targetsStr.jsonAsJsonType() as ArrayNode
168 targets(arrayNode.asListOfString())
171 fun targets(target: List<String>) {
172 policyType.targets = target.toMutableList()
175 fun build(): PolicyType {
176 buildEntityType(policyType)
181 class RelationshipTypeBuilder(private val id: String, private val version: String,
182 derivedFrom: String, private val description: String?)
183 : EntityTypeBuilder(id, version, derivedFrom, description) {
185 private var relationshipType = RelationshipType()
187 fun validTargetTypes(validTargetTypesStr: String) {
188 val arrayNode = validTargetTypesStr.jsonAsJsonType() as ArrayNode
189 validTargetTypes(arrayNode.asListOfString())
192 fun validTargetTypes(validTargetTypes: List<String>) {
193 relationshipType.validTargetTypes = validTargetTypes.toMutableList()
196 fun build(): RelationshipType {
197 buildEntityType(relationshipType)
198 relationshipType.id = id
199 relationshipType.version = version
200 relationshipType.description = description
201 return relationshipType
205 class DataTypeBuilder(id: String, version: String, derivedFrom: String,
206 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
207 private var dataType = DataType()
209 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
210 if (dataType.constraints == null) {
211 dataType.constraints = mutableListOf()
213 val constraintClause = ConstraintClauseBuilder().apply(block).build()
214 dataType.constraints!!.add(constraintClause)
217 fun build(): DataType {
218 buildEntityType(dataType)
223 class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") {
225 private var capabilityDefinition = CapabilityDefinition()
226 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
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
233 fun build(): CapabilityDefinition {
234 capabilityDefinition.id = id
235 capabilityDefinition.description = description
236 capabilityDefinition.type = type
237 capabilityDefinition.properties = properties
238 return capabilityDefinition
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()
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
256 class InterfaceDefinitionBuilder(private val id: String) {
258 private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
259 private var operations: MutableMap<String, OperationDefinition>? = null
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()
267 fun build(): InterfaceDefinition {
268 interfaceDefinition.id = id
269 interfaceDefinition.operations = operations
270 return interfaceDefinition
274 class OperationDefinitionBuilder(private val id: String,
275 private val description: String? = "") {
276 private var operationDefinition: OperationDefinition = OperationDefinition()
278 fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
279 operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
282 fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
283 operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
286 fun build(): OperationDefinition {
287 operationDefinition.id = id
288 operationDefinition.description = description
289 return operationDefinition
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? = "") {
298 private var attributeDefinition: AttributeDefinition = AttributeDefinition()
300 fun entrySchema(entrySchemaType: String) {
301 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
304 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
305 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
308 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
309 if (attributeDefinition.constraints == null) {
310 attributeDefinition.constraints = mutableListOf()
312 val constraintClause = ConstraintClauseBuilder().apply(block).build()
313 attributeDefinition.constraints!!.add(constraintClause)
316 fun defaultValue(defaultValue: Any) {
317 defaultValue(defaultValue.asJsonType())
320 fun defaultValue(defaultValue: JsonNode) {
321 attributeDefinition.defaultValue = defaultValue
324 fun build(): AttributeDefinition {
325 attributeDefinition.id = id
326 attributeDefinition.type = type!!
327 attributeDefinition.required = required
328 attributeDefinition.description = description
329 return attributeDefinition
333 class PropertiesDefinitionBuilder {
334 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
336 fun property(id: String, property: PropertyDefinition) {
337 properties[id] = property
340 fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
341 val property = PropertyDefinitionBuilder(id, type, required, description).build()
342 properties[id] = property
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
351 fun build(): MutableMap<String, PropertyDefinition> {
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? = "") {
361 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
363 fun entrySchema(entrySchemaType: String) {
364 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
367 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
368 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
371 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
372 if (propertyDefinition.constraints == null) {
373 propertyDefinition.constraints = mutableListOf()
375 val constraintClause = ConstraintClauseBuilder().apply(block).build()
376 propertyDefinition.constraints!!.add(constraintClause)
379 fun defaultValue(defaultValue: Any) {
380 defaultValue(defaultValue.asJsonType())
383 fun defaultValue(defaultValue: JsonNode) {
384 propertyDefinition.defaultValue = defaultValue
387 fun value(value: JsonNode) {
388 propertyDefinition.value = value
391 fun build(): PropertyDefinition {
392 propertyDefinition.id = id
393 propertyDefinition.type = type!!
394 propertyDefinition.required = required
395 propertyDefinition.description = description
396 return propertyDefinition
400 class ConstraintsClauseBuilder {
401 val constraints: MutableList<ConstraintClause> = mutableListOf()
403 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
404 val constraintClause = ConstraintClauseBuilder().apply(block).build()
405 constraints.add(constraintClause)
408 fun build(): MutableList<ConstraintClause> {
413 class ConstraintClauseBuilder {
414 private val constraintClause = ConstraintClause()
416 fun equal(equal: Any) = equal(equal.asJsonType())
418 fun equal(equal: JsonNode) {
419 constraintClause.equal = equal
422 fun greaterOrEqual(greaterOrEqual: Any) {
423 constraintClause.greaterOrEqual = greaterOrEqual.asJsonPrimitive()
426 fun greaterThan(greaterThan: Any) {
427 constraintClause.greaterThan = greaterThan.asJsonPrimitive()
430 fun lessOrEqual(lessOrEqual: Any) {
431 constraintClause.lessOrEqual = lessOrEqual.asJsonPrimitive()
434 fun lessThan(lessThan: Any) {
435 constraintClause.lessThan = lessThan.asJsonPrimitive()
438 fun inRange(inRangeStr: String) = inRange(inRangeStr.jsonAsJsonType() as ArrayNode)
440 fun inRange(inRangeNode: ArrayNode) {
441 constraintClause.inRange = inRangeNode.toMutableList()
444 fun validValues(validValuesStr: String) = validValues(validValuesStr.jsonAsJsonType() as ArrayNode)
446 fun validValues(validValuesNode: ArrayNode) = validValues(validValuesNode.toMutableList())
448 fun validValues(validValues: List<JsonNode>) {
449 constraintClause.validValues = validValues.toMutableList()
452 fun length(length: Any) {
453 constraintClause.length = length.asJsonPrimitive()
456 fun minLength(minLength: Any) {
457 constraintClause.minLength = minLength.asJsonPrimitive()
460 fun maxLength(maxLength: Any) {
461 constraintClause.maxLength = maxLength.asJsonPrimitive()
464 fun pattern(pattern: String) {
465 constraintClause.pattern = pattern
468 fun schema(schema: String) {
469 constraintClause.schema = schema
472 fun build(): ConstraintClause {
473 return constraintClause
478 class EntrySchemaBuilder(private val type: String) {
479 private var entrySchema: EntrySchema = EntrySchema()
481 fun constrain(block: ConstraintClauseBuilder.() -> Unit) {
482 if (entrySchema.constraints == null) {
483 entrySchema.constraints = mutableListOf()
485 val constraintClause = ConstraintClauseBuilder().apply(block).build()
486 entrySchema.constraints!!.add(constraintClause)
489 fun build(): EntrySchema {
490 entrySchema.type = type