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 dataType: MutableMap<String, DataType> = hashMapOf()
36 var artifacts: MutableMap<String, ArtifactDefinition> = hashMapOf()
37 var components: MutableMap<String, DSLComponent> = hashMapOf()
38 var workflows: MutableMap<String, DSLWorkflow> = hashMapOf()
40 private fun initMetaData() {
41 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
42 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
43 metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
44 metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
47 fun metadata(id: String, value: String) {
51 fun property(id: String, expression: Any) {
52 if (properties == null)
53 properties = hashMapOf()
54 properties!![id] = expression.asJsonType()
57 fun dataType(id: String, version: String, derivedFrom: String, description: String,
58 block: DataTypeBuilder.() -> Unit) {
59 dataType[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
62 fun component(id: String, type: String, version: String, description: String, block: DSLComponentBuilder.() -> Unit) {
63 components[id] = DSLComponentBuilder(id, type, version, description).apply(block).build()
66 fun workflow(id: String, description: String, block: DSLWorkflowBuilder.() -> Unit) {
67 workflows[id] = DSLWorkflowBuilder(id, description).apply(block).build()
70 fun build(): DSLBluePrint {
72 dslBluePrint.metadata = metadata
73 dslBluePrint.properties = properties
74 dslBluePrint.dataType = dataType
75 dslBluePrint.components = components
76 dslBluePrint.workflows = workflows
81 class DSLComponentBuilder(private val id: String, private val type: String,
82 private val version: String, private val description: String) {
83 private val dslComponent = DSLComponent()
84 var properties: MutableMap<String, PropertyDefinition>? = null
85 var attributes: MutableMap<String, AttributeDefinition>? = null
87 // For already registered components
88 private var assignProperties: MutableMap<String, JsonNode>? = null
90 var artifacts: MutableMap<String, ArtifactDefinition>? = null
91 var implementation: Implementation? = null
92 var inputs: MutableMap<String, PropertyDefinition>? = null
93 var outputs: MutableMap<String, PropertyDefinition>? = null
95 // For already registered components
96 private var assignInputs: MutableMap<String, JsonNode>? = null
97 private var assignOutputs: MutableMap<String, JsonNode>? = null
99 fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
100 if (attributes == null)
101 attributes = hashMapOf()
102 val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
103 attributes!![id] = attribute
106 fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
107 block: DSLAttributeDefinitionBuilder.() -> Unit) {
108 if (attributes == null)
109 attributes = hashMapOf()
110 val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description)
111 .apply(block).build()
112 attributes!![id] = attribute
115 fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
116 if (properties == null)
117 properties = hashMapOf()
118 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
119 properties!![id] = property
122 fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
123 block: DSLPropertyDefinitionBuilder.() -> Unit) {
124 if (properties == null)
125 properties = hashMapOf()
126 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
127 .apply(block).build()
128 properties!![id] = property
131 fun assignProperty(id: String, expression: Any) {
132 if (assignProperties == null)
133 assignProperties = hashMapOf()
134 assignProperties!![id] = expression.asJsonType()
137 fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
138 implementation = Implementation().apply {
139 this.operationHost = operationHost!!
140 this.timeout = timeout
144 fun artifacts(id: String, type: String, file: String) {
145 if (artifacts == null)
146 artifacts = hashMapOf()
147 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
150 fun artifacts(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
151 if (artifacts == null)
152 artifacts = hashMapOf()
153 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
157 fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
160 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
161 inputs!![id] = property.build()
164 fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
165 block: DSLPropertyDefinitionBuilder.() -> Unit) {
168 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
169 .apply(block).build()
170 inputs!![id] = property
173 fun assignInput(id: String, expression: Any) {
174 if (assignInputs == null)
175 assignInputs = hashMapOf()
176 assignInputs!![id] = expression.asJsonType()
179 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
181 outputs = hashMapOf()
182 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
183 outputs!![id] = property.build()
186 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
187 block: DSLPropertyDefinitionBuilder.() -> Unit) {
189 outputs = hashMapOf()
190 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
191 .apply(block).build()
192 outputs!![id] = property
195 fun assignOutput(id: String, expression: Any) {
196 if (assignOutputs == null)
197 assignOutputs = hashMapOf()
198 assignOutputs!![id] = expression.asJsonType()
201 fun build(): DSLComponent {
203 dslComponent.type = type
204 dslComponent.version = version
205 dslComponent.description = description
206 dslComponent.attributes = attributes
207 dslComponent.properties = properties
208 dslComponent.assignProperties = assignProperties
209 dslComponent.implementation = implementation
210 dslComponent.artifacts = artifacts
211 dslComponent.inputs = inputs
212 dslComponent.outputs = outputs
213 dslComponent.assignInputs = assignInputs
214 dslComponent.assignOutputs = assignOutputs
215 dslComponent.outputs = outputs
221 class DSLWorkflowBuilder(private val actionName: String, private val description: String) {
222 private val dslWorkflow = DSLWorkflow()
223 private var steps: MutableMap<String, Step>? = null
224 private var inputs: MutableMap<String, PropertyDefinition>? = null
225 private var outputs: MutableMap<String, PropertyDefinition>? = null
227 fun input(id: String, type: String, required: Boolean, description: String? = "") {
230 val property = PropertyDefinitionBuilder(id, type, required, description)
231 inputs!![id] = property.build()
234 fun input(id: String, type: String, required: Boolean, description: String, defaultValue: Any?,
235 block: PropertyDefinitionBuilder.() -> Unit) {
238 val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
239 if (defaultValue != null)
240 property.defaultValue = defaultValue.asJsonType()
241 inputs!![id] = property
244 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
246 outputs = hashMapOf()
247 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
248 outputs!![id] = property.build()
251 fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "",
252 block: DSLPropertyDefinitionBuilder.() -> Unit) {
254 outputs = hashMapOf()
255 val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
256 .apply(block).build()
257 outputs!![id] = property
260 fun step(id: String, target: String, description: String) {
263 steps!![id] = StepBuilder(id, target, description).build()
266 fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
269 steps!![id] = StepBuilder(id, target, description).apply(block).build()
272 fun build(): DSLWorkflow {
273 dslWorkflow.actionName = actionName
274 dslWorkflow.description = description
275 dslWorkflow.inputs = inputs
276 dslWorkflow.outputs = outputs
277 dslWorkflow.steps = steps!!
282 class DSLAttributeDefinitionBuilder(private val id: String,
283 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
284 private val required: Boolean? = false,
285 private val expression: JsonNode,
286 private val description: String? = "") {
288 private var attributeDefinition = AttributeDefinition()
290 fun entrySchema(entrySchemaType: String) {
291 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
294 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
295 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
297 // TODO("Constrains")
299 fun defaultValue(defaultValue: JsonNode) {
300 attributeDefinition.defaultValue = defaultValue
303 fun build(): AttributeDefinition {
304 attributeDefinition.id = id
305 attributeDefinition.type = type!!
306 attributeDefinition.required = required
307 attributeDefinition.value = expression
308 attributeDefinition.description = description
309 return attributeDefinition
313 class DSLPropertyDefinitionBuilder(private val id: String,
314 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
315 private val required: Boolean? = false,
316 private val expression: JsonNode,
317 private val description: String? = "") {
319 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
321 fun entrySchema(entrySchemaType: String) {
322 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
325 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
326 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
328 // TODO("Constrains")
330 fun defaultValue(defaultValue: JsonNode) {
331 propertyDefinition.defaultValue = defaultValue
334 fun build(): PropertyDefinition {
335 propertyDefinition.id = id
336 propertyDefinition.type = type!!
337 propertyDefinition.required = required
338 propertyDefinition.value = expression
339 propertyDefinition.description = description
340 return propertyDefinition