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         nodeTemplateName: String,
41         executionServiceInput: ExecutionServiceInput
42     ): ExecutionServiceOutput {
43         // Get the Blueprint Context
44         val blueprintContext = bluePrintRuntimeService.bluePrintContext()
45
46         val nodeTemplate = blueprintContext.nodeTemplateByName(nodeTemplateName)
47         // Get the Component Name, NodeTemplate type is mapped to Component Name
48         val componentName = nodeTemplate.type
49
50         val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
51
52         val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
53
54         val nodeTemplateImplementation = blueprintContext
55             .nodeTemplateOperationImplementation(nodeTemplateName, interfaceName, operationName)
56             ?: Implementation()
57
58         log.info(
59             "executing node template($nodeTemplateName) component($componentName) " +
60                 "interface($interfaceName) operation($operationName) on host (${nodeTemplateImplementation.operationHost}) " +
61                 "with timeout(${nodeTemplateImplementation.timeout}) sec."
62         )
63
64         // Get the Component Instance
65         val plugin = BlueprintDependencyService.instance<AbstractComponentFunction>(componentName)
66         // Set the Blueprint Services
67         plugin.bluePrintRuntimeService = bluePrintRuntimeService
68         plugin.bluePrintClusterService = bluePrintClusterService
69         plugin.stepName = nodeTemplateName
70
71         // Parent request shouldn't tamper, so need to clone the request and send to the actual component.
72         val clonedExecutionServiceInput = ExecutionServiceInput().apply {
73             commonHeader = executionServiceInput.commonHeader
74             actionIdentifiers = executionServiceInput.actionIdentifiers
75             payload = executionServiceInput.payload
76         }
77
78         // Populate Step Meta Data
79         val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
80         stepInputs[BlueprintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = nodeTemplateName.asJsonPrimitive()
81         stepInputs[BlueprintConstants.PROPERTY_CURRENT_INTERFACE] = interfaceName.asJsonPrimitive()
82         stepInputs[BlueprintConstants.PROPERTY_CURRENT_OPERATION] = operationName.asJsonPrimitive()
83         val stepInputData = StepData().apply {
84             name = nodeTemplateName
85             properties = stepInputs
86         }
87         clonedExecutionServiceInput.stepData = stepInputData
88
89         // Get the Request from the Context and Set to the Function Input and Invoke the function
90         return plugin.applyNB(clonedExecutionServiceInput)
91     }
92 }