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