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