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