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.common.api.EventType
24 import org.onap.ccsdk.cds.controllerblueprints.core.*
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.EdgeLabel
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.Graph
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.*
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.annotation.Scope
31 import org.springframework.stereotype.Service
33 @Service("imperativeWorkflowExecutionService")
34 class ImperativeWorkflowExecutionService(
35 private val imperativeBluePrintWorkflowService: BluePrintWorkFlowService<ExecutionServiceInput, ExecutionServiceOutput>)
36 : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput> {
38 override suspend fun executeBluePrintWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>,
39 executionServiceInput: ExecutionServiceInput,
40 properties: MutableMap<String, Any>): ExecutionServiceOutput {
42 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
44 val workflowName = executionServiceInput.actionIdentifiers.actionName
46 val graph = bluePrintContext.workflowByName(workflowName).asGraph()
48 return imperativeBluePrintWorkflowService.executeWorkflow(graph, bluePrintRuntimeService,
49 executionServiceInput)
54 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
55 open class ImperativeBluePrintWorkflowService(private val nodeTemplateExecutionService: NodeTemplateExecutionService)
56 : AbstractBluePrintWorkFlowService<ExecutionServiceInput, ExecutionServiceOutput>() {
57 val log = logger(ImperativeBluePrintWorkflowService::class)
59 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
60 lateinit var executionServiceInput: ExecutionServiceInput
61 lateinit var workflowName: String
63 override suspend fun executeWorkflow(graph: Graph, bluePrintRuntimeService: BluePrintRuntimeService<*>,
64 input: ExecutionServiceInput): ExecutionServiceOutput {
66 this.bluePrintRuntimeService = bluePrintRuntimeService
67 this.executionServiceInput = input
68 this.workflowName = this.executionServiceInput.actionIdentifiers.actionName
69 this.workflowId = bluePrintRuntimeService.id()
70 val output = CompletableDeferred<ExecutionServiceOutput>()
71 val startMessage = WorkflowExecuteMessage(input, output)
72 val workflowActor = workflowActor()
73 if (!workflowActor.isClosedForSend) {
74 workflowActor.send(startMessage)
76 throw BluePrintProcessorException("workflow($workflowActor) actor is closed")
81 override suspend fun initializeWorkflow(input: ExecutionServiceInput): EdgeLabel {
82 return EdgeLabel.SUCCESS
85 override suspend fun prepareWorkflowOutput(): ExecutionServiceOutput {
86 val wfStatus = Status().apply {
87 if (exceptions.isNotEmpty()) {
89 val errorMessage = it.message ?: ""
90 bluePrintRuntimeService.getBluePrintError().addError(errorMessage)
91 log.error("workflow($workflowId) exception :", it)
93 message = BluePrintConstants.STATUS_FAILURE
95 message = BluePrintConstants.STATUS_SUCCESS
97 eventType = EventType.EVENT_COMPONENT_EXECUTED.name
99 return ExecutionServiceOutput().apply {
100 commonHeader = executionServiceInput.commonHeader
101 actionIdentifiers = executionServiceInput.actionIdentifiers
106 override suspend fun prepareNodeExecutionMessage(node: Graph.Node)
107 : NodeExecuteMessage<ExecutionServiceInput, ExecutionServiceOutput> {
108 val nodeOutput = ExecutionServiceOutput().apply {
109 commonHeader = executionServiceInput.commonHeader
110 actionIdentifiers = executionServiceInput.actionIdentifiers
112 return NodeExecuteMessage(node, executionServiceInput, nodeOutput)
115 override suspend fun prepareNodeSkipMessage(node: Graph.Node)
116 : NodeSkipMessage<ExecutionServiceInput, ExecutionServiceOutput> {
117 val nodeOutput = ExecutionServiceOutput().apply {
118 commonHeader = executionServiceInput.commonHeader
119 actionIdentifiers = executionServiceInput.actionIdentifiers
121 return NodeSkipMessage(node, executionServiceInput, nodeOutput)
124 override suspend fun executeNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
125 nodeOutput: ExecutionServiceOutput): EdgeLabel {
126 log.info("Executing workflow($workflowName[${this.workflowId}])'s step($${node.id})")
127 val step = bluePrintRuntimeService.bluePrintContext().workflowStepByName(this.workflowName, node.id)
128 checkNotEmpty(step.target) { "couldn't get step target for workflow(${this.workflowName})'s step(${node.id})" }
129 val nodeTemplateName = step.target!!
130 /** execute node template */
131 val executionServiceOutput = nodeTemplateExecutionService
132 .executeNodeTemplate(bluePrintRuntimeService, nodeTemplateName, nodeInput)
134 return when (executionServiceOutput.status.message) {
135 BluePrintConstants.STATUS_FAILURE -> EdgeLabel.FAILURE
136 else -> EdgeLabel.SUCCESS
140 override suspend fun skipNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
141 nodeOutput: ExecutionServiceOutput): EdgeLabel {
142 return EdgeLabel.SUCCESS
145 override suspend fun cancelNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
146 nodeOutput: ExecutionServiceOutput): EdgeLabel {
147 TODO("not implemented")
150 override suspend fun restartNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
151 nodeOutput: ExecutionServiceOutput): EdgeLabel {
152 TODO("not implemented")