2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
21 import com.fasterxml.jackson.databind.JsonNode
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.Status
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
28 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.slf4j.LoggerFactory
35 * AbstractComponentFunction
36 * @author Brinda Santh
38 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
40 private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)
42 lateinit var executionServiceInput: ExecutionServiceInput
43 var executionServiceOutput = ExecutionServiceOutput()
44 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
45 lateinit var processId: String
46 lateinit var workflowName: String
47 lateinit var stepName: String
48 lateinit var interfaceName: String
49 lateinit var operationName: String
50 lateinit var nodeTemplateName: String
51 var operationInputs: MutableMap<String, JsonNode> = hashMapOf()
53 override fun getName(): String {
57 override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
58 checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }
60 check(stepName.isNotEmpty()) { "failed to assign step name" }
62 this.executionServiceInput = executionRequest
64 processId = executionRequest.commonHeader.requestId
65 check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
67 workflowName = executionRequest.actionIdentifiers.actionName
68 check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
70 log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
72 val stepInputs = bluePrintRuntimeService.get("$stepName-step-inputs")
73 ?: JacksonUtils.objectMapper.createObjectNode()
75 stepInputs.fields().forEach {
76 this.operationInputs[it.key] = it.value
79 nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
80 check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
82 interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
83 check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
85 operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
86 check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
88 val operationResolvedProperties = bluePrintRuntimeService
89 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
91 this.operationInputs.putAll(operationResolvedProperties)
93 return executionRequest
96 override fun prepareResponse(): ExecutionServiceOutput {
97 log.info("Preparing Response...")
98 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
99 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
102 // Resolve the Output Expression
103 val stepOutputs = bluePrintRuntimeService
104 .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
106 bluePrintRuntimeService.put("$stepName-step-outputs", stepOutputs.asObjectNode())
107 // Set the Default Step Status
109 } catch (e: Exception) {
111 status.message = BluePrintConstants.STATUS_FAILURE
113 executionServiceOutput.status = status!!
114 return this.executionServiceOutput
117 override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
119 prepareRequest(executionServiceInput)
120 process(executionServiceInput)
121 } catch (runtimeException: RuntimeException) {
122 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
123 recover(runtimeException, executionServiceInput)
125 return prepareResponse()
128 fun getOperationInput(key: String): JsonNode {
129 return operationInputs[key]
130 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")
133 fun setAttribute(key: String, value: JsonNode) {
134 bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
137 fun addError(type: String, name: String, error: String) {
138 bluePrintRuntimeService.getBluePrintError().addError(type, name, error)
141 fun addError(error: String) {
142 bluePrintRuntimeService.getBluePrintError().addError(error)