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.ArtifactDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityAssignment
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.InterfaceAssignment
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipTemplate
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.RequirementAssignment
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.TopologyTemplate
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
32 import kotlin.reflect.KClass
33 import kotlin.reflect.KMutableProperty1
34 import kotlin.reflect.full.createInstance
35 import kotlin.reflect.jvm.reflect
37 open class TopologyTemplateBuilder {
38 private var topologyTemplate = TopologyTemplate()
39 private var nodeTemplates: MutableMap<String, NodeTemplate>? = null
40 var relationshipTemplates: MutableMap<String, RelationshipTemplate>? = null
41 private var workflows: MutableMap<String, Workflow>? = null
43 fun nodeTemplate(id: String, type: String, description: String, block: NodeTemplateBuilder.() -> Unit) {
44 if (nodeTemplates == null) nodeTemplates = hashMapOf()
45 nodeTemplates!![id] = NodeTemplateBuilder(id, type, description).apply(block).build()
48 fun nodeTemplate(nodeTemplate: NodeTemplate) {
49 if (nodeTemplates == null) nodeTemplates = hashMapOf()
50 nodeTemplates!![nodeTemplate.id!!] = nodeTemplate
53 fun relationshipTemplate(
57 block: RelationshipTemplateBuilder.() -> Unit
59 if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
60 relationshipTemplates!![id] = RelationshipTemplateBuilder(id, type, description).apply(block).build()
63 fun relationshipTemplate(relationshipTemplate: RelationshipTemplate) {
64 if (relationshipTemplates == null) relationshipTemplates = hashMapOf()
65 relationshipTemplates!![relationshipTemplate.id!!] = relationshipTemplate
68 fun nodeTemplateOperation(
69 nodeTemplateName: String,
71 interfaceName: String,
73 operationBlock: OperationAssignmentBuilder<PropertiesAssignmentBuilder,
74 PropertiesAssignmentBuilder>.() -> Unit
76 if (nodeTemplates == null) nodeTemplates = hashMapOf()
78 val nodeTemplateBuilder = NodeTemplateBuilder(nodeTemplateName, type, description)
79 nodeTemplateBuilder.operation(interfaceName, "$description operation", operationBlock)
80 nodeTemplates!![nodeTemplateName] = nodeTemplateBuilder.build()
83 fun workflow(id: String, description: String, block: WorkflowBuilder.() -> Unit) {
84 if (workflows == null) workflows = hashMapOf()
85 workflows!![id] = WorkflowBuilder(id, description).apply(block).build()
88 fun workflow(workflow: Workflow) {
89 if (workflows == null) workflows = hashMapOf()
90 workflows!![workflow.id!!] = workflow
93 // TODO("populate inputs, outputs")
94 fun workflowNodeTemplate(
96 nodeTemplateType: String,
98 block: NodeTemplateBuilder.() -> Unit
100 if (nodeTemplates == null) nodeTemplates = hashMapOf()
102 if (workflows == null) workflows = hashMapOf()
104 val workflowBuilder = WorkflowBuilder(actionName, description)
105 workflowBuilder.nodeTemplateStep(actionName, description)
106 // Workflow name is NodeTemplate name
107 workflows!![actionName] = workflowBuilder.build()
109 nodeTemplates!![actionName] =
110 NodeTemplateBuilder(actionName, nodeTemplateType, description).apply(block).build()
113 fun build(): TopologyTemplate {
114 topologyTemplate.nodeTemplates = nodeTemplates
115 topologyTemplate.relationshipTemplates = relationshipTemplates
116 topologyTemplate.workflows = workflows
117 return topologyTemplate
121 open class NodeTemplateBuilder(
122 private val id: String,
123 private val type: String,
124 private val description: String? = ""
127 var nodeTemplate: NodeTemplate = NodeTemplate()
128 var properties: MutableMap<String, JsonNode>? = null
129 var interfaces: MutableMap<String, InterfaceAssignment>? = null
130 var artifacts: MutableMap<String, ArtifactDefinition>? = null
131 var capabilities: MutableMap<String, CapabilityAssignment>? = null
132 var requirements: MutableMap<String, RequirementAssignment>? = null
134 fun properties(properties: Map<String, JsonNode>) {
135 if (this.properties == null) this.properties = hashMapOf()
136 this.properties!!.putAll(properties)
139 fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
140 if (properties == null) properties = hashMapOf()
141 properties = PropertiesAssignmentBuilder().apply(block).build()
144 open fun <Prop : PropertiesAssignmentBuilder> typedProperties(block: Prop.() -> Unit) {
145 if (properties == null) properties = hashMapOf()
146 val instance: Prop = (block.reflect()?.parameters?.get(0)?.type?.classifier as KClass<Prop>).createInstance()
147 properties = instance.apply(block).build()
150 open fun <In : PropertiesAssignmentBuilder, Out : PropertiesAssignmentBuilder> typedOperation(
151 interfaceName: String,
152 description: String = "",
153 block: OperationAssignmentBuilder<In, Out>.() -> Unit
155 if (interfaces == null) interfaces = hashMapOf()
157 val interfaceAssignment = InterfaceAssignment()
158 val defaultOperationName = BluePrintConstants.DEFAULT_STEP_OPERATION
159 interfaceAssignment.operations = hashMapOf()
160 interfaceAssignment.operations!![defaultOperationName] =
161 OperationAssignmentBuilder<In, Out>(defaultOperationName, description).apply(block).build()
162 interfaces!![interfaceName] = interfaceAssignment
166 interfaceName: String,
168 block: OperationAssignmentBuilder<PropertiesAssignmentBuilder, PropertiesAssignmentBuilder>.() -> Unit
170 typedOperation<PropertiesAssignmentBuilder, PropertiesAssignmentBuilder>(interfaceName, description, block)
173 fun artifact(id: String, type: String, file: String) {
174 if (artifacts == null) artifacts = hashMapOf()
175 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
178 fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
179 if (artifacts == null) artifacts = hashMapOf()
180 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
183 fun artifacts(artifacts: MutableMap<String, ArtifactDefinition>?) {
184 this.artifacts = artifacts
187 fun capability(id: String, block: CapabilityAssignmentBuilder.() -> Unit) {
188 if (capabilities == null) capabilities = hashMapOf()
189 capabilities!![id] = CapabilityAssignmentBuilder(id).apply(block).build()
192 fun capabilities(capabilities: MutableMap<String, CapabilityAssignment>) {
193 if (this.capabilities == null) this.capabilities = hashMapOf()
194 this.capabilities!!.putAll(capabilities)
197 fun requirement(id: String, capability: String, node: String, relationship: String) {
198 if (requirements == null) requirements = hashMapOf()
199 requirements!![id] = RequirementAssignmentBuilder(id, capability, node, relationship).build()
202 fun requirements(requirements: MutableMap<String, RequirementAssignment>) {
203 if (this.requirements == null) this.requirements = hashMapOf()
204 this.requirements!!.putAll(requirements)
207 open fun build(): NodeTemplate {
209 nodeTemplate.type = type
210 nodeTemplate.description = description
211 nodeTemplate.properties = properties
212 nodeTemplate.interfaces = interfaces
213 nodeTemplate.artifacts = artifacts
214 nodeTemplate.capabilities = capabilities
215 nodeTemplate.requirements = requirements
220 open class RelationshipTemplateBuilder(
221 private val id: String,
222 private val type: String,
223 private val description: String? = ""
225 var relationshipTemplate: RelationshipTemplate = RelationshipTemplate()
226 var properties: MutableMap<String, JsonNode>? = null
228 fun properties(properties: Map<String, JsonNode>) {
229 if (this.properties == null) this.properties = hashMapOf()
230 this.properties!!.putAll(properties)
233 fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
234 if (properties == null) properties = hashMapOf()
235 properties = PropertiesAssignmentBuilder().apply(block).build()
238 fun property(id: String, value: Any) {
239 if (properties == null) properties = hashMapOf()
240 properties!![id] = value.asJsonType()
243 fun property(id: String, value: JsonNode) {
244 if (properties == null) properties = hashMapOf()
245 properties!![id] = value
248 fun copy(copy: String) {
249 relationshipTemplate.copy = copy
252 open fun <Prop : PropertiesAssignmentBuilder> typedProperties(block: Prop.() -> Unit) {
253 if (properties == null) properties = hashMapOf()
254 val instance: Prop = (block.reflect()?.parameters?.get(0)?.type?.classifier as KClass<Prop>).createInstance()
255 properties = instance.apply(block).build()
258 open fun build(): RelationshipTemplate {
259 relationshipTemplate.id = id
260 relationshipTemplate.type = type
261 relationshipTemplate.description = description
262 relationshipTemplate.properties = properties
263 return relationshipTemplate
267 class ArtifactDefinitionBuilder(private val id: String, private val type: String, private val file: String) {
269 private var artifactDefinition: ArtifactDefinition = ArtifactDefinition()
270 private var properties: MutableMap<String, JsonNode>? = null
272 fun repository(repository: String) {
273 artifactDefinition.repository = repository
276 fun deployPath(deployPath: String) {
277 artifactDefinition.deployPath = deployPath
280 fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
281 if (properties == null)
282 properties = hashMapOf()
283 properties = PropertiesAssignmentBuilder().apply(block).build()
286 fun build(): ArtifactDefinition {
287 artifactDefinition.id = id
288 artifactDefinition.type = type
289 artifactDefinition.file = file
290 artifactDefinition.properties = properties
291 return artifactDefinition
295 open class CapabilityAssignmentBuilder(private val id: String) {
296 var capabilityAssignment: CapabilityAssignment = CapabilityAssignment()
297 var attributes: MutableMap<String, JsonNode>? = null
298 var properties: MutableMap<String, JsonNode>? = null
300 fun attributes(block: AttributesAssignmentBuilder.() -> Unit) {
301 if (attributes == null) attributes = hashMapOf()
302 attributes = AttributesAssignmentBuilder().apply(block).build()
305 fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
306 if (properties == null) properties = hashMapOf()
307 properties = PropertiesAssignmentBuilder().apply(block).build()
310 fun property(id: String, value: Any) {
311 if (properties == null) properties = hashMapOf()
312 properties!![id] = value.asJsonType()
315 fun property(id: String, value: JsonNode) {
316 if (properties == null) properties = hashMapOf()
317 properties!![id] = value
320 open fun build(): CapabilityAssignment {
321 capabilityAssignment.properties = properties
322 capabilityAssignment.attributes = attributes
323 return capabilityAssignment
327 open class RequirementAssignmentBuilder(
328 private val id: String,
329 private val capability: String,
330 private val node: String,
331 private val relationship: String
334 private var requirementAssignment: RequirementAssignment = RequirementAssignment()
336 fun build(): RequirementAssignment {
337 requirementAssignment.id = id
338 requirementAssignment.capability = capability
339 requirementAssignment.node = node
340 requirementAssignment.relationship = relationship
341 return requirementAssignment
345 class InterfaceAssignmentBuilder(private val id: String) {
347 private var interfaceAssignment: InterfaceAssignment = InterfaceAssignment()
348 private var operations: MutableMap<String, OperationAssignment>? = null
352 description: String? = "",
353 block: OperationAssignmentBuilder<PropertiesAssignmentBuilder, PropertiesAssignmentBuilder>.() -> Unit
355 if (operations == null)
356 operations = hashMapOf()
357 operations!![id] = OperationAssignmentBuilder<PropertiesAssignmentBuilder, PropertiesAssignmentBuilder>(
359 ).apply(block).build()
362 fun build(): InterfaceAssignment {
363 interfaceAssignment.id = id
364 interfaceAssignment.operations = operations
365 return interfaceAssignment
369 class OperationAssignmentBuilder<In : PropertiesAssignmentBuilder, Out : PropertiesAssignmentBuilder>(
370 private val id: String,
371 private val description: String? = ""
374 private var operationAssignment: OperationAssignment = OperationAssignment()
376 fun implementation(implementation: Implementation?) {
377 operationAssignment.implementation = implementation
380 fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
381 operationAssignment.implementation = Implementation().apply {
382 this.operationHost = operationHost!!
383 this.timeout = timeout
389 operationHost: String? = BluePrintConstants.PROPERTY_SELF,
390 block: ImplementationBuilder.() -> Unit
392 operationAssignment.implementation = ImplementationBuilder(timeout, operationHost!!).apply(block).build()
395 fun inputs(inputs: MutableMap<String, JsonNode>?) {
396 operationAssignment.inputs = inputs
399 fun inputs(block: In.() -> Unit) {
400 val instance: In = (block.reflect()?.parameters?.get(0)?.type?.classifier as KClass<In>).createInstance()
401 operationAssignment.inputs = instance.apply(block).build()
404 fun outputs(outputs: MutableMap<String, JsonNode>?) {
405 operationAssignment.outputs = outputs
408 fun outputs(block: Out.() -> Unit) {
409 val instance: Out = (block.reflect()?.parameters?.get(0)?.type?.classifier as KClass<Out>).createInstance()
410 operationAssignment.outputs = instance.apply(block).build()
413 fun build(): OperationAssignment {
414 operationAssignment.id = id
415 operationAssignment.description = description
416 return operationAssignment
420 class ImplementationBuilder(private val timeout: Int, private val operationHost: String) {
421 private val implementation = Implementation()
423 fun primary(primary: String) {
424 implementation.primary = primary
427 fun dependencies(vararg dependencies: String) {
428 if (implementation.dependencies == null)
429 implementation.dependencies = arrayListOf()
430 dependencies.forEach {
431 implementation.dependencies!!.add(it)
435 fun build(): Implementation {
436 implementation.timeout = timeout
437 implementation.operationHost = operationHost
438 return implementation
442 open class PropertiesAssignmentBuilder {
443 var properties: MutableMap<String, JsonNode> = hashMapOf()
445 fun property(id: String, value: Any) {
446 property(id, value.asJsonType())
449 fun property(id: String, value: JsonNode) {
450 properties[id] = value
453 fun property(kProperty: KMutableProperty1<*, *>, value: JsonNode) {
454 properties[kProperty.name] = value
457 open fun build(): MutableMap<String, JsonNode> {
462 open class AttributesAssignmentBuilder {
463 var attributes: MutableMap<String, JsonNode> = hashMapOf()
465 fun attribute(id: String, value: String) {
466 attribute(id, value.asJsonType())
469 fun attribute(id: String, value: JsonNode) {
470 attributes[id] = value
473 fun build(): MutableMap<String, JsonNode> {