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