2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.workflow
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
32 open class NodeTemplateExecutionService {
34 private val log = LoggerFactory.getLogger(NodeTemplateExecutionService::class.java)!!
36 suspend fun executeNodeTemplate(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
37 executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
38 // Get the Blueprint Context
39 val blueprintContext = bluePrintRuntimeService.bluePrintContext()
41 val nodeTemplate = blueprintContext.nodeTemplateByName(nodeTemplateName)
42 // Get the Component Name, NodeTemplate type is mapped to Component Name
43 val componentName = nodeTemplate.type
45 val interfaceName = blueprintContext.nodeTemplateFirstInterfaceName(nodeTemplateName)
47 val operationName = blueprintContext.nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName)
49 val nodeTemplateImplementation = blueprintContext
50 .nodeTemplateOperationImplementation(nodeTemplateName, interfaceName, operationName)
52 val timeout: Int = nodeTemplateImplementation?.timeout ?: 180
54 log.info("executing node template($nodeTemplateName) component($componentName) " +
55 "interface($interfaceName) operation($operationName) with timeout($timeout) sec.")
57 // Get the Component Instance
58 val plugin = BluePrintDependencyService.instance<AbstractComponentFunction>(componentName)
59 // Set the Blueprint Service
60 plugin.bluePrintRuntimeService = bluePrintRuntimeService
61 plugin.stepName = nodeTemplateName
63 // Parent request shouldn't tamper, so need to clone the request and send to the actual component.
64 val clonedExecutionServiceInput = ExecutionServiceInput().apply {
65 commonHeader = executionServiceInput.commonHeader
66 actionIdentifiers = executionServiceInput.actionIdentifiers
67 payload = executionServiceInput.payload
70 // Populate Step Meta Data
71 val stepInputs: MutableMap<String, JsonNode> = hashMapOf()
72 stepInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = nodeTemplateName.asJsonPrimitive()
73 stepInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = interfaceName.asJsonPrimitive()
74 stepInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = operationName.asJsonPrimitive()
75 stepInputs[BluePrintConstants.PROPERTY_CURRENT_TIMEOUT] = timeout.asJsonPrimitive()
76 val stepInputData = StepData().apply {
77 name = nodeTemplateName
78 properties = stepInputs
80 clonedExecutionServiceInput.stepData = stepInputData
82 // Get the Request from the Context and Set to the Function Input and Invoke the function
83 return plugin.applyNB(clonedExecutionServiceInput)