Merge "CDS-Listener should send response back to SDC"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / workflow / NodeTemplateExecutionService.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 com.fasterxml.jackson.databind.JsonNode
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.StepData
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
27 import org.slf4j.LoggerFactory
28 import org.springframework.context.ApplicationContext
29 import org.springframework.stereotype.Service
30
31 @Service
32 open class NodeTemplateExecutionService(private val applicationContext: ApplicationContext) {
33
34     private val log = LoggerFactory.getLogger(NodeTemplateExecutionService::class.java)!!
35
36     suspend fun executeNodeTemplate(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
37                                     executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
38         // Get the Blueprint Context
39         val blueprintContext = bluePrintRuntimeService.bluePrintContext()
40         // Get the Component Name, NodeTemplate type is mapped to Component Name
41         val componentName = blueprintContext.nodeTemplateByName(nodeTemplateName).type
42
43         val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
44
45         val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
46
47         log.info("executing node template($nodeTemplateName) component($componentName) " +
48                 "interface($interfaceName) operation($operationName)")
49
50         // Get the Component Instance
51         val plugin = applicationContext.getBean(componentName, AbstractComponentFunction::class.java)
52         // Set the Blueprint Service
53         plugin.bluePrintRuntimeService = bluePrintRuntimeService
54         plugin.stepName = nodeTemplateName
55
56         // Parent request shouldn't tamper, so need to clone the request and send to the actual component.
57         val clonedExecutionServiceInput = ExecutionServiceInput().apply {
58             commonHeader = executionServiceInput.commonHeader
59             actionIdentifiers = executionServiceInput.actionIdentifiers
60             payload = executionServiceInput.payload
61         }
62
63         // Populate Step Meta Data
64         val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
65         stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, nodeTemplateName)
66         stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, interfaceName)
67         stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, operationName)
68         val stepInputData = StepData().apply {
69             name = nodeTemplateName
70             properties = stepInputs
71         }
72         clonedExecutionServiceInput.stepData = stepInputData
73
74         // Get the Request from the Context and Set to the Function Input and Invoke the function
75         return plugin.applyNB(clonedExecutionServiceInput)
76     }
77
78 }