2 * Copyright © 2018-2019 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
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
30 /** This implementation is used to build services, which doesn't need blueprints */
31 abstract class AbstractServiceFunction : BlueprintFunctionNode<ExecutionServiceInput, ExecutionServiceOutput> {
34 private val log = logger(AbstractServiceFunction::class)
36 lateinit var executionServiceInput: ExecutionServiceInput
37 var executionServiceOutput = ExecutionServiceOutput()
38 lateinit var processId: String
39 lateinit var actionName: String
40 lateinit var responseActionPayload: JsonNode
42 override fun getName(): String {
46 override suspend fun prepareRequestNB(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
48 this.executionServiceInput = executionRequest
50 actionName = executionRequest.actionIdentifiers.actionName
51 check(actionName.isNotEmpty()) { "couldn't get action name" }
53 processId = executionRequest.commonHeader.requestId
54 check(processId.isNotEmpty()) { "couldn't get process id for service action($actionName)" }
56 return executionRequest
59 override suspend fun applyNB(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
61 prepareRequestNB(executionServiceInput)
62 processNB(executionServiceInput)
63 } catch (runtimeException: RuntimeException) {
64 log.error("failed in ${getName()} : ${runtimeException.message}", runtimeException)
65 recoverNB(runtimeException, executionServiceInput)
67 return prepareResponseNB()
70 override suspend fun prepareResponseNB(): ExecutionServiceOutput {
71 log.debug("Preparing Response...")
72 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
73 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
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
85 executionServiceOutput.status = status
86 return this.executionServiceOutput
89 fun setResponsePayloadForAction(actionPayload: JsonNode) {
90 this.responseActionPayload = actionPayload
94 * Get Execution Input Payload data
96 fun requestPayload(): JsonNode? {
97 return executionServiceInput.payload
101 * Get Execution Input payload action property with [expression]
102 * ex: requestPayloadActionProperty("data") will look for path "payload/<action-name>-request/data"
104 fun requestPayloadActionProperty(expression: String?): JsonNode? {
105 val requestExpression = if (expression.isNullOrBlank()) {
106 "$actionName-request"
108 "$actionName-request.$expression"
110 return executionServiceInput.payload.jsonPathParse(".$requestExpression")