Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / workflow-service / src / main / kotlin / org / onap / ccsdk / cds / 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.cds.blueprintsprocessor.services.workflow
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.slf4j.LoggerFactory
29 import org.springframework.stereotype.Service
30
31 @Service
32 open class NodeTemplateExecutionService {
33
34     private val log = LoggerFactory.getLogger(NodeTemplateExecutionService::class.java)!!
35
36     suspend fun executeNodeTemplate(
37         bluePrintRuntimeService: BluePrintRuntimeService<*>,
38         nodeTemplateName: String,
39         executionServiceInput: ExecutionServiceInput
40     ): ExecutionServiceOutput {
41         // Get the Blueprint Context
42         val blueprintContext = bluePrintRuntimeService.bluePrintContext()
43
44         val nodeTemplate = blueprintContext.nodeTemplateByName(nodeTemplateName)
45         // Get the Component Name, NodeTemplate type is mapped to Component Name
46         val componentName = nodeTemplate.type
47
48         val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
49
50         val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
51
52         val nodeTemplateImplementation = blueprintContext
53             .nodeTemplateOperationImplementation(nodeTemplateName, interfaceName, operationName)
54
55         val timeout: Int = nodeTemplateImplementation?.timeout ?: 180
56
57         log.info(
58             "executing node template($nodeTemplateName) component($componentName) " +
59                     "interface($interfaceName) operation($operationName) with timeout($timeout) sec."
60         )
61
62         // Get the Component Instance
63         val plugin = BluePrintDependencyService.instance<AbstractComponentFunction>(componentName)
64         // Set the Blueprint Service
65         plugin.bluePrintRuntimeService = bluePrintRuntimeService
66         plugin.stepName = nodeTemplateName
67
68         // Parent request shouldn't tamper, so need to clone the request and send to the actual component.
69         val clonedExecutionServiceInput = ExecutionServiceInput().apply {
70             commonHeader = executionServiceInput.commonHeader
71             actionIdentifiers = executionServiceInput.actionIdentifiers
72             payload = executionServiceInput.payload
73         }
74
75         // Populate Step Meta Data
76         val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
77         stepInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = nodeTemplateName.asJsonPrimitive()
78         stepInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = interfaceName.asJsonPrimitive()
79         stepInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = operationName.asJsonPrimitive()
80         stepInputs[BluePrintConstants.PROPERTY_CURRENT_TIMEOUT] = timeout.asJsonPrimitive()
81         val stepInputData = StepData().apply {
82             name = nodeTemplateName
83             properties = stepInputs
84         }
85         clonedExecutionServiceInput.stepData = stepInputData
86
87         // Get the Request from the Context and Set to the Function Input and Invoke the function
88         return plugin.applyNB(clonedExecutionServiceInput)
89     }
90 }