Merge "Store resolution and expose with REST API"
[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.data.Activity
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
24
25 class WorkflowBuilder(private val id: String, private val description: String) {
26
27     private var workflow = Workflow()
28     private var steps: MutableMap<String, Step>? = null
29     private var inputs: MutableMap<String, PropertyDefinition>? = null
30     private var outputs: MutableMap<String, PropertyDefinition>? = null
31
32     // Used Internally
33     fun nodeTemplateStep(nodeTemplateName: String, description: String) {
34         step(nodeTemplateName, nodeTemplateName, "$description step")
35     }
36
37     fun step(id: String, target: String, description: String) {
38         if (steps == null)
39             steps = hashMapOf()
40         steps!![id] = StepBuilder(id, target, description).build()
41     }
42
43     fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
44         if (steps == null)
45             steps = hashMapOf()
46         steps!![id] = StepBuilder(id, target, description).apply(block).build()
47     }
48
49     fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
50         inputs = PropertiesDefinitionBuilder().apply(block).build()
51     }
52
53     fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
54         outputs = PropertiesDefinitionBuilder().apply(block).build()
55     }
56
57     fun build(): Workflow {
58         workflow.id = id
59         workflow.description = description
60         workflow.steps = steps
61         workflow.inputs = inputs
62         workflow.outputs = outputs
63         return workflow
64     }
65
66 }
67
68 class StepBuilder(private val id: String, private val target: String,
69                   private val description: String) {
70
71     private var step = Step()
72     private var activities: ArrayList<Activity> = arrayListOf()
73     private var onSuccess: ArrayList<String>? = null
74     private var onFailure: ArrayList<String>? = null
75
76     fun activity(callOperation: String) {
77         val activity = Activity()
78         activity.callOperation = callOperation
79         activities.add(activity)
80     }
81
82     fun success(vararg successTargets: String) {
83         if (onSuccess == null)
84             onSuccess = arrayListOf()
85         successTargets.forEach {
86             onSuccess!!.add(it)
87         }
88     }
89
90     fun failure(vararg failureTargets: String) {
91         if (onFailure == null)
92             onFailure = arrayListOf()
93         failureTargets.forEach {
94             onFailure!!.add(it)
95         }
96     }
97
98     fun build(): Step {
99         step.id = id
100         step.target = target
101         // Add Default Activity, Assumption is only one Operation
102         activity(".${BluePrintConstants.DEFAULT_STEP_OPERATION}")
103         step.description = description
104         step.activities = activities
105         step.onSuccess = onSuccess
106         step.onFailure = onFailure
107         return step
108     }
109 }