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.blueprintsprocessor.services.workflow
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
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> {
38 private val log = LoggerFactory.getLogger(BluePrintWorkflowExecutionServiceImpl::class.java)!!
40 override suspend fun executeBluePrintWorkflow(
41 bluePrintRuntimeService: BluePrintRuntimeService<*>,
42 executionServiceInput: ExecutionServiceInput,
43 properties: MutableMap<String, Any>
44 ): ExecutionServiceOutput {
46 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
48 val workflowName = executionServiceInput.actionIdentifiers.actionName
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!")
55 val input = executionServiceInput.payload.get("$workflowName-request")
56 bluePrintRuntimeService.assignWorkflowInputs(workflowName, input)
58 val workflow = bluePrintContext.workflowByName(workflowName)
60 val steps = workflow.steps ?: throw BluePrintProcessorException("could't get steps for workflow($workflowName)")
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)
67 // Get the DG Node Template
68 val nodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(workflowName)
70 val derivedFrom = bluePrintContext.nodeTemplateNodeType(nodeTemplateName).derivedFrom
72 log.info("Executing workflow($workflowName) NodeTemplate($nodeTemplateName), derived from($derivedFrom)")
73 /** Return ExecutionServiceOutput based on DG node or Component Node */
75 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
76 componentWorkflowExecutionService
77 .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
79 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
80 dgWorkflowExecutionService
81 .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
84 throw BluePrintProcessorException(
85 "couldn't execute workflow($workflowName) step mapped " +
86 "to node template($nodeTemplateName) derived from($derivedFrom)"
91 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
92 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
94 // Resolve Workflow Outputs
95 var workflowOutputs: MutableMap<String, JsonNode>? = null
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().errors.add(it) }
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()
109 return executionServiceOutput