Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / services / execution / AbstractComponentFunction.kt
1 /*\r
2  *  Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *  Modifications Copyright © 2019 IBM.\r
4  *\r
5  *  Licensed under the Apache License, Version 2.0 (the "License");\r
6  *  you may not use this file except in compliance with the License.\r
7  *  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  *  Unless required by applicable law or agreed to in writing, software\r
12  *  distributed under the License is distributed on an "AS IS" BASIS,\r
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  *  See the License for the specific language governing permissions and\r
15  *  limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.blueprintsprocessor.services.execution\r
19 \r
20 \r
21 import com.fasterxml.jackson.databind.JsonNode\r
22 import com.fasterxml.jackson.databind.node.JsonNodeFactory\r
23 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput\r
24 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput\r
25 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status\r
26 import org.onap.ccsdk.apps.controllerblueprints.common.api.EventType\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.asObjectNode\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.getAsString\r
31 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode\r
32 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService\r
33 import org.slf4j.LoggerFactory\r
34 \r
35 /**\r
36  * AbstractComponentFunction\r
37  * @author Brinda Santh\r
38  */\r
39 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {\r
40     @Transient\r
41     private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)\r
42 \r
43     lateinit var executionServiceInput: ExecutionServiceInput\r
44     var executionServiceOutput = ExecutionServiceOutput()\r
45     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>\r
46     lateinit var processId: String\r
47     lateinit var workflowName: String\r
48     lateinit var stepName: String\r
49     lateinit var interfaceName: String\r
50     lateinit var operationName: String\r
51     lateinit var nodeTemplateName: String\r
52     var operationInputs: MutableMap<String, JsonNode> = hashMapOf()\r
53 \r
54     override fun getName(): String {\r
55         return stepName\r
56     }\r
57 \r
58     override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {\r
59         checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }\r
60 \r
61         check(stepName.isNotEmpty()) { "failed to assign step name" }\r
62 \r
63         this.executionServiceInput = executionRequest\r
64 \r
65         processId = executionRequest.commonHeader.requestId\r
66         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }\r
67 \r
68         workflowName = executionRequest.actionIdentifiers.actionName\r
69         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }\r
70 \r
71         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")\r
72 \r
73         val operationInputs = bluePrintRuntimeService.get("$stepName-step-inputs")\r
74                 ?: JsonNodeFactory.instance.objectNode()\r
75 \r
76         operationInputs.fields().forEach {\r
77             this.operationInputs[it.key] = it.value\r
78         }\r
79 \r
80         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)\r
81         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }\r
82 \r
83         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)\r
84         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }\r
85 \r
86         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)\r
87         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }\r
88 \r
89         val operationResolvedProperties = bluePrintRuntimeService\r
90                 .resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)\r
91 \r
92         this.operationInputs.putAll(operationResolvedProperties)\r
93 \r
94         return executionRequest\r
95     }\r
96 \r
97     override fun prepareResponse(): ExecutionServiceOutput {\r
98         log.info("Preparing Response...")\r
99         executionServiceOutput.commonHeader = executionServiceInput.commonHeader\r
100         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers\r
101 \r
102         // Resolve the Output Expression\r
103         val stepOutputs = bluePrintRuntimeService\r
104                 .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)\r
105 \r
106         // FIXME("Not the right place to populate the response payload")\r
107         executionServiceOutput.payload = stepOutputs.asObjectNode()\r
108 \r
109         bluePrintRuntimeService.put("$stepName-step-outputs", executionServiceOutput.payload)\r
110 \r
111         // FIXME("Not the right place to populate the status")\r
112         // Populate Status\r
113         val status = Status()\r
114         status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name\r
115         status.code = 200\r
116         status.message = BluePrintConstants.STATUS_SUCCESS\r
117         executionServiceOutput.status = status\r
118         return this.executionServiceOutput\r
119     }\r
120 \r
121     override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {\r
122         try {\r
123             prepareRequest(executionServiceInput)\r
124             process(executionServiceInput)\r
125         } catch (runtimeException: RuntimeException) {\r
126             recover(runtimeException, executionServiceInput)\r
127         }\r
128         return prepareResponse()\r
129     }\r
130 \r
131     fun getOperationInput(key: String): JsonNode {\r
132         return operationInputs[key]\r
133                 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")\r
134     }\r
135 \r
136     fun setAttribute(key: String, value: JsonNode) {\r
137         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)\r
138     }\r
139 \r
140     fun addError(type: String, name: String, error: String) {\r
141         bluePrintRuntimeService.getBluePrintError().addError(type, name, error)\r
142     }\r
143 \r
144     fun addError(error: String) {\r
145         bluePrintRuntimeService.getBluePrintError().addError(error)\r
146     }\r
147 \r
148 }