Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / AbstractServiceFunction.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.Status
23 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
26 import org.onap.ccsdk.cds.controllerblueprints.core.jsonPathParse
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29
30 /** This implementation is used to build services, which doesn't need blueprints */
31 abstract class AbstractServiceFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
32
33     @Transient
34     private val log = logger(AbstractServiceFunction::class)
35
36     lateinit var executionServiceInput: ExecutionServiceInput
37     var executionServiceOutput = ExecutionServiceOutput()
38     lateinit var processId: String
39     lateinit var actionName: String
40     lateinit var responseActionPayload: JsonNode
41
42     override fun getName(): String {
43         return actionName
44     }
45
46     override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
47
48         this.executionServiceInput = executionRequest
49
50         actionName = executionRequest.actionIdentifiers.actionName
51         check(actionName.isNotEmpty()) { "couldn't get action name" }
52
53         processId = executionRequest.commonHeader.requestId
54         check(processId.isNotEmpty()) { "couldn't get process id for service action($actionName)" }
55
56         return executionRequest
57     }
58
59     override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
60         try {
61             prepareRequestNB(executionServiceInput)
62             processNB(executionServiceInput)
63         } catch (runtimeException: RuntimeException) {
64             log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
65             recoverNB(runtimeException, executionServiceInput)
66         }
67         return prepareResponseNB()
68     }
69
70     override suspend fun prepareResponseNB(): ExecutionServiceOutput {
71         log.debug("Preparing Response...")
72         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
73         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
74         var status = Status()
75         try {
76             // Set the Response Payload
77             executionServiceOutput.payload = JacksonUtils.objectMapper.createObjectNode()
78             executionServiceOutput.payload.set<JsonNode>("$actionName-response", responseActionPayload)
79             // Set the Default Step Status
80             status.eventType = EventType.EVENT_COMPONENT_EXECUTED.name
81         } catch (e: Exception) {
82             status.message = BluePrintConstants.STATUS_FAILURE
83             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
84         }
85         executionServiceOutput.status = status
86         return this.executionServiceOutput
87     }
88
89     fun setResponsePayloadForAction(actionPayload: JsonNode) {
90         this.responseActionPayload = actionPayload
91     }
92
93     /**
94      * Get Execution Input Payload data
95      */
96     fun requestPayload(): JsonNode? {
97         return executionServiceInput.payload
98     }
99
100     /**
101      * Get Execution Input payload action property with [expression]
102      * ex: requestPayloadActionProperty("data") will look for path "payload/<action-name>-request/data"
103      */
104     fun requestPayloadActionProperty(expression: String?): JsonNode? {
105         val requestExpression = if (expression.isNullOrBlank()) {
106             "$actionName-request"
107         } else {
108             "$actionName-request.$expression"
109         }
110         return executionServiceInput.payload.jsonPathParse(".$requestExpression")
111     }
112 }