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.blueprintsprocessor.core.api.data.StepData
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
27 import org.onap.ccsdk.cds.controllerblueprints.core.*
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.slf4j.LoggerFactory
33 * AbstractComponentFunction
34 * @author Brinda Santh
36 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
38 private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)
40 lateinit var executionServiceInput: ExecutionServiceInput
41 var executionServiceOutput = ExecutionServiceOutput()
42 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
43 lateinit var processId: String
44 lateinit var workflowName: String
45 lateinit var stepName: String
46 lateinit var interfaceName: String
47 lateinit var operationName: String
48 lateinit var nodeTemplateName: String
49 var operationInputs: MutableMap<String, JsonNode> = hashMapOf()
51 override fun getName(): String {
55 override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
56 checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }
57 checkNotNull(executionRequest.stepData) { "failed to get step info" }
59 // Get the Step Name and Step Inputs
60 this.stepName = executionRequest.stepData!!.name
61 this.operationInputs = executionRequest.stepData!!.properties
63 checkNotEmpty(stepName) { "failed to get step name from step data" }
65 this.executionServiceInput = executionRequest
67 processId = executionRequest.commonHeader.requestId
68 check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
70 workflowName = executionRequest.actionIdentifiers.actionName
71 check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
73 log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
75 nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
76 check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
78 interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
79 check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
81 operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
82 check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
84 val operationResolvedProperties = bluePrintRuntimeService
85 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
87 this.operationInputs.putAll(operationResolvedProperties)
89 return executionRequest
92 override suspend fun prepareResponseNB(): ExecutionServiceOutput {
93 log.info("Preparing Response...")
94 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
95 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
98 // Resolve the Output Expression
99 val stepOutputs = bluePrintRuntimeService
100 .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
102 val stepOutputData = StepData().apply {
104 properties = stepOutputs
106 executionServiceOutput.stepData = stepOutputData
107 // Set the Default Step Status
108 status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name
109 } catch (e: Exception) {
110 status.message = BluePrintConstants.STATUS_FAILURE
111 status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
113 executionServiceOutput.status = status
114 return this.executionServiceOutput
117 override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
119 prepareRequestNB(executionServiceInput)
120 processNB(executionServiceInput)
121 } catch (runtimeException: RuntimeException) {
122 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
123 recoverNB(runtimeException, executionServiceInput)
125 return prepareResponseNB()
128 fun getOperationInput(key: String): JsonNode {
129 return operationInputs[key]
130 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")
133 fun getOptionalOperationInput(key: String): JsonNode? {
134 return operationInputs[key]
137 fun setAttribute(key: String, value: JsonNode) {
138 bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
141 fun addError(type: String, name: String, error: String) {
142 bluePrintRuntimeService.getBluePrintError().addError(type, name, error)
145 fun addError(error: String) {
146 bluePrintRuntimeService.getBluePrintError().addError(error)