6bee17f4b6a995101254e3585ea291d5acd13218
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / workflow / ImperativeWorkflowExecutionService.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 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
31
32 @Service("imperativeWorkflowExecutionService")
33 class ImperativeWorkflowExecutionService(
34         private val imperativeBluePrintWorkflowService: BluePrintWorkFlowService<ExecutionServiceInput, ExecutionServiceOutput>)
35     : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput> {
36
37     override suspend fun executeBluePrintWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>,
38                                                   executionServiceInput: ExecutionServiceInput,
39                                                   properties: MutableMap<String, Any>): ExecutionServiceOutput {
40
41         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
42
43         val workflowName = executionServiceInput.actionIdentifiers.actionName
44
45         val graph = bluePrintContext.workflowByName(workflowName).asGraph()
46
47         return imperativeBluePrintWorkflowService.executeWorkflow(graph, bluePrintRuntimeService,
48                 executionServiceInput)
49     }
50 }
51
52 @Service
53 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
54 open class ImperativeBluePrintWorkflowService(private val nodeTemplateExecutionService: NodeTemplateExecutionService)
55     : AbstractBluePrintWorkFlowService<ExecutionServiceInput, ExecutionServiceOutput>() {
56     val log = logger(ImperativeBluePrintWorkflowService::class)
57
58     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
59     lateinit var executionServiceInput: ExecutionServiceInput
60     lateinit var workflowName: String
61
62     override suspend fun executeWorkflow(graph: Graph, bluePrintRuntimeService: BluePrintRuntimeService<*>,
63                                          input: ExecutionServiceInput): ExecutionServiceOutput {
64         this.graph = graph
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)
74         } else {
75             throw BluePrintProcessorException("workflow($workflowActor) actor is closed")
76         }
77         return output.await()
78     }
79
80     override suspend fun initializeWorkflow(input: ExecutionServiceInput): EdgeLabel {
81         return EdgeLabel.SUCCESS
82     }
83
84     override suspend fun prepareWorkflowOutput(): ExecutionServiceOutput {
85         val wfStatus = Status().apply {
86             if (exceptions.isNotEmpty()) {
87                 exceptions.forEach {
88                     val errorMessage = it.message ?: ""
89                     bluePrintRuntimeService.getBluePrintError().addError(errorMessage)
90                     log.error("workflow($workflowId) exception :", it)
91                 }
92                 message = BluePrintConstants.STATUS_FAILURE
93             } else {
94                 message = BluePrintConstants.STATUS_SUCCESS
95             }
96         }
97         return ExecutionServiceOutput().apply {
98             commonHeader = executionServiceInput.commonHeader
99             actionIdentifiers = executionServiceInput.actionIdentifiers
100             status = wfStatus
101         }
102     }
103
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
109         }
110         return NodeExecuteMessage(node, executionServiceInput, nodeOutput)
111     }
112
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
118         }
119         return NodeSkipMessage(node, executionServiceInput, nodeOutput)
120     }
121
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)
131
132         return when (executionServiceOutput.status.message) {
133             BluePrintConstants.STATUS_FAILURE -> EdgeLabel.FAILURE
134             else -> EdgeLabel.SUCCESS
135         }
136     }
137
138     override suspend fun skipNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
139                                   nodeOutput: ExecutionServiceOutput): EdgeLabel {
140         return EdgeLabel.SUCCESS
141     }
142
143     override suspend fun cancelNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
144                                     nodeOutput: ExecutionServiceOutput): EdgeLabel {
145         TODO("not implemented")
146     }
147
148     override suspend fun restartNode(node: Graph.Node, nodeInput: ExecutionServiceInput,
149                                      nodeOutput: ExecutionServiceOutput): EdgeLabel {
150         TODO("not implemented")
151     }
152 }