7a59a0ab272c3de023feba0d3f001bb77e9079a3
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import kotlinx.coroutines.async
21 import kotlinx.coroutines.coroutineScope
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
25 import org.onap.ccsdk.apps.blueprintsprocessor.services.workflow.BlueprintSvcLogicContext
26 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonNode
28 import org.onap.ccsdk.apps.controllerblueprints.core.putJsonElement
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException
31 import org.onap.ccsdk.sli.core.sli.SvcLogicNode
32 import org.onap.ccsdk.sli.core.sli.provider.base.ExecuteNodeExecutor
33 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicExpressionResolver
34 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase
35 import org.slf4j.LoggerFactory
36 import org.springframework.beans.factory.annotation.Autowired
37 import org.springframework.context.ApplicationContext
38 import org.springframework.stereotype.Service
39
40 @Service
41 open class ComponentExecuteNodeExecutor : ExecuteNodeExecutor() {
42
43     private val log = LoggerFactory.getLogger(ComponentExecuteNodeExecutor::class.java)
44
45     @Autowired
46     private lateinit var context: ApplicationContext
47
48     fun getComponentFunction(pluginName: String): AbstractComponentFunction {
49         return context.getBean(pluginName, AbstractComponentFunction::class.java)
50     }
51
52     @Throws(SvcLogicException::class)
53     override fun execute(svc: SvcLogicServiceBase, node: SvcLogicNode, svcLogicContext: SvcLogicContext)
54             : SvcLogicNode = runBlocking {
55         coroutineScope {
56             val job = async { executeAsy(svc, node, svcLogicContext) }
57             job.await()
58         }
59     }
60
61
62     private fun executeAsy(svc: SvcLogicServiceBase, node: SvcLogicNode, svcLogicContext: SvcLogicContext): SvcLogicNode {
63
64         var outValue: String
65
66         val ctx = svcLogicContext as BlueprintSvcLogicContext
67
68         val nodeTemplateName = SvcLogicExpressionResolver.evaluate(node.getAttribute("plugin"), node, ctx)
69
70         try {
71             // Get the Blueprint Context
72             val blueprintContext = ctx.getBluePrintService().bluePrintContext()
73             // Get the Component Name, NodeTemplate type is mapped to Component Name
74             val componentName = blueprintContext.nodeTemplateByName(nodeTemplateName).type
75
76             val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
77
78             val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
79
80             log.info("executing node template($nodeTemplateName) component($componentName) interface($interfaceName) operation($operationName)")
81             // Get the Component Instance
82             val plugin = this.getComponentFunction(componentName)
83             // Set the Blueprint Service
84             plugin.bluePrintRuntimeService = ctx.getBluePrintService()
85             plugin.stepName = nodeTemplateName
86
87             val executionInput = ctx.getRequest() as ExecutionServiceInput
88
89             // Populate Step Meta Data
90             val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
91             stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, nodeTemplateName)
92             stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, interfaceName)
93             stepInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, operationName)
94
95             plugin.bluePrintRuntimeService.put("$nodeTemplateName-step-inputs", stepInputs.asJsonNode())
96
97             // Get the Request from the Context and Set to the Function Input and Invoke the function
98             val executionOutput = plugin.apply(executionInput)
99
100             ctx.setResponse(executionOutput)
101
102             outValue = executionOutput.status.message
103             ctx.status = executionOutput.status.message
104
105         } catch (e: Exception) {
106             this.log.error("Could not execute plugin($nodeTemplateName) : ", e)
107             outValue = "failure"
108             ctx.status = "failure"
109         }
110
111         return this.getNextNode(node, outValue)
112     }
113 }