fcf0558c73c4be6495f4060daff9a804183c752a
[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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
26 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
27 import org.slf4j.LoggerFactory
28 import org.springframework.stereotype.Service
29
30 @Service("bluePrintWorkflowExecutionService")
31 open class BluePrintWorkflowExecutionServiceImpl(
32     private val componentWorkflowExecutionService: ComponentWorkflowExecutionService,
33     private val dgWorkflowExecutionService: DGWorkflowExecutionService
34 ) : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput> {
35
36     private val log = LoggerFactory.getLogger(BluePrintWorkflowExecutionServiceImpl::class.java)!!
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         // Assign Workflow inputs
47         //check if request structure exists
48         if (!executionServiceInput.payload.has("$workflowName-request")) {
49             throw BluePrintProcessorException("Input request missing the expected '$workflowName-request' block!")
50         }
51         val input = executionServiceInput.payload.get("$workflowName-request")
52         bluePrintRuntimeService.assignWorkflowInputs(workflowName, input)
53
54         // Get the DG Node Template
55         val nodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(workflowName)
56
57         val derivedFrom = bluePrintContext.nodeTemplateNodeType(nodeTemplateName).derivedFrom
58
59         log.info("Executing workflow($workflowName) NodeTemplate($nodeTemplateName), derived from($derivedFrom)")
60
61         val executionServiceOutput: ExecutionServiceOutput = when {
62             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
63                 componentWorkflowExecutionService
64                     .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
65             }
66             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
67                 dgWorkflowExecutionService
68                     .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
69             }
70             else -> {
71                 throw BluePrintProcessorException("couldn't execute workflow($workflowName) step mapped " +
72                         "to node template($nodeTemplateName) derived from($derivedFrom)")
73             }
74         }
75
76         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
77         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
78         // Resolve Workflow Outputs
79         val workflowOutputs = bluePrintRuntimeService.resolveWorkflowOutputs(workflowName)
80
81         // Set the Response Payload
82         executionServiceOutput.payload = JacksonUtils.objectMapper.createObjectNode()
83         executionServiceOutput.payload.set("$workflowName-response", workflowOutputs.asObjectNode())
84         return executionServiceOutput
85     }
86
87 }