Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / cds / blueprintsprocessor / services / workflow / BluePrintWorkflowExecutionServiceImpl.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 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.interfaces.BluePrintWorkflowExecutionService
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
25 import org.slf4j.LoggerFactory
26 import org.springframework.stereotype.Service
27
28 @Service("bluePrintWorkflowExecutionService")
29 open class BluePrintWorkflowExecutionServiceImpl(
30         private val componentWorkflowExecutionService: ComponentWorkflowExecutionService,
31         private val dgWorkflowExecutionService: DGWorkflowExecutionService
32 ) : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput> {
33
34     private val log = LoggerFactory.getLogger(BluePrintWorkflowExecutionServiceImpl::class.java)!!
35
36     override suspend fun executeBluePrintWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>,
37                                                   executionServiceInput: ExecutionServiceInput,
38                                                   properties: MutableMap<String, Any>): ExecutionServiceOutput {
39
40         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
41
42         val workflowName = executionServiceInput.actionIdentifiers.actionName
43
44         // Get the DG Node Template
45         val nodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(workflowName)
46
47         val derivedFrom = bluePrintContext.nodeTemplateNodeType(nodeTemplateName).derivedFrom
48
49         log.info("Executing workflow($workflowName) NodeTemplate($nodeTemplateName), derived from($derivedFrom)")
50
51         val executionServiceOutput: ExecutionServiceOutput = when {
52             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
53                 componentWorkflowExecutionService
54                         .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
55             }
56             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_DG, true) -> {
57                 dgWorkflowExecutionService
58                         .executeBluePrintWorkflow(bluePrintRuntimeService, executionServiceInput, properties)
59             }
60             else -> {
61                 throw BluePrintProcessorException("couldn't execute workflow($workflowName) step mapped " +
62                         "to node template($nodeTemplateName) derived from($derivedFrom)")
63             }
64         }
65
66         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
67         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
68         // TODO("Populate Response Payload and status")
69         return executionServiceOutput
70     }
71
72 }