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 * @author Brinda Santh
27 class DSLBluePrintBuilder(private val name: String,
28 private val version: String,
29 private val author: String,
30 private val tags: String) {
32 private var dslBluePrint = DSLBluePrint()
33 private var metadata: MutableMap<String, String> = hashMapOf()
34 var properties: MutableMap<String, JsonNode>? = null
35 var dataTypes: MutableMap<String, DataType> = hashMapOf()
36 var artifactTypes: MutableMap<String, ArtifactType> = hashMapOf()
37 var components: MutableMap<String, DSLComponent> = hashMapOf()
38 private var registryComponents: MutableMap<String, DSLRegistryComponent> = hashMapOf()
39 var workflows: MutableMap<String, DSLWorkflow> = hashMapOf()
41 private fun initMetaData() {
42 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
43 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
44 metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
45 metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
48 fun metadata(id: String, value: String) {
52 fun property(id: String, expression: Any) {
53 if (properties == null)
54 properties = hashMapOf()
55 properties!![id] = expression.asJsonType()
58 fun dataType(dataType: DataType) {
59 dataTypes[dataType.id!!] = dataType
62 fun dataType(id: String, version: String, derivedFrom: String, description: String,
63 block: DataTypeBuilder.() -> Unit) {
64 dataTypes[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
67 fun artifactType(artifactType: ArtifactType) {
68 artifactTypes[artifactType.id!!] = artifactType
71 fun artifactType(id: String, version: String, derivedFrom: String, description: String,
72 block: ArtifactTypeBuilder.() -> Unit) {
73 artifactTypes[id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
76 fun component(id: String, type: String, version: String, description: String,
77 block: DSLComponentBuilder.() -> Unit) {
78 components[id] = DSLComponentBuilder(id, type, version, description).apply(block).build()
81 fun registryComponent(id: String, type: String, version: String, interfaceName: String, description: String,
82 block: DSLRegistryComponentBuilder.() -> Unit) {
83 registryComponents[id] = DSLRegistryComponentBuilder(id, type, version, interfaceName, description)
87 fun workflow(id: String, description: String, block: DSLWorkflowBuilder.() -> Unit) {
88 workflows[id] = DSLWorkflowBuilder(id, description).apply(block).build()
91 fun build(): DSLBluePrint {
93 dslBluePrint.metadata = metadata
94 dslBluePrint.properties = properties
95 dslBluePrint.dataTypes = dataTypes
96 dslBluePrint.artifactTypes = artifactTypes
97 dslBluePrint.components = components
98 dslBluePrint.registryComponents = registryComponents
99 dslBluePrint.workflows = workflows
104 class DSLComponentBuilder(private val id: String, private val type: String,
105 private val version: String, private val description: String) {
106 private val dslComponent = DSLComponent()
107 var properties: MutableMap<String, PropertyDefinition>? = null
108 var attributes: MutableMap<String, AttributeDefinition>? = null
110 var artifacts: MutableMap<String, ArtifactDefinition>? = null
111 var implementation: Implementation? = null
112 var inputs: MutableMap<String, PropertyDefinition>? = null
113 var outputs: MutableMap<String, PropertyDefinition>? = null
115 fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
116 if (attributes == null)
117 attributes = hashMapOf()
118 val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
119 attributes!![id] = attribute
122 fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
123 block: DSLAttributeDefinitionBuilder.() -> Unit) {
124 if (attributes == null)
125 attributes = hashMapOf()
126 val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description)
127 .apply(block).build()
128 attributes!![id] = attribute
131 fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
132 if (properties == null)
133 properties = hashMapOf()
134 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
135 properties!![id] = property
138 fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
139 block: DSLPropertyDefinitionBuilder.() -> Unit) {
140 if (properties == null)
141 properties = hashMapOf()
142 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
143 .apply(block).build()
144 properties!![id] = property
147 fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
148 implementation = Implementation().apply {
149 this.operationHost = operationHost!!
150 this.timeout = timeout
154 fun artifact(id: String, type: String, file: String) {
155 if (artifacts == null)
156 artifacts = hashMapOf()
157 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
160 fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
161 if (artifacts == null)
162 artifacts = hashMapOf()
163 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
167 fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
170 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
171 inputs!![id] = property.build()
174 fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
175 block: DSLPropertyDefinitionBuilder.() -> Unit) {
178 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
179 .apply(block).build()
180 inputs!![id] = property
183 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
185 outputs = hashMapOf()
186 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
187 outputs!![id] = property.build()
190 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
191 block: DSLPropertyDefinitionBuilder.() -> Unit) {
193 outputs = hashMapOf()
194 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
195 .apply(block).build()
196 outputs!![id] = property
199 fun build(): DSLComponent {
201 dslComponent.type = type
202 dslComponent.version = version
203 dslComponent.description = description
204 dslComponent.attributes = attributes
205 dslComponent.properties = properties
206 dslComponent.implementation = implementation
207 dslComponent.artifacts = artifacts
208 dslComponent.inputs = inputs
209 dslComponent.outputs = outputs
215 class DSLRegistryComponentBuilder(private val id: String, private val type: String,
216 private val version: String,
217 private val interfaceName: String,
218 private val description: String) {
219 private val dslComponent = DSLRegistryComponent()
220 var properties: MutableMap<String, JsonNode>? = null
222 var artifacts: MutableMap<String, ArtifactDefinition>? = null
223 var implementation: Implementation? = null
224 var inputs: MutableMap<String, JsonNode>? = null
225 var outputs: MutableMap<String, JsonNode>? = null
227 fun property(id: String, expression: Any) {
228 if (properties == null)
229 properties = hashMapOf()
230 properties!![id] = expression.asJsonType()
233 fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
234 implementation = Implementation().apply {
235 this.operationHost = operationHost!!
236 this.timeout = timeout
240 fun artifact(id: String, type: String, file: String) {
241 if (artifacts == null)
242 artifacts = hashMapOf()
243 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
246 fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
247 if (artifacts == null)
248 artifacts = hashMapOf()
249 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
252 fun input(id: String, expression: Any) {
255 inputs!![id] = expression.asJsonType()
258 fun output(id: String, expression: Any) {
260 outputs = hashMapOf()
261 outputs!![id] = expression.asJsonType()
264 fun build(): DSLRegistryComponent {
266 dslComponent.type = type
267 dslComponent.version = version
268 dslComponent.interfaceName = interfaceName
269 dslComponent.description = description
270 dslComponent.properties = properties
271 dslComponent.implementation = implementation
272 dslComponent.artifacts = artifacts
273 dslComponent.inputs = inputs
274 dslComponent.outputs = outputs
279 class DSLWorkflowBuilder(private val actionName: String, private val description: String) {
280 private val dslWorkflow = DSLWorkflow()
281 private var steps: MutableMap<String, Step>? = null
282 private var inputs: MutableMap<String, PropertyDefinition>? = null
283 private var outputs: MutableMap<String, PropertyDefinition>? = null
285 fun input(id: String, type: String, required: Boolean, description: String? = "") {
288 val property = PropertyDefinitionBuilder(id, type, required, description)
289 inputs!![id] = property.build()
292 fun input(id: String, type: String, required: Boolean, description: String, defaultValue: Any?,
293 block: PropertyDefinitionBuilder.() -> Unit) {
296 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
297 if (defaultValue != null)
298 property.defaultValue = defaultValue.asJsonType()
299 inputs!![id] = property
302 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
304 outputs = hashMapOf()
305 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
306 outputs!![id] = property.build()
309 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
310 block: DSLPropertyDefinitionBuilder.() -> Unit) {
312 outputs = hashMapOf()
313 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
314 .apply(block).build()
315 outputs!![id] = property
318 fun step(id: String, target: String, description: String) {
321 steps!![id] = StepBuilder(id, target, description).build()
324 fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
327 steps!![id] = StepBuilder(id, target, description).apply(block).build()
330 fun build(): DSLWorkflow {
331 dslWorkflow.actionName = actionName
332 dslWorkflow.description = description
333 dslWorkflow.inputs = inputs
334 dslWorkflow.outputs = outputs
335 dslWorkflow.steps = steps!!
340 class DSLAttributeDefinitionBuilder(private val id: String,
341 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
342 private val required: Boolean? = false,
343 private val expression: JsonNode,
344 private val description: String? = "") {
346 private var attributeDefinition = AttributeDefinition()
348 fun entrySchema(entrySchemaType: String) {
349 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
352 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
353 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
355 // TODO("Constrains")
357 fun defaultValue(defaultValue: JsonNode) {
358 attributeDefinition.defaultValue = defaultValue
361 fun build(): AttributeDefinition {
362 attributeDefinition.id = id
363 attributeDefinition.type = type!!
364 attributeDefinition.required = required
365 attributeDefinition.value = expression
366 attributeDefinition.description = description
367 return attributeDefinition
371 class DSLPropertyDefinitionBuilder(private val id: String,
372 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
373 private val required: Boolean? = false,
374 private val expression: JsonNode,
375 private val description: String? = "") {
377 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
379 fun entrySchema(entrySchemaType: String) {
380 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
383 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
384 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
386 // TODO("Constrains")
388 fun defaultValue(defaultValue: JsonNode) {
389 propertyDefinition.defaultValue = defaultValue
392 fun build(): PropertyDefinition {
393 propertyDefinition.id = id
394 propertyDefinition.type = type!!
395 propertyDefinition.required = required
396 propertyDefinition.value = expression
397 propertyDefinition.description = description
398 return propertyDefinition