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.ArtifactType
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.AttributeDefinition
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
31 * @author Brinda Santh
33 class DSLBlueprintBuilder(
34 private val name: String,
35 private val version: String,
36 private val author: String,
37 private val tags: String
40 private var dslBlueprint = DSLBlueprint()
41 private var metadata: MutableMap<String, String> = hashMapOf()
42 var properties: MutableMap<String, JsonNode>? = null
43 var dataTypes: MutableMap<String, DataType> = hashMapOf()
44 var artifactTypes: MutableMap<String, ArtifactType> = hashMapOf()
45 var components: MutableMap<String, DSLComponent> = hashMapOf()
46 private var registryComponents: MutableMap<String, DSLRegistryComponent> = hashMapOf()
47 var workflows: MutableMap<String, DSLWorkflow> = hashMapOf()
49 private fun initMetaData() {
50 metadata[BlueprintConstants.METADATA_TEMPLATE_NAME] = name
51 metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION] = version
52 metadata[BlueprintConstants.METADATA_TEMPLATE_AUTHOR] = author
53 metadata[BlueprintConstants.METADATA_TEMPLATE_TAGS] = tags
56 fun metadata(id: String, value: String) {
60 fun property(id: String, expression: Any) {
61 if (properties == null)
62 properties = hashMapOf()
63 properties!![id] = expression.asJsonType()
66 fun dataType(dataType: DataType) {
67 dataTypes[dataType.id!!] = dataType
75 block: DataTypeBuilder.() -> Unit
77 dataTypes[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
80 fun artifactType(artifactType: ArtifactType) {
81 artifactTypes[artifactType.id!!] = artifactType
89 block: ArtifactTypeBuilder.() -> Unit
91 artifactTypes[id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
99 block: DSLComponentBuilder.() -> Unit
101 components[id] = DSLComponentBuilder(id, type, version, description).apply(block).build()
104 fun registryComponent(
108 interfaceName: String,
110 block: DSLRegistryComponentBuilder.() -> Unit
112 registryComponents[id] = DSLRegistryComponentBuilder(id, type, version, interfaceName, description)
113 .apply(block).build()
116 fun workflow(id: String, description: String, block: DSLWorkflowBuilder.() -> Unit) {
117 workflows[id] = DSLWorkflowBuilder(id, description).apply(block).build()
120 fun build(): DSLBlueprint {
122 dslBlueprint.metadata = metadata
123 dslBlueprint.properties = properties
124 dslBlueprint.dataTypes = dataTypes
125 dslBlueprint.artifactTypes = artifactTypes
126 dslBlueprint.components = components
127 dslBlueprint.registryComponents = registryComponents
128 dslBlueprint.workflows = workflows
133 class DSLComponentBuilder(
134 private val id: String,
135 private val type: String,
136 private val version: String,
137 private val description: String
140 private val dslComponent = DSLComponent()
141 var properties: MutableMap<String, PropertyDefinition>? = null
142 var attributes: MutableMap<String, AttributeDefinition>? = null
144 var artifacts: MutableMap<String, ArtifactDefinition>? = null
145 var implementation: Implementation? = null
146 var inputs: MutableMap<String, PropertyDefinition>? = null
147 var outputs: MutableMap<String, PropertyDefinition>? = null
149 fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
150 if (attributes == null)
151 attributes = hashMapOf()
152 val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
153 attributes!![id] = attribute
161 description: String? = "",
162 block: DSLAttributeDefinitionBuilder.() -> Unit
164 if (attributes == null)
165 attributes = hashMapOf()
166 val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description)
167 .apply(block).build()
168 attributes!![id] = attribute
171 fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
172 if (properties == null)
173 properties = hashMapOf()
174 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
175 properties!![id] = property
183 description: String? = "",
184 block: DSLPropertyDefinitionBuilder.() -> Unit
186 if (properties == null)
187 properties = hashMapOf()
188 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
189 .apply(block).build()
190 properties!![id] = property
193 fun implementation(timeout: Int, operationHost: String? = BlueprintConstants.PROPERTY_SELF) {
194 implementation = Implementation().apply {
195 this.operationHost = operationHost!!
196 this.timeout = timeout
200 fun artifact(id: String, type: String, file: String) {
201 if (artifacts == null)
202 artifacts = hashMapOf()
203 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
206 fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
207 if (artifacts == null)
208 artifacts = hashMapOf()
209 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
212 fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
215 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
216 inputs!![id] = property.build()
224 description: String? = "",
225 block: DSLPropertyDefinitionBuilder.() -> Unit
229 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
230 .apply(block).build()
231 inputs!![id] = property
234 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
236 outputs = hashMapOf()
237 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
238 outputs!![id] = property.build()
246 description: String? = "",
247 block: DSLPropertyDefinitionBuilder.() -> Unit
250 outputs = hashMapOf()
251 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
252 .apply(block).build()
253 outputs!![id] = property
256 fun build(): DSLComponent {
258 dslComponent.type = type
259 dslComponent.version = version
260 dslComponent.description = description
261 dslComponent.attributes = attributes
262 dslComponent.properties = properties
263 dslComponent.implementation = implementation
264 dslComponent.artifacts = artifacts
265 dslComponent.inputs = inputs
266 dslComponent.outputs = outputs
272 class DSLRegistryComponentBuilder(
273 private val id: String,
274 private val type: String,
275 private val version: String,
276 private val interfaceName: String,
277 private val description: String
280 private val dslComponent = DSLRegistryComponent()
281 var properties: MutableMap<String, JsonNode>? = null
283 var artifacts: MutableMap<String, ArtifactDefinition>? = null
284 var implementation: Implementation? = null
285 var inputs: MutableMap<String, JsonNode>? = null
286 var outputs: MutableMap<String, JsonNode>? = null
288 fun property(id: String, expression: Any) {
289 if (properties == null)
290 properties = hashMapOf()
291 properties!![id] = expression.asJsonType()
294 fun implementation(timeout: Int, operationHost: String? = BlueprintConstants.PROPERTY_SELF) {
295 implementation = Implementation().apply {
296 this.operationHost = operationHost!!
297 this.timeout = timeout
301 fun artifact(id: String, type: String, file: String) {
302 if (artifacts == null)
303 artifacts = hashMapOf()
304 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
307 fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
308 if (artifacts == null)
309 artifacts = hashMapOf()
310 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
313 fun input(id: String, expression: Any) {
316 inputs!![id] = expression.asJsonType()
319 fun output(id: String, expression: Any) {
321 outputs = hashMapOf()
322 outputs!![id] = expression.asJsonType()
325 fun build(): DSLRegistryComponent {
327 dslComponent.type = type
328 dslComponent.version = version
329 dslComponent.interfaceName = interfaceName
330 dslComponent.description = description
331 dslComponent.properties = properties
332 dslComponent.implementation = implementation
333 dslComponent.artifacts = artifacts
334 dslComponent.inputs = inputs
335 dslComponent.outputs = outputs
340 class DSLWorkflowBuilder(private val actionName: String, private val description: String) {
342 private val dslWorkflow = DSLWorkflow()
343 private var steps: MutableMap<String, Step>? = null
344 private var inputs: MutableMap<String, PropertyDefinition>? = null
345 private var outputs: MutableMap<String, PropertyDefinition>? = null
347 fun input(id: String, type: String, required: Boolean, description: String? = "") {
350 val property = PropertyDefinitionBuilder(id, type, required, description)
351 inputs!![id] = property.build()
360 block: PropertyDefinitionBuilder.() -> Unit
364 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
365 if (defaultValue != null)
366 property.defaultValue = defaultValue.asJsonType()
367 inputs!![id] = property
370 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
372 outputs = hashMapOf()
373 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
374 outputs!![id] = property.build()
382 description: String? = "",
383 block: DSLPropertyDefinitionBuilder.() -> Unit
386 outputs = hashMapOf()
387 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
388 .apply(block).build()
389 outputs!![id] = property
392 fun step(id: String, target: String, description: String) {
395 steps!![id] = StepBuilder(id, target, description).build()
398 fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
401 steps!![id] = StepBuilder(id, target, description).apply(block).build()
404 fun build(): DSLWorkflow {
405 dslWorkflow.actionName = actionName
406 dslWorkflow.description = description
407 dslWorkflow.inputs = inputs
408 dslWorkflow.outputs = outputs
409 dslWorkflow.steps = steps!!
414 class DSLAttributeDefinitionBuilder(
415 private val id: String,
416 private val type: String? = BlueprintConstants.DATA_TYPE_STRING,
417 private val required: Boolean? = false,
418 private val expression: JsonNode,
419 private val description: String? = ""
422 private var attributeDefinition = AttributeDefinition()
424 fun entrySchema(entrySchemaType: String) {
425 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
428 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
429 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
431 // TODO("Constrains")
433 fun defaultValue(defaultValue: JsonNode) {
434 attributeDefinition.defaultValue = defaultValue
437 fun build(): AttributeDefinition {
438 attributeDefinition.id = id
439 attributeDefinition.type = type!!
440 attributeDefinition.required = required
441 attributeDefinition.value = expression
442 attributeDefinition.description = description
443 return attributeDefinition
447 class DSLPropertyDefinitionBuilder(
448 private val id: String,
449 private val type: String? = BlueprintConstants.DATA_TYPE_STRING,
450 private val required: Boolean? = false,
451 private val expression: JsonNode,
452 private val description: String? = ""
455 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
457 fun entrySchema(entrySchemaType: String) {
458 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
461 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
462 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
464 // TODO("Constrains")
466 fun defaultValue(defaultValue: JsonNode) {
467 propertyDefinition.defaultValue = defaultValue
470 fun build(): PropertyDefinition {
471 propertyDefinition.id = id
472 propertyDefinition.type = type!!
473 propertyDefinition.required = required
474 propertyDefinition.value = expression
475 propertyDefinition.description = description
476 return propertyDefinition