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