125a1ff6f94eb55969042820b2651d6313d64cf5
[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 org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
21 import org.onap.ccsdk.apps.blueprintsprocessor.services.workflow.BlueprintSvcLogicContext
22 import org.onap.ccsdk.sli.core.sli.SvcLogicContext
23 import org.onap.ccsdk.sli.core.sli.SvcLogicException
24 import org.onap.ccsdk.sli.core.sli.SvcLogicNode
25 import org.onap.ccsdk.sli.core.sli.provider.ExecuteNodeExecutor
26 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicExpressionResolver
27 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.context.ApplicationContext
31 import org.springframework.stereotype.Service
32
33 @Service
34 open class ComponentExecuteNodeExecutor : ExecuteNodeExecutor() {
35
36     private val log = LoggerFactory.getLogger(ComponentExecuteNodeExecutor::class.java)
37
38     @Autowired
39     private lateinit var context: ApplicationContext
40
41     fun getComponentFunction(pluginName: String): AbstractComponentFunction {
42         return context.getBean(pluginName, AbstractComponentFunction::class.java)
43     }
44
45     @Throws(SvcLogicException::class)
46     override fun execute(svc: SvcLogicService, node: SvcLogicNode, svcLogicContext: SvcLogicContext): SvcLogicNode {
47
48         var outValue: String
49
50         val ctx = svcLogicContext as BlueprintSvcLogicContext
51
52         val nodeTemplateName = SvcLogicExpressionResolver.evaluate(node.getAttribute("plugin"), node, ctx)
53
54         try {
55             // Get the Blueprint Context
56             val blueprintContext = ctx.getBluePrintService().bluePrintContext()
57             // Get the Component Name, NodeTemplate type is mapped to Component Name
58             val componentName = blueprintContext.nodeTemplateByName(nodeTemplateName).type
59
60             log.info("executing node template($nodeTemplateName) component($componentName)")
61             // Get the Component Instance
62             val plugin = this.getComponentFunction(componentName)
63
64             val executionInput = ctx.getRequest() as ExecutionServiceInput
65             // Get the Request from the Context and Set to the Function Input and Invoke the function
66             val executionOutput = plugin.apply(executionInput)
67
68             ctx.setResponse(executionOutput)
69
70             outValue = executionOutput.status.message
71             ctx.status = executionOutput.status.message
72
73         } catch (e: Exception) {
74             this.log.error("Could not execute plugin($nodeTemplateName) : ", e)
75             outValue = "failure"
76             ctx.status = "failure"
77         }
78
79         return this.getNextNode(node, outValue)
80     }
81 }