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