2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
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
27 class WorkflowBuilder(private val id: String, private val description: String) {
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
35 fun nodeTemplateStep(nodeTemplateName: String, description: String) {
36 step(nodeTemplateName, nodeTemplateName, "$description step")
39 fun step(id: String, target: String, description: String) {
42 steps!![id] = StepBuilder(id, target, description).build()
45 fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) {
48 steps!![id] = StepBuilder(id, target, description).apply(block).build()
51 fun inputs(kClazz: KClass<*>) {
52 inputs = kClazz.asPropertyDefinitionMap()
55 fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
56 inputs = PropertiesDefinitionBuilder().apply(block).build()
59 fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
60 outputs = PropertiesDefinitionBuilder().apply(block).build()
63 fun outputs(kClazz: KClass<*>) {
64 outputs = kClazz.asPropertyDefinitionMap()
67 fun build(): Workflow {
69 workflow.description = description
70 workflow.steps = steps
71 workflow.inputs = inputs
72 workflow.outputs = outputs
78 class StepBuilder(private val id: String, private val target: String,
79 private val description: String) {
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
86 fun activity(callOperation: String) {
87 val activity = Activity()
88 activity.callOperation = callOperation
89 activities.add(activity)
92 fun success(vararg successTargets: String) {
93 if (onSuccess == null)
94 onSuccess = arrayListOf()
95 successTargets.forEach {
100 fun failure(vararg failureTargets: String) {
101 if (onFailure == null)
102 onFailure = arrayListOf()
103 failureTargets.forEach {
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