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 org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
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 build(): PolicyType {
167 buildEntityType(policyType)
172 class RelationshipTypeBuilder(private val id: String, private val version: String,
173 derivedFrom: String, private val description: String?)
174 : EntityTypeBuilder(id, version, derivedFrom, description) {
176 private var relationshipType = RelationshipType()
178 fun build(): RelationshipType {
179 buildEntityType(relationshipType)
180 relationshipType.id = id
181 relationshipType.version = version
182 relationshipType.description = description
183 return relationshipType
187 class DataTypeBuilder(id: String, version: String, derivedFrom: String,
188 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
189 private var dataType = DataType()
191 fun build(): DataType {
192 buildEntityType(dataType)
197 class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") {
199 private var capabilityDefinition = CapabilityDefinition()
200 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
202 fun property(id: String, type: String? = BluePrintConstants.DATA_TYPE_STRING, required: Boolean? = false, description: String? = "") {
203 val property = PropertyDefinitionBuilder(id, type, required, description).build()
204 properties[id] = property
207 fun build(): CapabilityDefinition {
208 capabilityDefinition.id = id
209 capabilityDefinition.description = description
210 capabilityDefinition.type = type
211 capabilityDefinition.properties = properties
212 return capabilityDefinition
216 class RequirementDefinitionBuilder(private val id: String, private val capability: String, private val node: String,
217 private val relationship: String, private val description: String? = "") {
218 private var requirementDefinition = RequirementDefinition()
220 fun build(): RequirementDefinition {
221 requirementDefinition.id = id
222 requirementDefinition.description = description
223 requirementDefinition.capability = capability
224 requirementDefinition.node = node
225 requirementDefinition.relationship = relationship
226 return requirementDefinition
230 class InterfaceDefinitionBuilder(private val id: String) {
232 private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
233 private var operations: MutableMap<String, OperationDefinition>? = null
235 fun operation(id: String, description: String? = "", block: OperationDefinitionBuilder.() -> Unit) {
236 if (operations == null)
237 operations = hashMapOf()
238 operations!![id] = OperationDefinitionBuilder(id, description).apply(block).build()
241 fun build(): InterfaceDefinition {
242 interfaceDefinition.id = id
243 interfaceDefinition.operations = operations
244 return interfaceDefinition
248 class OperationDefinitionBuilder(private val id: String,
249 private val description: String? = "") {
250 private var operationDefinition: OperationDefinition = OperationDefinition()
252 fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
253 operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
256 fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
257 operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
260 fun build(): OperationDefinition {
261 operationDefinition.id = id
262 operationDefinition.description = description
263 return operationDefinition
267 class AttributesDefinitionBuilder {
268 private val attributes: MutableMap<String, AttributeDefinition> = hashMapOf()
270 fun attribute(id: String, attribute: AttributeDefinition) {
271 attributes[id] = attribute
274 fun attribute(id: String, type: String?, required: Boolean?, description: String?) {
275 val attribute = AttributeDefinitionBuilder(id, type, required, description).build()
276 attributes[id] = attribute
279 fun attribute(id: String, type: String?, required: Boolean?, description: String?,
280 block: AttributeDefinitionBuilder.() -> Unit) {
281 val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
282 attributes[id] = attribute
285 fun build(): MutableMap<String, AttributeDefinition> {
290 class AttributeDefinitionBuilder(private val id: String,
291 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
292 private val required: Boolean? = false,
293 private val description: String? = "") {
295 private var attributeDefinition: AttributeDefinition = AttributeDefinition()
297 fun entrySchema(entrySchemaType: String) {
298 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
301 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
302 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
305 // TODO("Constrains")
307 fun defaultValue(defaultValue: JsonNode) {
308 attributeDefinition.defaultValue = defaultValue
311 fun build(): AttributeDefinition {
312 attributeDefinition.id = id
313 attributeDefinition.type = type!!
314 attributeDefinition.required = required
315 attributeDefinition.description = description
316 return attributeDefinition
320 class PropertiesDefinitionBuilder {
321 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
323 fun property(id: String, property: PropertyDefinition) {
324 properties[id] = property
327 fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
328 val property = PropertyDefinitionBuilder(id, type, required, description).build()
329 properties[id] = property
332 fun property(id: String, type: String?, required: Boolean?, description: String? = "",
333 block: PropertyDefinitionBuilder.() -> Unit) {
334 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
335 properties[id] = property
338 fun build(): MutableMap<String, PropertyDefinition> {
343 class PropertyDefinitionBuilder(private val id: String,
344 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
345 private val required: Boolean? = false,
346 private val description: String? = "") {
348 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
350 fun entrySchema(entrySchemaType: String) {
351 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
354 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
355 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
358 fun constrains(block: ConstraintClauseBuilder.() -> Unit) {
359 propertyDefinition.constraints = ConstraintClauseBuilder().apply(block).build()
362 fun defaultValue(defaultValue: Any) {
363 defaultValue(defaultValue.asJsonType())
366 fun defaultValue(defaultValue: JsonNode) {
367 propertyDefinition.defaultValue = defaultValue
370 fun value(value: JsonNode) {
371 propertyDefinition.value = value
374 fun build(): PropertyDefinition {
375 propertyDefinition.id = id
376 propertyDefinition.type = type!!
377 propertyDefinition.required = required
378 propertyDefinition.description = description
379 return propertyDefinition
383 class ConstraintClauseBuilder {
384 private val constraints: MutableList<ConstraintClause> = mutableListOf()
385 //TODO("Implementation")
387 fun validValues(values: List<JsonNode>) {
388 val constraintClause = ConstraintClause()
389 constraintClause.validValues = values.toMutableList()
390 constraints.add(constraintClause)
393 fun build(): MutableList<ConstraintClause> {
399 class EntrySchemaBuilder(private val type: String) {
400 private var entrySchema: EntrySchema = EntrySchema()
402 fun build(): EntrySchema {
403 entrySchema.type = type