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
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             // Set the Default Step Status
147             status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name
148         } catch (e: Exception) {
149             status.message = BlueprintConstants.STATUS_FAILURE
150             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
151         }
152         executionServiceOutput.status = status
153         return this.executionServiceOutput
154     }
155
156     override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
157         try {
158             prepareRequestNB(executionServiceInput)
159             implementation.lock?.let {
160                 bluePrintClusterService.clusterLock("${it.key.textValue()}@$CDS_LOCK_GROUP")
161                     .executeWithLock(it.acquireTimeout.intValue().times(1000).toLong()) {
162                         applyNBWithTimeout(executionServiceInput)
163                     }
164             } ?: applyNBWithTimeout(executionServiceInput)
165         } catch (runtimeException: RuntimeException) {
166             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
167             recoverNB(runtimeException, executionServiceInput)
168         }
169         return prepareResponseNB()
170     }
171
172     private suspend fun applyNBWithTimeout(executionServiceInput: ExecutionServiceInput) =
173         withTimeout((implementation.timeout * 1000).toLong()) {
174             log.debug(
175                 "DEBUG::: AbstractComponentFunction.withTimeout " +
176                     "section ${implementation.timeout} seconds"
177             )
178             processNB(executionServiceInput)
179         }
180
181     fun getOperationInput(key: String): JsonNode {
182         return operationInputs[key]
183             ?: throw BlueprintProcessorException("couldn't get the operation input($key) value.")
184     }
185
186     fun getOptionalOperationInput(key: String): JsonNode? {
187         return operationInputs[key]
188     }
189
190     fun setAttribute(key: String, value: JsonNode) {
191         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
192     }
193
194     fun addError(type: String, name: String, error: String) {
195         bluePrintRuntimeService.getBlueprintError().addError(type, name, error)
196     }
197
198     fun addError(error: String) {
199         bluePrintRuntimeService.getBlueprintError().addError(error)
200     }
201
202     /**
203      * Get Execution Input Payload data
204      */
205     fun requestPayload(): JsonNode? {
206         return executionServiceInput.payload
207     }
208
209     /**
210      * Get Execution Input payload action property with [expression]
211      * ex: requestPayloadActionProperty("data") will look for path "payload/<action-name>-request/data"
212      */
213     fun requestPayloadActionProperty(expression: String?): JsonNode? {
214         val requestExpression = if (expression.isNullOrBlank()) {
215             "$workflowName-request"
216         } else {
217             "$workflowName-request.$expression"
218         }
219         return executionServiceInput.payload.jsonPathParse(".$requestExpression")
220     }
221
222     suspend fun artifactContent(artifactName: String): String {
223         return bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
224     }
225
226     suspend fun relationshipProperty(relationshipName: String, propertyName: String): JsonNode {
227         return bluePrintRuntimeService.resolveRelationshipTemplateProperties(relationshipName).get(propertyName)
228             ?: throw BlueprintProcessorException("failed to get relationship($relationshipName) property($propertyName)")
229     }
230
231     suspend fun mashTemplateNData(artifactName: String, json: String): String {
232         val content = artifactContent(artifactName)
233         return BlueprintVelocityTemplateService.generateContent(content, json)
234     }
235
236     suspend fun readLinesFromArtifact(artifactName: String): List<String> {
237         val artifactDefinition =
238             bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
239         val file = normalizedFile(bluePrintRuntimeService.bluePrintContext().rootPath, artifactDefinition.file)
240         return file.readNBLines()
241     }
242 }