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.data.*
24 open class EntityTypeBuilder(private val id: String,
25 private val version: String,
26 private val derivedFrom: String,
27 private val description: String? = "") {
28 var metadata: MutableMap<String, String>? = null
29 var properties: MutableMap<String, PropertyDefinition>? = null
30 var attributes: MutableMap<String, AttributeDefinition>? = null
32 fun metadata(key: String, value: String) {
34 metadata = hashMapOf()
35 metadata!![key] = value
38 fun attribute(id: String, type: String, required: Boolean, description: String? = "") {
39 if (attributes == null)
40 attributes = hashMapOf()
41 val attribute = AttributeDefinitionBuilder(id, type, required, description).build()
42 attributes!![id] = attribute
45 fun attribute(id: String, type: String, required: Boolean, description: String? = "",
46 block: AttributeDefinitionBuilder.() -> Unit) {
47 if (attributes == null)
48 attributes = hashMapOf()
49 val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
50 attributes!![id] = attribute
53 fun property(id: String, type: String, required: Boolean, description: String? = "") {
54 if (properties == null)
55 properties = hashMapOf()
56 val property = PropertyDefinitionBuilder(id, type, required, description).build()
57 properties!![id] = property
60 fun property(id: String, type: String, required: Boolean, description: String? = "",
61 block: PropertyDefinitionBuilder.() -> Unit) {
62 if (properties == null)
63 properties = hashMapOf()
64 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
65 properties!![id] = property
68 fun buildEntityType(entity: EntityType) {
70 entity.description = description
71 entity.version = version
72 entity.derivedFrom = derivedFrom
73 entity.metadata = metadata
74 entity.properties = properties
75 entity.attributes = attributes
79 class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
80 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
81 private var nodeType = NodeType()
82 private var capabilities: MutableMap<String, CapabilityDefinition>? = null
83 private var requirements: MutableMap<String, RequirementDefinition>? = null
84 private var interfaces: MutableMap<String, InterfaceDefinition>? = null
85 private var artifacts: MutableMap<String, ArtifactDefinition>? = null
87 fun capability(id: String, type: String, description: String, block: CapabilityDefinitionBuilder.() -> Unit) {
88 if (capabilities == null)
89 capabilities = hashMapOf()
90 capabilities!![id] = CapabilityDefinitionBuilder(id, type, description).apply(block).build()
93 fun requirement(id: String, capability: String, node: String, relationship: String, description: String) {
94 if (requirements == null)
95 requirements = hashMapOf()
96 requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description).build()
99 fun requirement(id: String, capability: String, node: String, relationship: String, description: String,
100 block: RequirementDefinitionBuilder.() -> Unit) {
101 if (requirements == null)
102 requirements = hashMapOf()
103 requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description)
104 .apply(block).build()
107 fun artifact(id: String, type: String, file: String) {
108 if (artifacts == null)
109 artifacts = hashMapOf()
110 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
113 private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) {
114 if (interfaces == null)
115 interfaces = hashMapOf()
116 interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build()
119 fun operation(interfaceName: String, description: String?, block: OperationDefinitionBuilder.() -> Unit) {
120 if (interfaces == null)
121 interfaces = hashMapOf()
123 val interfaceDefinition = InterfaceDefinition()
124 val defaultOperationName = "process"
125 interfaceDefinition.operations = hashMapOf()
126 interfaceDefinition.operations!![defaultOperationName] =
127 OperationDefinitionBuilder(defaultOperationName, description).apply(block).build()
128 interfaces!![interfaceName] = interfaceDefinition
131 fun build(): NodeType {
132 buildEntityType(nodeType)
133 nodeType.capabilities = capabilities
134 nodeType.requirements = requirements
135 nodeType.interfaces = interfaces
136 nodeType.artifacts = artifacts
141 class ArtifactTypeBuilder(id: String, version: String, derivedFrom: String,
142 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
143 private var artifactType = ArtifactType()
144 private var fileExt: MutableList<String>? = null
146 fun fileExt(vararg fileExts: String) {
148 fileExt = arrayListOf()
154 fun build(): ArtifactType {
155 buildEntityType(artifactType)
156 artifactType.fileExt = fileExt
161 class PolicyTypeBuilder(id: String, version: String, derivedFrom: String,
162 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
163 private var policyType = PolicyType()
165 fun build(): PolicyType {
166 buildEntityType(policyType)
171 class RelationshipTypeBuilder(private val id: String, private val version: String,
172 derivedFrom: String, private val description: String?)
173 : EntityTypeBuilder(id, version, derivedFrom, description) {
175 private var relationshipType = RelationshipType()
177 fun build(): RelationshipType {
178 buildEntityType(relationshipType)
179 relationshipType.id = id
180 relationshipType.version = version
181 relationshipType.description = description
182 return relationshipType
186 class DataTypeBuilder(id: String, version: String, derivedFrom: String,
187 description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
188 private var dataType = DataType()
190 fun build(): DataType {
191 buildEntityType(dataType)
196 class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") {
198 private var capabilityDefinition = CapabilityDefinition()
199 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
201 fun property(id: String, type: String? = BluePrintConstants.DATA_TYPE_STRING, required: Boolean? = false, description: String? = "") {
202 val property = PropertyDefinitionBuilder(id, type, required, description).build()
203 properties[id] = property
206 fun build(): CapabilityDefinition {
207 capabilityDefinition.id = id
208 capabilityDefinition.description = description
209 capabilityDefinition.type = type
210 capabilityDefinition.properties = properties
211 return capabilityDefinition
215 class RequirementDefinitionBuilder(private val id: String, private val capability: String, private val node: String,
216 private val relationship: String, private val description: String? = "") {
217 private var requirementDefinition = RequirementDefinition()
219 fun build(): RequirementDefinition {
220 requirementDefinition.id = id
221 requirementDefinition.description = description
222 requirementDefinition.capability = capability
223 requirementDefinition.node = node
224 requirementDefinition.relationship = relationship
225 return requirementDefinition
229 class InterfaceDefinitionBuilder(private val id: String) {
231 private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
232 private var operations: MutableMap<String, OperationDefinition>? = null
234 fun operation(id: String, description: String? = "", block: OperationDefinitionBuilder.() -> Unit) {
235 if (operations == null)
236 operations = hashMapOf()
237 operations!![id] = OperationDefinitionBuilder(id, description).apply(block).build()
240 fun build(): InterfaceDefinition {
241 interfaceDefinition.id = id
242 interfaceDefinition.operations = operations
243 return interfaceDefinition
247 class OperationDefinitionBuilder(private val id: String,
248 private val description: String? = "") {
249 private var operationDefinition: OperationDefinition = OperationDefinition()
251 fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
252 operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
255 fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
256 operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
259 fun build(): OperationDefinition {
260 operationDefinition.id = id
261 operationDefinition.description = description
262 return operationDefinition
266 class AttributesDefinitionBuilder {
267 private val attributes: MutableMap<String, AttributeDefinition> = hashMapOf()
269 fun property(id: String, attribute: AttributeDefinition) {
270 attributes[id] = attribute
273 fun property(id: String, type: String?, required: Boolean?, description: String?) {
274 val attribute = AttributeDefinitionBuilder(id, type, required, description).build()
275 attributes[id] = attribute
278 fun property(id: String, type: String?, required: Boolean?, description: String?,
279 block: AttributeDefinitionBuilder.() -> Unit) {
280 val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
281 attributes[id] = attribute
284 fun build(): MutableMap<String, AttributeDefinition> {
289 class AttributeDefinitionBuilder(private val id: String,
290 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
291 private val required: Boolean? = false,
292 private val description: String? = "") {
294 private var attributeDefinition: AttributeDefinition = AttributeDefinition()
296 fun entrySchema(entrySchemaType: String) {
297 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
300 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
301 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
304 // TODO("Constrains")
306 fun defaultValue(defaultValue: JsonNode) {
307 attributeDefinition.defaultValue = defaultValue
310 fun build(): AttributeDefinition {
311 attributeDefinition.id = id
312 attributeDefinition.type = type!!
313 attributeDefinition.required = required
314 attributeDefinition.description = description
315 return attributeDefinition
319 class PropertiesDefinitionBuilder {
320 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
322 fun property(id: String, property: PropertyDefinition) {
323 properties[id] = property
326 fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
327 val property = PropertyDefinitionBuilder(id, type, required, description).build()
328 properties[id] = property
331 fun property(id: String, type: String?, required: Boolean?, description: String? = "",
332 block: PropertyDefinitionBuilder.() -> Unit) {
333 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
334 properties[id] = property
337 fun build(): MutableMap<String, PropertyDefinition> {
342 class PropertyDefinitionBuilder(private val id: String,
343 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
344 private val required: Boolean? = false,
345 private val description: String? = "") {
347 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
349 fun entrySchema(entrySchemaType: String) {
350 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
353 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
354 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
356 // TODO("Constrains")
358 fun defaultValue(defaultValue: JsonNode) {
359 propertyDefinition.defaultValue = defaultValue
362 fun value(value: JsonNode) {
363 propertyDefinition.value = value
366 fun build(): PropertyDefinition {
367 propertyDefinition.id = id
368 propertyDefinition.type = type!!
369 propertyDefinition.required = required
370 propertyDefinition.description = description
371 return propertyDefinition
375 class EntrySchemaBuilder(private val type: String) {
376 private var entrySchema: EntrySchema = EntrySchema()
378 fun build(): EntrySchema {
379 entrySchema.type = type