Refractor blueprint script dependency
[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
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.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
31 import org.slf4j.LoggerFactory
32
33 /**
34  * AbstractComponentFunction
35  * @author Brinda Santh
36  */
37 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
38     @Transient
39     private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)
40
41     lateinit var executionServiceInput: ExecutionServiceInput
42     var executionServiceOutput = ExecutionServiceOutput()
43     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
44     lateinit var processId: String
45     lateinit var workflowName: String
46     lateinit var stepName: String
47     lateinit var interfaceName: String
48     lateinit var operationName: String
49     lateinit var nodeTemplateName: String
50     var operationInputs: MutableMap<String, JsonNode> = hashMapOf()
51
52     override fun getName(): String {
53         return stepName
54     }
55
56     override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
57         checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }
58         checkNotNull(executionRequest.stepData) { "failed to get step info" }
59
60         // Get the Step Name and Step Inputs
61         this.stepName = executionRequest.stepData!!.name
62         this.operationInputs = executionRequest.stepData!!.properties
63
64         checkNotEmpty(stepName) { "failed to get step name from step data" }
65
66         this.executionServiceInput = executionRequest
67
68         processId = executionRequest.commonHeader.requestId
69         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
70
71         workflowName = executionRequest.actionIdentifiers.actionName
72         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
73
74         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
75
76         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
77         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
78
79         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
80         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
81
82         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
83         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
84
85         val operationResolvedProperties = bluePrintRuntimeService
86                 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
87
88         this.operationInputs.putAll(operationResolvedProperties)
89
90         return executionRequest
91     }
92
93     override suspend fun prepareResponseNB(): ExecutionServiceOutput {
94         log.info("Preparing Response...")
95         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
96         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
97         var status = Status()
98         try {
99             // Resolve the Output Expression
100             val stepOutputs = bluePrintRuntimeService
101                     .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
102
103             val stepOutputData = StepData().apply {
104                 name = stepName
105                 properties = stepOutputs
106             }
107             executionServiceOutput.stepData = stepOutputData
108             // Set the Default Step Status
109             status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name
110         } catch (e: Exception) {
111             status.message = BluePrintConstants.STATUS_FAILURE
112             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
113         }
114         executionServiceOutput.status = status
115         return this.executionServiceOutput
116     }
117
118     override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
119         try {
120             prepareRequestNB(executionServiceInput)
121             processNB(executionServiceInput)
122         } catch (runtimeException: RuntimeException) {
123             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
124             recoverNB(runtimeException, executionServiceInput)
125         }
126         return prepareResponseNB()
127     }
128
129     fun getOperationInput(key: String): JsonNode {
130         return operationInputs[key]
131                 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")
132     }
133
134     fun getOptionalOperationInput(key: String): JsonNode? {
135         return operationInputs[key]
136     }
137
138     fun setAttribute(key: String, value: JsonNode) {
139         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
140     }
141
142     fun addError(type: String, name: String, error: String) {
143         bluePrintRuntimeService.getBluePrintError().addError(type, name, error)
144     }
145
146     fun addError(error: String) {
147         bluePrintRuntimeService.getBluePrintError().addError(error)
148     }
149
150     fun artifactContent(artifactName: String): String {
151         return bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
152     }
153
154     suspend fun mashTemplateNData(artifactName: String, json: String): String {
155         val content = artifactContent(artifactName)
156         return BluePrintVelocityTemplateService.generateContent(content, json)
157     }
158
159     suspend fun readLinesFromArtifact(artifactName: String): List<String> {
160         val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
161         val file = normalizedFile(bluePrintRuntimeService.bluePrintContext().rootPath, artifactDefinition.file)
162         return file.readNBLines()
163     }
164
165 }