Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / AbstractComponentFunction.kt
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 import com.fasterxml.jackson.databind.JsonNode
21 import kotlinx.coroutines.withTimeout
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.blueprintsprocessor.core.cluster.executeWithLock
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.BluePrintClusterService
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.CDS_LOCK_GROUP
29 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
33 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotBlank
34 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
35 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
36 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
37 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
38 import org.onap.ccsdk.cds.controllerblueprints.core.jsonPathParse
39 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
40 import org.onap.ccsdk.cds.controllerblueprints.core.readNBLines
41 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
42 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
43 import org.slf4j.LoggerFactory
44
45 /**
46  * AbstractComponentFunction
47  * @author Brinda Santh
48  */
49 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
50
51     @Transient
52     private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)
53
54     lateinit var executionServiceInput: ExecutionServiceInput
55     var executionServiceOutput = ExecutionServiceOutput()
56     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
57     lateinit var bluePrintClusterService: BluePrintClusterService
58     lateinit var implementation: Implementation
59     lateinit var processId: String
60     lateinit var workflowName: String
61     lateinit var stepName: String
62     lateinit var interfaceName: String
63     lateinit var operationName: String
64     lateinit var nodeTemplateName: String
65     var operationInputs: MutableMap<String, JsonNode> = hashMapOf()
66
67     override fun getName(): String {
68         return "$stepName - $nodeTemplateName"
69     }
70
71     override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
72         check(this::bluePrintRuntimeService.isInitialized) { "failed to prepare blueprint runtime" }
73         checkNotNull(executionRequest.stepData) { "failed to get step info" }
74
75         // Get the Step Name and Step Inputs
76         this.stepName = executionRequest.stepData!!.name
77         this.operationInputs = executionRequest.stepData!!.properties
78
79         checkNotEmpty(stepName) { "failed to get step name from step data" }
80
81         this.executionServiceInput = executionRequest
82
83         processId = executionRequest.commonHeader.requestId
84         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
85
86         workflowName = executionRequest.actionIdentifiers.actionName
87         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
88
89         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
90
91         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
92         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
93
94         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
95         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
96
97         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
98         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
99
100         /** Get the Implementation Details */
101         implementation = bluePrintRuntimeService.bluePrintContext()
102             .nodeTemplateOperationImplementation(nodeTemplateName, interfaceName, operationName)
103             ?: Implementation()
104
105         /** Resolve and validate lock properties */
106         implementation.lock?.apply {
107             val resolvedValues = bluePrintRuntimeService.resolvePropertyAssignments(
108                 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TEMPLATE,
109                 interfaceName,
110                 mutableMapOf("key" to this.key, "acquireTimeout" to this.acquireTimeout)
111             )
112             this.key = resolvedValues["key"] ?: "".asJsonType()
113             this.acquireTimeout = resolvedValues["acquireTimeout"] ?: "".asJsonType()
114
115             checkNotBlank(this.key.textValue()) { "Failed to resolve lock key" }
116             check(this.acquireTimeout.isInt && this.acquireTimeout.intValue() >= 0) {
117                 "Failed to resolve lock acquireTimeout - must be a positive integer"
118             }
119         }
120
121         check(this::implementation.isInitialized) { "failed to prepare implementation" }
122
123         val operationResolvedProperties = bluePrintRuntimeService
124             .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
125
126         this.operationInputs.putAll(operationResolvedProperties)
127
128         return executionRequest
129     }
130
131     override suspend fun prepareResponseNB(): ExecutionServiceOutput {
132         log.info("Preparing Response...")
133         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
134         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
135         val status = Status()
136         try {
137             // Resolve the Output Expression
138             val stepOutputs = bluePrintRuntimeService
139                 .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
140
141             val stepOutputData = StepData().apply {
142                 name = stepName
143                 properties = stepOutputs
144             }
145             executionServiceOutput.stepData = stepOutputData
146
147             status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name
148
149             bluePrintRuntimeService.getBluePrintError().stepErrors(stepName)?.let {
150                 status.message = BluePrintConstants.STATUS_FAILURE
151             }
152         } catch (e: Exception) {
153             status.message = BluePrintConstants.STATUS_FAILURE
154             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
155         }
156         executionServiceOutput.status = status
157         return this.executionServiceOutput
158     }
159
160     override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
161         try {
162             prepareRequestNB(executionServiceInput)
163             implementation.lock?.let {
164                 bluePrintClusterService.clusterLock("${it.key.textValue()}@$CDS_LOCK_GROUP")
165                     .executeWithLock(it.acquireTimeout.intValue().times(1000).toLong()) {
166                         applyNBWithTimeout(executionServiceInput)
167                     }
168             } ?: applyNBWithTimeout(executionServiceInput)
169         } catch (runtimeException: RuntimeException) {
170             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
171             recoverNB(runtimeException, executionServiceInput)
172         }
173         return prepareResponseNB()
174     }
175
176     private suspend fun applyNBWithTimeout(executionServiceInput: ExecutionServiceInput) =
177         withTimeout((implementation.timeout * 1000).toLong()) {
178             log.debug(
179                 "DEBUG::: AbstractComponentFunction.withTimeout " +
180                     "section ${implementation.timeout} seconds"
181             )
182             processNB(executionServiceInput)
183         }
184
185     fun getOperationInput(key: String): JsonNode {
186         return operationInputs[key]
187             ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")
188     }
189
190     fun getOptionalOperationInput(key: String): JsonNode? {
191         return operationInputs[key]
192     }
193
194     fun setAttribute(key: String, value: JsonNode) {
195         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
196     }
197
198     fun addError(type: String, name: String, error: String) {
199         bluePrintRuntimeService.getBluePrintError().addError(type, name, error, stepName)
200     }
201
202     fun addError(error: String) {
203         bluePrintRuntimeService.getBluePrintError().addError(error, stepName)
204     }
205
206     /**
207      * Get Execution Input Payload data
208      */
209     fun requestPayload(): JsonNode? {
210         return executionServiceInput.payload
211     }
212
213     /**
214      * Get Execution Input payload action property with [expression]
215      * ex: requestPayloadActionProperty("data") will look for path "payload/<action-name>-request/data"
216      */
217     fun requestPayloadActionProperty(expression: String?): JsonNode? {
218         val requestExpression = if (expression.isNullOrBlank()) {
219             "$workflowName-request"
220         } else {
221             "$workflowName-request.$expression"
222         }
223         return executionServiceInput.payload.jsonPathParse(".$requestExpression")
224     }
225
226     suspend fun artifactContent(artifactName: String): String {
227         return bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
228     }
229
230     suspend fun relationshipProperty(relationshipName: String, propertyName: String): JsonNode {
231         return bluePrintRuntimeService.resolveRelationshipTemplateProperties(relationshipName).get(propertyName)
232             ?: throw BluePrintProcessorException("failed to get relationship($relationshipName) property($propertyName)")
233     }
234
235     suspend fun mashTemplateNData(artifactName: String, json: String): String {
236         val content = artifactContent(artifactName)
237         return BluePrintVelocityTemplateService.generateContent(content, json)
238     }
239
240     suspend fun readLinesFromArtifact(artifactName: String): List<String> {
241         val artifactDefinition =
242             bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
243         val file = normalizedFile(bluePrintRuntimeService.bluePrintContext().rootPath, artifactDefinition.file)
244         return file.readNBLines()
245     }
246 }