Merge "Resource resolution should return a string"
[ccsdk/cds.git] / ms / controllerblueprints / modules / 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
78 class StepBuilder(private val id: String, private val target: String,
79                   private val description: String) {
80
81     private var step = Step()
82     private var activities: ArrayList<Activity> = arrayListOf()
83     private var onSuccess: ArrayList<String>? = null
84     private var onFailure: ArrayList<String>? = null
85
86     fun activity(callOperation: String) {
87         val activity = Activity()
88         activity.callOperation = callOperation
89         activities.add(activity)
90     }
91
92     fun success(vararg successTargets: String) {
93         if (onSuccess == null)
94             onSuccess = arrayListOf()
95         successTargets.forEach {
96             onSuccess!!.add(it)
97         }
98     }
99
100     fun failure(vararg failureTargets: String) {
101         if (onFailure == null)
102             onFailure = arrayListOf()
103         failureTargets.forEach {
104             onFailure!!.add(it)
105         }
106     }
107
108     fun build(): Step {
109         step.id = id
110         step.target = target
111         // Add Default Activity, Assumption is only one Operation
112         activity(".${BluePrintConstants.DEFAULT_STEP_OPERATION}")
113         step.description = description
114         step.activities = activities
115         step.onSuccess = onSuccess
116         step.onFailure = onFailure
117         return step
118     }
119 }