Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / ccsdk / apps / 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.apps.blueprintsprocessor.services.workflow
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import kotlinx.coroutines.Dispatchers
21 import kotlinx.coroutines.withContext
22 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
23 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput
24 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonNode
27 import org.onap.ccsdk.apps.controllerblueprints.core.putJsonElement
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
29 import org.slf4j.LoggerFactory
30 import org.springframework.context.ApplicationContext
31 import org.springframework.stereotype.Service
32
33 @Service
34 open class NodeTemplateExecutionService(private val applicationContext: ApplicationContext) {
35
36     private val log = LoggerFactory.getLogger(NodeTemplateExecutionService::class.java)!!
37
38     suspend fun executeNodeTemplate(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
39                                     executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
40         // Get the Blueprint Context
41         val blueprintContext = bluePrintRuntimeService.bluePrintContext()
42         // Get the Component Name, NodeTemplate type is mapped to Component Name
43         val componentName = blueprintContext.nodeTemplateByName(nodeTemplateName).type
44
45         val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
46
47         val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
48
49         log.info("executing node template($nodeTemplateName) component($componentName) " +
50                 "interface($interfaceName) operation($operationName)")
51
52         // Get the Component Instance
53         val plugin = applicationContext.getBean(componentName, AbstractComponentFunction::class.java)
54         // Set the Blueprint Service
55         plugin.bluePrintRuntimeService = bluePrintRuntimeService
56         plugin.stepName = nodeTemplateName
57
58         // Populate Step Meta Data
59         val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
60         stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, nodeTemplateName)
61         stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, interfaceName)
62         stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, operationName)
63
64         plugin.bluePrintRuntimeService.put("$nodeTemplateName-step-inputs", stepInputs.asJsonNode())
65
66         // Get the Request from the Context and Set to the Function Input and Invoke the function
67         return withContext(Dispatchers.Default) {
68             plugin.apply(executionServiceInput)
69         }
70     }
71
72 }