aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLBuilder.kt
blob: b469ded745810431bcaf6ae8ce3810bc88c56fc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 *  Copyright © 2019 IBM.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.onap.ccsdk.cds.controllerblueprints.core.dsl

import com.fasterxml.jackson.databind.JsonNode
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
import org.onap.ccsdk.cds.controllerblueprints.core.data.AttributeDefinition
import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
import org.onap.ccsdk.cds.controllerblueprints.core.data.Step

/**
 * @author Brinda Santh
 */
class DSLBluePrintBuilder(
    private val name: String,
    private val version: String,
    private val author: String,
    private val tags: String
) {

    private var dslBluePrint = DSLBluePrint()
    private var metadata: MutableMap<String, String> = hashMapOf()
    var properties: MutableMap<String, JsonNode>? = null
    var dataTypes: MutableMap<String, DataType> = hashMapOf()
    var artifactTypes: MutableMap<String, ArtifactType> = hashMapOf()
    var components: MutableMap<String, DSLComponent> = hashMapOf()
    private var registryComponents: MutableMap<String, DSLRegistryComponent> = hashMapOf()
    var workflows: MutableMap<String, DSLWorkflow> = hashMapOf()

    private fun initMetaData() {
        metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
        metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
        metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
        metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
    }

    fun metadata(id: String, value: String) {
        metadata[id] = value
    }

    fun property(id: String, expression: Any) {
        if (properties == null)
            properties = hashMapOf()
        properties!![id] = expression.asJsonType()
    }

    fun dataType(dataType: DataType) {
        dataTypes[dataType.id!!] = dataType
    }

    fun dataType(
        id: String,
        version: String,
        derivedFrom: String,
        description: String,
        block: DataTypeBuilder.() -> Unit
    ) {
        dataTypes[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
    }

    fun artifactType(artifactType: ArtifactType) {
        artifactTypes[artifactType.id!!] = artifactType
    }

    fun artifactType(
        id: String,
        version: String,
        derivedFrom: String,
        description: String,
        block: ArtifactTypeBuilder.() -> Unit
    ) {
        artifactTypes[id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
    }

    fun component(
        id: String,
        type: String,
        version: String,
        description: String,
        block: DSLComponentBuilder.() -> Unit
    ) {
        components[id] = DSLComponentBuilder(id, type, version, description).apply(block).build()
    }

    fun registryComponent(
        id: String,
        type: String,
        version: String,
        interfaceName: String,
        description: String,
        block: DSLRegistryComponentBuilder.() -> Unit
    ) {
        registryComponents[id] = DSLRegistryComponentBuilder(id, type, version, interfaceName, description)
            .apply(block).build()
    }

    fun workflow(id: String, description: String, block: DSLWorkflowBuilder.() -> Unit) {
        workflows[id] = DSLWorkflowBuilder(id, description).apply(block).build()
    }

    fun build(): DSLBluePrint {
        initMetaData()
        dslBluePrint.metadata = metadata
        dslBluePrint.properties = properties
        dslBluePrint.dataTypes = dataTypes
        dslBluePrint.artifactTypes = artifactTypes
        dslBluePrint.components = components
        dslBluePrint.registryComponents = registryComponents
        dslBluePrint.workflows = workflows
        return dslBluePrint
    }
}

class DSLComponentBuilder(
    private val id: String,
    private val type: String,
    private val version: String,
    private val description: String
) {

    private val dslComponent = DSLComponent()
    var properties: MutableMap<String, PropertyDefinition>? = null
    var attributes: MutableMap<String, AttributeDefinition>? = null

    var artifacts: MutableMap<String, ArtifactDefinition>? = null
    var implementation: Implementation? = null
    var inputs: MutableMap<String, PropertyDefinition>? = null
    var outputs: MutableMap<String, PropertyDefinition>? = null

    fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
        if (attributes == null)
            attributes = hashMapOf()
        val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
        attributes!![id] = attribute
    }

    fun attribute(
        id: String,
        type: String,
        required: Boolean,
        expression: Any,
        description: String? = "",
        block: DSLAttributeDefinitionBuilder.() -> Unit
    ) {
        if (attributes == null)
            attributes = hashMapOf()
        val attribute = DSLAttributeDefinitionBuilder(id, type, required, expression.asJsonType(), description)
            .apply(block).build()
        attributes!![id] = attribute
    }

    fun property(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
        if (properties == null)
            properties = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description).build()
        properties!![id] = property
    }

    fun property(
        id: String,
        type: String,
        required: Boolean,
        expression: Any,
        description: String? = "",
        block: DSLPropertyDefinitionBuilder.() -> Unit
    ) {
        if (properties == null)
            properties = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
            .apply(block).build()
        properties!![id] = property
    }

    fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
        implementation = Implementation().apply {
            this.operationHost = operationHost!!
            this.timeout = timeout
        }
    }

    fun artifact(id: String, type: String, file: String) {
        if (artifacts == null)
            artifacts = hashMapOf()
        artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
    }

    fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
        if (artifacts == null)
            artifacts = hashMapOf()
        artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
    }

    fun input(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
        if (inputs == null)
            inputs = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
        inputs!![id] = property.build()
    }

    fun input(
        id: String,
        type: String,
        required: Boolean,
        expression: Any,
        description: String? = "",
        block: DSLPropertyDefinitionBuilder.() -> Unit
    ) {
        if (inputs == null)
            inputs = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
            .apply(block).build()
        inputs!![id] = property
    }

    fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
        if (outputs == null)
            outputs = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
        outputs!![id] = property.build()
    }

    fun output(
        id: String,
        type: String,
        required: Boolean,
        expression: Any,
        description: String? = "",
        block: DSLPropertyDefinitionBuilder.() -> Unit
    ) {
        if (outputs == null)
            outputs = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
            .apply(block).build()
        outputs!![id] = property
    }

    fun build(): DSLComponent {
        dslComponent.id = id
        dslComponent.type = type
        dslComponent.version = version
        dslComponent.description = description
        dslComponent.attributes = attributes
        dslComponent.properties = properties
        dslComponent.implementation = implementation
        dslComponent.artifacts = artifacts
        dslComponent.inputs = inputs
        dslComponent.outputs = outputs

        return dslComponent
    }
}

