Merge "Add Restconf Kotlin script sample"
[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.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
28 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.slf4j.LoggerFactory
33
34 /**
35  * AbstractComponentFunction
36  * @author Brinda Santh
37  */
38 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
39     @Transient
40     private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)
41
42     lateinit var executionServiceInput: ExecutionServiceInput
43     var executionServiceOutput = ExecutionServiceOutput()
44     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
45     lateinit var processId: String
46     lateinit var workflowName: String
47     lateinit var stepName: String
48     lateinit var interfaceName: String
49     lateinit var operationName: String
50     lateinit var nodeTemplateName: String
51     var operationInputs: MutableMap<String, JsonNode> = hashMapOf()
52
53     override fun getName(): String {
54         return stepName
55     }
56
57     override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
58         checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }
59
60         check(stepName.isNotEmpty()) { "failed to assign step name" }
61
62         this.executionServiceInput = executionRequest
63
64         processId = executionRequest.commonHeader.requestId
65         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
66
67         workflowName = executionRequest.actionIdentifiers.actionName
68         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
69
70         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
71
72         val stepInputs = bluePrintRuntimeService.get("$stepName-step-inputs")
73                 ?: JacksonUtils.objectMapper.createObjectNode()
74
75         stepInputs.fields().forEach {
76             this.operationInputs[it.key] = it.value
77         }
78
79         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
80         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
81
82         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
83         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
84
85         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
86         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
87
88         val operationResolvedProperties = bluePrintRuntimeService
89                 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
90
91         this.operationInputs.putAll(operationResolvedProperties)
92
93         return executionRequest
94     }
95
96     override fun prepareResponse(): ExecutionServiceOutput {
97         log.info("Preparing Response...")
98         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
99         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
100         var status: Status?
101         try {
102             // Resolve the Output Expression
103             val stepOutputs = bluePrintRuntimeService
104                     .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
105
106             bluePrintRuntimeService.put("$stepName-step-outputs", stepOutputs.asObjectNode())
107             // Set the Default Step Status
108             status = Status()
109         } catch (e: Exception) {
110             status = Status()
111             status.message = BluePrintConstants.STATUS_FAILURE
112         }
113         executionServiceOutput.status = status!!
114         return this.executionServiceOutput
115     }
116
117     override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
118         try {
119             prepareRequest(executionServiceInput)
120             process(executionServiceInput)
121         } catch (runtimeException: RuntimeException) {
122             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
123             recover(runtimeException, executionServiceInput)
124         }
125         return prepareResponse()
126     }
127
128     fun getOperationInput(key: String): JsonNode {
129         return operationInputs[key]
130                 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")
131     }
132
133     fun setAttribute(key: String, value: JsonNode) {
134         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)
135     }
136
137     fun addError(type: String, name: String, error: String) {
138         bluePrintRuntimeService.getBluePrintError().addError(type, name, error)
139     }
140
141     fun addError(error: String) {
142         bluePrintRuntimeService.getBluePrintError().addError(error)
143     }
144
145 }