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