35fef96fd3406a05444d97ac792447f51caed803
[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.controllerblueprints.common.api.EventType
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.asObjectNode
29 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
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 fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
59         checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }
60
61         check(stepName.isNotEmpty()) { "failed to assign step name" }
62
63         this.executionServiceInput = executionRequest
64
65         processId = executionRequest.commonHeader.requestId
66         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }
67
68         workflowName = executionRequest.actionIdentifiers.actionName
69         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }
70
71         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")
72
73         val stepInputs = bluePrintRuntimeService.get("$stepName-step-inputs")
74                 ?: JacksonUtils.objectMapper.createObjectNode()
75
76         stepInputs.fields().forEach {
77             this.operationInputs[it.key] = it.value
78         }
79
80         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)
81         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }
82
83         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)
84         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }
85
86         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)
87         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }
88
89         val operationResolvedProperties = bluePrintRuntimeService
90                 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)
91
92         this.operationInputs.putAll(operationResolvedProperties)
93
94         return executionRequest
95     }
96
97     override fun prepareResponse(): ExecutionServiceOutput {
98         log.info("Preparing Response...")
99         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
100         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
101         var status: Status?
102         try {
103             // Resolve the Output Expression
104             val stepOutputs = bluePrintRuntimeService
105                     .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)
106
107             bluePrintRuntimeService.put("$stepName-step-outputs", stepOutputs.asObjectNode())
108             // Set the Default Step Status
109             status = Status()
110         } catch (e: Exception) {
111             status = Status()
112             status.message = BluePrintConstants.STATUS_FAILURE
113         }
114         executionServiceOutput.status = status!!
115         return this.executionServiceOutput
116     }
117
118     override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
119         try {
120             prepareRequest(executionServiceInput)
121             process(executionServiceInput)
122         } catch (runtimeException: 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 }