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 kotlinx.coroutines.CompletableDeferred
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.blueprintsprocessor.core.api.data.Status
23 import org.onap.ccsdk.cds.controllerblueprints.core.*
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.EdgeLabel
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.Graph
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.*
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory
29 import org.springframework.context.annotation.Scope
30 import org.springframework.stereotype.Service
32 @Service("imperativeWorkflowExecutionService")
33 class ImperativeWorkflowExecutionService(
34 private val imperativeBluePrintWorkflowService: BluePrintWorkFlowService<ExecutionServiceInput, ExecutionServiceOutput>)
35 : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput> {
37 override suspend fun executeBluePrintWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>,
38 executionServiceInput: ExecutionServiceInput,
39 properties: MutableMap<String, Any>): ExecutionServiceOutput {
41 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
43 val workflowName = executionServiceInput.actionIdentifiers.actionName
45 val graph = bluePrintContext.workflowByName(workflowName).asGraph()
47 return imperativeBluePrintWorkflowService.executeWorkflow(graph, bluePrintRuntimeService,
48 executionServiceInput)
53 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
54 open class ImperativeBluePrintWorkflowService(private val nodeTemplateExecutionService: NodeTemplateExecutionService)
55 : AbstractBluePrintWorkFlowService<ExecutionServiceInput, ExecutionServiceOutput>() {
56 val log = logger(ImperativeBluePrintWorkflowService::class)
58 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
59 lateinit var executionServiceInput: ExecutionServiceInput
60 lateinit var workflowName: String
62 override suspend fun executeWorkflow(graph: Graph, bluePrintRuntimeService: BluePrintRuntimeService<*>,
63 input: ExecutionServiceInput): ExecutionServiceOutput {
65 this.bluePrintRuntimeService = bluePrintRuntimeService
66 this.executionServiceInput = input
67 this.workflowName = this.executionServiceInput.actionIdentifiers.actionName
68 this.workflowId = bluePrintRuntimeService.id()
69 val output = CompletableDeferred<ExecutionServiceOutput>()
70 val startMessage = WorkflowExecuteMessage(input, output)
71 val workflowActor = workflowActor()
72 if (!workflowActor.isClosedForSend) {
73 workflowActor.send(startMessage)
75 throw BluePrintProcessorException("workflow($workflowActor) actor is closed")
80 override suspend fun initializeWorkflow(input: ExecutionServiceInput): EdgeLabel {
81 return EdgeLabel.SUCCESS
84 override suspend fun prepareWorkflowOutput(): ExecutionServiceOutput {
85 val wfStatus = Status().apply {
86 if (exceptions.isNotEmpty()) {
88 val errorMessage = it.message ?: ""
89 bluePrintRuntimeService.getBluePrintError().addError(errorMessage)
90 log.error("workflow($workflowId) exception :", it)
92 message = BluePrintConstants.STATUS_FAILURE
94 message = BluePrintConstants.STATUS_SUCCESS
97 return ExecutionServiceOutput().apply {
98 commonHeader = executionServiceInput.commonHeader
99 actionIdentifiers = executionServiceInput.actionIdentifiers
104 override suspend fun prepareNodeExecutionMessage(node: Graph.Node)
105 : NodeExecuteMessage<ExecutionServiceInput, ExecutionServiceOutput> {
106 val nodeOutput = ExecutionServiceOutput().apply {
107 commonHeader = executionServiceInput.commonHeader
108 actionIdentifiers = executionServiceInput.actionIdentifiers
110 return NodeExecuteMessage(node, executionServiceInput, nodeOutput)
113 override suspend fun prepareNodeSkipMessage(node: Graph.Node)
114 : NodeSkipMessage<ExecutionServiceInput, ExecutionServiceOutput> {
115 val nodeOutput = ExecutionServiceOutput().apply {
116 commonHeader = executionServiceInput.commonHeader
117 actionIdentifiers = executionServiceInput.actionIdentifiers
119 return NodeSkipMessage(node, executionServiceInput, nodeOutput)
122 override suspend fun executeNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
123 nodeOutput: ExecutionServiceOutput): EdgeLabel {
124 log.info("Executing workflow($workflowName[${this.workflowId}])'s step($${node.id})")
125 val step = bluePrintRuntimeService.bluePrintContext().workflowStepByName(this.workflowName, node.id)
126 checkNotEmpty(step.target) { "couldn't get step target for workflow(${this.workflowName})'s step(${node.id})" }
127 val nodeTemplateName = step.target!!
128 /** execute node template */
129 val executionServiceOutput = nodeTemplateExecutionService
130 .executeNodeTemplate(bluePrintRuntimeService, nodeTemplateName, nodeInput)
132 return when (executionServiceOutput.status.message) {
133 BluePrintConstants.STATUS_FAILURE -> EdgeLabel.FAILURE
134 else -> EdgeLabel.SUCCESS
138 override suspend fun skipNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
139 nodeOutput: ExecutionServiceOutput): EdgeLabel {
140 return EdgeLabel.SUCCESS
143 override suspend fun cancelNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
144 nodeOutput: ExecutionServiceOutput): EdgeLabel {
145 TODO("not implemented")
148 override suspend fun restartNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
149 nodeOutput: ExecutionServiceOutput): EdgeLabel {
150 TODO("not implemented")