Merge "Add default values for health-api properties"
[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.data.Implementation
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
29 import org.slf4j.LoggerFactory
30 import org.springframework.stereotype.Service
31
32 @Service
33 open class NodeTemplateExecutionService {
34
35     private val log = LoggerFactory.getLogger(NodeTemplateExecutionService::class.java)!!
36
37     suspend fun executeNodeTemplate(
38         bluePrintRuntimeService: BluePrintRuntimeService<*>,
39         nodeTemplateName: String,
40         executionServiceInput: ExecutionServiceInput
41     ): ExecutionServiceOutput {
42         // Get the Blueprint Context
43         val blueprintContext = bluePrintRuntimeService.bluePrintContext()
44
45         val nodeTemplate = blueprintContext.nodeTemplateByName(nodeTemplateName)
46         // Get the Component Name, NodeTemplate type is mapped to Component Name
47         val componentName = nodeTemplate.type
48
49         val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
50
51         val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
52
53         val nodeTemplateImplementation = blueprintContext
54             .nodeTemplateOperationImplementation(nodeTemplateName, interfaceName, operationName)
55             ?: Implementation()
56
57         log.info(
58             "executing node template($nodeTemplateName) component($componentName) " +
59                 "interface($interfaceName) operation($operationName) on host (${nodeTemplateImplementation.operationHost}) " +
60                 "with timeout(${nodeTemplateImplementation.timeout}) sec."
61         )
62
63         // Get the Component Instance
64         val plugin = BluePrintDependencyService.instance<AbstractComponentFunction>(componentName)
65         // Set the Blueprint Service
66         plugin.bluePrintRuntimeService = bluePrintRuntimeService
67         plugin.stepName = nodeTemplateName
68
69         // Parent request shouldn't tamper, so need to clone the request and send to the actual component.
70         val clonedExecutionServiceInput = ExecutionServiceInput().apply {
71             commonHeader = executionServiceInput.commonHeader
72             actionIdentifiers = executionServiceInput.actionIdentifiers
73             payload = executionServiceInput.payload
74         }
75
76         // Populate Step Meta Data
77         val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
78         stepInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = nodeTemplateName.asJsonPrimitive()
79         stepInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = interfaceName.asJsonPrimitive()
80         stepInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = operationName.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 }