class DSLRegistryComponentBuilder(
    private val id: String,
    private val type: String,
    private val version: String,
    private val interfaceName: String,
    private val description: String
) {

    private val dslComponent = DSLRegistryComponent()
    var properties: MutableMap<String, JsonNode>? = null

    var artifacts: MutableMap<String, ArtifactDefinition>? = null
    var implementation: Implementation? = null
    var inputs: MutableMap<String, JsonNode>? = null
    var outputs: MutableMap<String, JsonNode>? = null

    fun property(id: String, expression: Any) {
        if (properties == null)
            properties = hashMapOf()
        properties!![id] = expression.asJsonType()
    }

    fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
        implementation = Implementation().apply {
            this.operationHost = operationHost!!
            this.timeout = timeout
        }
    }

    fun artifact(id: String, type: String, file: String) {
        if (artifacts == null)
            artifacts = hashMapOf()
        artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
    }

    fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
        if (artifacts == null)
            artifacts = hashMapOf()
        artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
    }

    fun input(id: String, expression: Any) {
        if (inputs == null)
            inputs = hashMapOf()
        inputs!![id] = expression.asJsonType()
    }

    fun output(id: String, expression: Any) {
        if (outputs == null)
            outputs = hashMapOf()
        outputs!![id] = expression.asJsonType()
    }

    fun build(): DSLRegistryComponent {
        dslComponent.id = id
        dslComponent.type = type
        dslComponent.version = version
        dslComponent.interfaceName = interfaceName
        dslComponent.description = description
        dslComponent.properties = properties
        dslComponent.implementation = implementation
        dslComponent.artifacts = artifacts
        dslComponent.inputs = inputs
        dslComponent.outputs = outputs
        return dslComponent
    }
}

class DSLWorkflowBuilder(private val actionName: String, private val description: String) {

    private val dslWorkflow = DSLWorkflow()
    private var steps: MutableMap<String, Step>? = null
    private var inputs: MutableMap<String, PropertyDefinition>? = null
    private var outputs: MutableMap<String, PropertyDefinition>? = null

    fun input(id: String, type: String, required: Boolean, description: String? = "") {
        if (inputs == null)
            inputs = hashMapOf()
        val property = PropertyDefinitionBuilder(id, type, required, description)
        inputs!![id] = property.build()
    }

    fun input(
        id: String,
        type: String,
        required: Boolean,
        description: String,
        defaultValue: Any?,
        block: PropertyDefinitionBuilder.() -> Unit
    ) {
        if (inputs == null)
            inputs = hashMapOf()
        val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
        if (defaultValue != null)
            property.defaultValue = defaultValue.asJsonType()
        inputs!![id] = property
    }

    fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") {
        if (outputs == null)
            outputs = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
        outputs!![id] = property.build()
    }

    fun output(
        id: String,
        type: String,
        required: Boolean,
        expression: Any,
        description: String? = "",
        block: DSLPropertyDefinitionBuilder.() -> Unit
    ) {
        if (outputs == null)
            outputs = hashMapOf()
        val property = DSLPropertyDefinitionBuilder(id, type, required, expression.asJsonType(), description)
            .apply(block).build()
        outputs!![id] = property
    }

    fun step(id: String, target: String, description: String) {
        if (steps == null)
            steps = hashMapOf()
        steps!![id] = StepBuilder(id, target, description).build()
    }

    fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
        if (steps == null)
            steps = hashMapOf()
        steps!![id] = StepBuilder(id, target, description).apply(block).build()
    }

    fun build(): DSLWorkflow {
        dslWorkflow.actionName = actionName
        dslWorkflow.description = description
        dslWorkflow.inputs = inputs
        dslWorkflow.outputs = outputs
        dslWorkflow.steps = steps!!
        return dslWorkflow
    }
}

class DSLAttributeDefinitionBuilder(
    private val id: String,
    private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
    private val required: Boolean? = false,
    private val expression: JsonNode,
    private val description: String? = ""
) {

    private var attributeDefinition = AttributeDefinition()

    fun entrySchema(entrySchemaType: String) {
        attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
    }

    fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
        attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
    }
    // TODO("Constrains")

    fun defaultValue(defaultValue: JsonNode) {
        attributeDefinition.defaultValue = defaultValue
    }

    fun build(): AttributeDefinition {
        attributeDefinition.id = id
        attributeDefinition.type = type!!
        attributeDefinition.required = required
        attributeDefinition.value = expression
        attributeDefinition.description = description
        return attributeDefinition
    }
}

class DSLPropertyDefinitionBuilder(
    private val id: String,
    private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
    private val required: Boolean? = false,
    private val expression: JsonNode,
    private val description: String? = ""
) {

    private var propertyDefinition: PropertyDefinition = PropertyDefinition()

    fun entrySchema(entrySchemaType: String) {
        propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
    }

    fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
        propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
    }
    // TODO("Constrains")

    fun defaultValue(defaultValue: JsonNode) {
        propertyDefinition.defaultValue = defaultValue
    }

    fun build(): PropertyDefinition {
        propertyDefinition.id = id
        propertyDefinition.type = type!!
        propertyDefinition.required = required
        propertyDefinition.value = expression
        propertyDefinition.description = description
        return propertyDefinition
    }
}