12f6bc47f928c99cec3881e0e5ccac185891e3b9
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *  Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
19
20
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.BluePrintConstants
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
30 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
31 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
33 import org.slf4j.LoggerFactory
34
35 /**
36  * AbstractComponentFunction
37  * @author Brinda Santh
38  */
39 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
40     @Transient
41     private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)
42
43     lateinit var executionServiceInput: ExecutionServiceInput
44     var executionServiceOutput = ExecutionServiceOutput()
45     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
46     lateinit var processId: String
47     lateinit var workflowName: String
48     lateinit var stepName: String
49     lateinit var interfaceName: String
50     lateinit var operationName: String
51     lateinit var nodeTemplateName: String
52     var operationInputs: MutableMap<String, JsonNode> = hashMapOf()
53
54     override fun getName(): String {
55         return stepName
56     }
57
58     override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
59         checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }
60         checkNotNull(executionRequest.stepData) { "failed to get step info" }
61
62         // Get the Step Name and Step Inputs
63         this.stepName = executionRequest.stepData!!.name
64         this.operationInputs = executionRequest.stepData!!.properties
65
66         checkNotEmpty(stepName) { "failed to get step name from step data" }
67
68         this.executionServiceInput = executionRequest
69
70         processId = executionRequest.commonHeader.requestId
71         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
72
73         workflowName = executionRequest.actionIdentifiers.actionName
74         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
75
76         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
77
78         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
79         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
80
81         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
82         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
83
84         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
85         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
86
87         val operationResolvedProperties = bluePrintRuntimeService
88                 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
89
90         this.operationInputs.putAll(operationResolvedProperties)
91
92         return executionRequest
93     }
94
95     override suspend fun prepareResponseNB(): ExecutionServiceOutput {
96         log.info("Preparing Response...")
97         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
98         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
99         var status = Status()
100         try {
101             // Resolve the Output Expression
102             val stepOutputs = bluePrintRuntimeService
103                     .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
104
105             val stepOutputData = StepData().apply {
106                 name = stepName
107                 properties = stepOutputs
108             }
109             executionServiceOutput.stepData = stepOutputData
110             // Set the Default Step Status
111             status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name
112         } catch (e: Exception) {
113             status.message = BluePrintConstants.STATUS_FAILURE
114             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
115         }
116         executionServiceOutput.status = status
117         return this.executionServiceOutput
118     }
119
120     override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
121         try {
122             prepareRequestNB(executionServiceInput)
123             processNB(executionServiceInput)
124         } catch (runtimeException: RuntimeException) {
125             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
126             recoverNB(runtimeException, executionServiceInput)
127         }
128         return prepareResponseNB()
129     }
130
131     fun getOperationInput(key: String): JsonNode {
132         return operationInputs[key]
133                 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")
134     }
135
136     fun setAttribute(key: String, value: JsonNode) {
137         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
138     }
139
140     fun addError(type: String, name: String, error: String) {
141         bluePrintRuntimeService.getBluePrintError().addError(type, name, error)
142     }
143
144     fun addError(error: String) {
145         bluePrintRuntimeService.getBluePrintError().addError(error)
146     }
147
148 }