a67e006a62efa86523b41f8c524eb07738ba8f4e
[ccsdk/cds.git] /
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.asJsonNode\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.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils\r
34 import org.slf4j.LoggerFactory\r
35 \r
36 /**\r
37  * AbstractComponentFunction\r
38  * @author Brinda Santh\r
39  */\r
40 abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {\r
41     @Transient\r
42     private val log = LoggerFactory.getLogger(AbstractComponentFunction::class.java)\r
43 \r
44     lateinit var executionServiceInput: ExecutionServiceInput\r
45     var executionServiceOutput = ExecutionServiceOutput()\r
46     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>\r
47     lateinit var processId: String\r
48     lateinit var workflowName: String\r
49     lateinit var stepName: String\r
50     lateinit var interfaceName: String\r
51     lateinit var operationName: String\r
52     lateinit var nodeTemplateName: String\r
53     var operationInputs: MutableMap<String, JsonNode> = hashMapOf()\r
54     /**\r
55      * Store Dynamic Dependency Instances\r
56      */\r
57     var functionDependencyInstances: MutableMap<String, Any> = hashMapOf()\r
58 \r
59     override fun getName(): String {\r
60         return stepName\r
61     }\r
62 \r
63     override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {\r
64         checkNotNull(bluePrintRuntimeService) { "failed to prepare blueprint runtime" }\r
65 \r
66         check(stepName.isNotEmpty()) { "failed to assign step name" }\r
67 \r
68         this.executionServiceInput = executionRequest\r
69 \r
70         processId = executionRequest.commonHeader.requestId\r
71         check(processId.isNotEmpty()) { "couldn't get process id for step($stepName)" }\r
72 \r
73         workflowName = executionRequest.actionIdentifiers.actionName\r
74         check(workflowName.isNotEmpty()) { "couldn't get action name for step($stepName)" }\r
75 \r
76         log.info("preparing request id($processId) for workflow($workflowName) step($stepName)")\r
77 \r
78         val operationInputs = bluePrintRuntimeService.get("$stepName-step-inputs")\r
79                 ?: JsonNodeFactory.instance.objectNode()\r
80 \r
81         operationInputs.fields().forEach {\r
82             this.operationInputs[it.key] = it.value\r
83         }\r
84 \r
85         nodeTemplateName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE)\r
86         check(nodeTemplateName.isNotEmpty()) { "couldn't get NodeTemplate name for step($stepName)" }\r
87 \r
88         interfaceName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_INTERFACE)\r
89         check(interfaceName.isNotEmpty()) { "couldn't get Interface name for step($stepName)" }\r
90 \r
91         operationName = this.operationInputs.getAsString(BluePrintConstants.PROPERTY_CURRENT_OPERATION)\r
92         check(operationName.isNotEmpty()) { "couldn't get Operation name for step($stepName)" }\r
93 \r
94         val operationResolvedProperties = bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName)\r
95 \r
96         this.operationInputs.putAll(operationResolvedProperties)\r
97 \r
98         return executionRequest\r
99     }\r
100 \r
101     override fun prepareResponse(): ExecutionServiceOutput {\r
102         log.info("Preparing Response...")\r
103         executionServiceOutput.commonHeader = executionServiceInput.commonHeader\r
104         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers\r
105 \r
106 \r
107         // Resolve the Output Expression\r
108         val stepOutputs = bluePrintRuntimeService\r
109                 .resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName, interfaceName, operationName)\r
110 \r
111         bluePrintRuntimeService.put("$stepName-step-outputs", stepOutputs.asJsonNode())\r
112 \r
113         executionServiceOutput.payload = JacksonUtils.objectNodeFromObject(stepOutputs)\r
114 \r
115         // Populate Status\r
116         val status = Status()\r
117         status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name\r
118         status.code = 200\r
119         status.message = BluePrintConstants.STATUS_SUCCESS\r
120         executionServiceOutput.status = status\r
121         return this.executionServiceOutput\r
122     }\r
123 \r
124     override fun apply(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {\r
125         prepareRequest(executionServiceInput)\r
126         process(executionServiceInput)\r
127         return prepareResponse()\r
128     }\r
129 \r
130     fun getOperationInput(key: String): JsonNode {\r
131         return operationInputs[key]\r
132                 ?: throw BluePrintProcessorException("couldn't get the operation input($key) value.")\r
133     }\r
134 \r
135     fun setAttribute(key: String, value: JsonNode) {\r
136         bluePrintRuntimeService.setNodeTemplateAttributeValue(nodeTemplateName, key, value)\r
137     }\r
138 \r
139     /**\r
140      * This will be called from the scripts to serve instance from runtime to scripts.\r
141      */\r
142     open fun <T> functionDependencyInstanceAsType(name: String): T {\r
143         return functionDependencyInstances[name] as? T\r
144                 ?: throw BluePrintProcessorException("couldn't get script property instance ($name)")\r
145     }\r
146 }