Merge "Passing Option from Search DB to metadata"
[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.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.slf4j.LoggerFactory
29 import org.springframework.stereotype.Service
30
31 @Service
32 open class NodeTemplateExecutionService {
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
41         val nodeTemplate = blueprintContext.nodeTemplateByName(nodeTemplateName)
42         // Get the Component Name, NodeTemplate type is mapped to Component Name
43         val componentName = nodeTemplate.type
44
45         val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
46
47         val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
48
49         val nodeTemplateImplementation = blueprintContext
50                 .nodeTemplateOperationImplementation(nodeTemplateName, interfaceName, operationName)
51
52         val timeout: Int = nodeTemplateImplementation?.timeout ?: 180
53
54         log.info("executing node template($nodeTemplateName) component($componentName) " +
55                 "interface($interfaceName) operation($operationName) with timeout($timeout) sec.")
56
57         // Get the Component Instance
58         val plugin = BluePrintDependencyService.instance<AbstractComponentFunction>(componentName)
59         // Set the Blueprint Service
60         plugin.bluePrintRuntimeService = bluePrintRuntimeService
61         plugin.stepName = nodeTemplateName
62
63         // Parent request shouldn't tamper, so need to clone the request and send to the actual component.
64         val clonedExecutionServiceInput = ExecutionServiceInput().apply {
65             commonHeader = executionServiceInput.commonHeader
66             actionIdentifiers = executionServiceInput.actionIdentifiers
67             payload = executionServiceInput.payload
68         }
69
70         // Populate Step Meta Data
71         val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
72         stepInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = nodeTemplateName.asJsonPrimitive()
73         stepInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = interfaceName.asJsonPrimitive()
74         stepInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = operationName.asJsonPrimitive()
75         stepInputs[BluePrintConstants.PROPERTY_CURRENT_TIMEOUT] = timeout.asJsonPrimitive()
76         val stepInputData = StepData().apply {
77             name = nodeTemplateName
78             properties = stepInputs
79         }
80         clonedExecutionServiceInput.stepData = stepInputData
81
82         // Get the Request from the Context and Set to the Function Input and Invoke the function
83         return plugin.applyNB(clonedExecutionServiceInput)
84     }
85
86 }