Modify workflow execution service options.
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / workflow / BluePrintWorkflowExecutionServiceImpl.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.blueprintsprocessor.services.workflow
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.slf4j.LoggerFactory
28 import org.springframework.stereotype.Service
29
30 @Service("bluePrintWorkflowExecutionService")
31 open class BluePrintWorkflowExecutionServiceImpl(
32         private val componentWorkflowExecutionService: ComponentWorkflowExecutionService,
33         private val dgWorkflowExecutionService: DGWorkflowExecutionService,
34         private val imperativeWorkflowExecutionService: ImperativeWorkflowExecutionService
35 ) : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput> {
36
37     private val log = LoggerFactory.getLogger(BluePrintWorkflowExecutionServiceImpl::class.java)!!
38
39     override suspend fun executeBluePrintWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>,
40                                                   executionServiceInput: ExecutionServiceInput,
41                                                   properties: MutableMap<String, Any>): ExecutionServiceOutput {
42
43         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
44
45         val workflowName = executionServiceInput.actionIdentifiers.actionName
46
47         // Assign Workflow inputs
48         //check if request structure exists
49         if (!executionServiceInput.payload.has("$workflowName-request")) {
50             throw BluePrintProcessorException("Input request missing the expected '$workflowName-request' block!")
51         }
52         val input = executionServiceInput.payload.get("$workflowName-request")
53         bluePrintRuntimeService.assignWorkflowInputs(workflowName, input)
54
55         val workflow = bluePrintContext.workflowByName(workflowName)
56
57         val steps = workflow.steps ?: throw BluePrintProcessorException("could't get steps for workflow($workflowName)")
58
59         /** If workflow has multiple steps, then it is imperative workflow */
60         val executionServiceOutput: ExecutionServiceOutput = if (steps.size > 1) {
61             imperativeWorkflowExecutionService
62                     .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
63         } else {
64             // Get the DG Node Template
65             val nodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(workflowName)
66
67             val derivedFrom = bluePrintContext.nodeTemplateNodeType(nodeTemplateName).derivedFrom
68
69             log.info("Executing workflow($workflowName) NodeTemplate($nodeTemplateName), derived from($derivedFrom)")
70             /** Return ExecutionServiceOutput based on DG node or Component Node */
71             when {
72                 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
73                     componentWorkflowExecutionService
74                             .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
75                 }
76                 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
77                     dgWorkflowExecutionService
78                             .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
79                 }
80                 else -> {
81                     throw BluePrintProcessorException("couldn't execute workflow($workflowName) step mapped " +
82                             "to node template($nodeTemplateName) derived from($derivedFrom)")
83                 }
84             }
85         }
86         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
87         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
88         // Resolve Workflow Outputs
89         val workflowOutputs = bluePrintRuntimeService.resolveWorkflowOutputs(workflowName)
90
91         // Set the Response Payload
92         executionServiceOutput.payload = JacksonUtils.objectMapper.createObjectNode()
93         executionServiceOutput.payload.set("$workflowName-response", workflowOutputs.asObjectNode())
94         return executionServiceOutput
95     }
96
97 }