Add missing k8s-rb-instance-release-name.json
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BlueprintWorkflowDSLBuilder.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
20 import org.onap.ccsdk.cds.controllerblueprints.core.asPropertyDefinitionMap
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.Activity
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
25 import kotlin.reflect.KClass
26
27 class WorkflowBuilder(private val id: String, private val description: String) {
28
29     private var workflow = Workflow()
30     private var steps: MutableMap<String, Step>? = null
31     private var inputs: MutableMap<String, PropertyDefinition>? = null
32     private var outputs: MutableMap<String, PropertyDefinition>? = null
33
34     // Used Internally
35     fun nodeTemplateStep(nodeTemplateName: String, description: String) {
36         step(nodeTemplateName, nodeTemplateName, "$description step")
37     }
38
39     fun step(id: String, target: String, description: String) {
40         if (steps == null)
41             steps = hashMapOf()
42         steps!![id] = StepBuilder(id, target, description).build()
43     }
44
45     fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
46         if (steps == null)
47             steps = hashMapOf()
48         steps!![id] = StepBuilder(id, target, description).apply(block).build()
49     }
50
51     fun inputs(kClazz: KClass<*>) {
52         inputs = kClazz.asPropertyDefinitionMap()
53     }
54
55     fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
56         inputs = PropertiesDefinitionBuilder().apply(block).build()
57     }
58
59     fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
60         outputs = PropertiesDefinitionBuilder().apply(block).build()
61     }
62
63     fun outputs(kClazz: KClass<*>) {
64         outputs = kClazz.asPropertyDefinitionMap()
65     }
66
67     fun build(): Workflow {
68         workflow.id = id
69         workflow.description = description
70         workflow.steps = steps
71         workflow.inputs = inputs
72         workflow.outputs = outputs
73         return workflow
74     }
75 }
76
77 class StepBuilder(
78     private val id: String,
79     private val target: String,
80     private val description: String
81 ) {
82
83     private var step = Step()
84     private var activities: ArrayList<Activity> = arrayListOf()
85     private var onSuccess: ArrayList<String>? = null
86     private var onFailure: ArrayList<String>? = null
87
88     fun activity(callOperation: String) {
89         val activity = Activity()
90         activity.callOperation = callOperation
91         activities.add(activity)
92     }
93
94     fun success(vararg successTargets: String) {
95         if (onSuccess == null)
96             onSuccess = arrayListOf()
97         successTargets.forEach {
98             onSuccess!!.add(it)
99         }
100     }
101
102     fun failure(vararg failureTargets: String) {
103         if (onFailure == null)
104             onFailure = arrayListOf()
105         failureTargets.forEach {
106             onFailure!!.add(it)
107         }
108     }
109
110     fun build(): Step {
111         step.id = id
112         step.target = target
113         // Add Default Activity, Assumption is only one Operation
114         activity(".${BlueprintConstants.DEFAULT_STEP_OPERATION}")
115         step.description = description
116         step.activities = activities
117         step.onSuccess = onSuccess
118         step.onFailure = onFailure
119         return step
120     }
121 }