aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BlueprintWorkflowDSLBuilder.kt
blob: 0de1a510ccba6f32b9f368b82e80ec5e0832e40f (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
/*
 *  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 org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.asPropertyDefinitionMap
import org.onap.ccsdk.cds.controllerblueprints.core.data.Activity
import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
import kotlin.reflect.KClass

class WorkflowBuilder(private val id: String, private val description: String) {

    private var workflow = Workflow()
    private var steps: MutableMap<String, Step>? = null
    private var inputs: MutableMap<String, PropertyDefinition>? = null
    private var outputs: MutableMap<String, PropertyDefinition>? = null

    // Used Internally
    fun nodeTemplateStep(nodeTemplateName: String, description: String) {
        step(nodeTemplateName, nodeTemplateName, "$description step")
    }

    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 inputs(kClazz: KClass<*>) {
        inputs = kClazz.asPropertyDefinitionMap()
    }

    fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
        inputs = PropertiesDefinitionBuilder().apply(block).build()
    }

    fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
        outputs = PropertiesDefinitionBuilder().apply(block).build()
    }

    fun outputs(kClazz: KClass<*>) {
        outputs = kClazz.asPropertyDefinitionMap()
    }

    fun build(): Workflow {
        workflow.id = id
        workflow.description = description
        workflow.steps = steps
        workflow.inputs = inputs
        workflow.outputs = outputs
        return workflow
    }
}

class StepBuilder(
    private val id: String,
    private val target: String,
    private val description: String
) {

    private var step = Step()
    private var activities: ArrayList<Activity> = arrayListOf()
    private var onSuccess: ArrayList<String>? = null
    private var onFailure: ArrayList<String>? = null

    fun activity(callOperation: String) {
        val activity = Activity()
        activity.callOperation = callOperation
        activities.add(activity)
    }

    fun success(vararg successTargets: String) {
        if (onSuccess == null)
            onSuccess = arrayListOf()
        successTargets.forEach {
            onSuccess!!.add(it)
        }
    }

    fun failure(vararg failureTargets: String) {
        if (onFailure == null)
            onFailure = arrayListOf()
        failureTargets.forEach {
            onFailure!!.add(it)
        }
    }

    fun build(): Step {
        step.id = id
        step.target = target
        // Add Default Activity, Assumption is only one Operation
        activity(".${BlueprintConstants.DEFAULT_STEP_OPERATION}")
        step.description = description
        step.activities = activities
        step.onSuccess = onSuccess
        step.onFailure = onFailure
        return step
    }
}