aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentRemoteScriptExecutor.kt
blob: 2581e56281d680177f3acfa6d44b754d86232e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright © 2018-2019 AT&T Intellectual Property.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.services.execution

import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
import org.onap.ccsdk.cds.blueprintsprocessor.core.asJsonType
import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createActionIdentifiersProto
import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createCommonHeaderProto
import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createExecutionServiceInputProto
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
import org.springframework.beans.factory.config.ConfigurableBeanFactory
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Component
import java.util.UUID

/**
 * This is generic Remote Script Component Executor function
 * @author Brinda Santh
 */
@Component("component-remote-script-executor")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
open class ComponentRemoteScriptExecutor(
    private var streamingRemoteExecutionService: StreamingRemoteExecutionService<
        org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput,
        org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput>
) : AbstractComponentFunction() {

    companion object {

        const val INPUT_SELECTOR = "selector"
        const val INPUT_BLUEPRINT_NAME = "blueprint-name"
        const val INPUT_BLUEPRINT_VERSION = "blueprint-version"
        const val INPUT_BLUEPRINT_ACTION = "blueprint-action"
        const val INPUT_TIMEOUT = "timeout"
        const val INPUT_REQUEST_DATA = "request-data"

        const val ATTRIBUTE_RESPONSE_DATA = "response-data"
        const val ATTRIBUTE_STATUS = "status"

        const val OUTPUT_STATUS = "status"
    }

    override suspend fun processNB(executionRequest: ExecutionServiceInput) {
        val selector = getOperationInput(INPUT_SELECTOR)
        val blueprintName = getOperationInput(INPUT_BLUEPRINT_NAME).asText()
        val blueprintVersion = getOperationInput(INPUT_BLUEPRINT_VERSION).asText()
        val blueprintAction = getOperationInput(INPUT_BLUEPRINT_ACTION).asText()
        val requestData = getOperationInput(INPUT_REQUEST_DATA)
        val timeout = getOperationInput(INPUT_TIMEOUT).asLong()

        val requestPayload = PayloadUtils.prepareRequestPayloadStr(blueprintAction, requestData)

        val txId = UUID.randomUUID().toString()
        val commonHeader = createCommonHeaderProto(
            executionRequest.commonHeader.subRequestId,
            txId, BluePrintConstants.APP_NAME
        )
        val actionIdentifier = createActionIdentifiersProto(blueprintName, blueprintVersion, blueprintAction)

        val executionServiceInputProto =
            createExecutionServiceInputProto(commonHeader, actionIdentifier, requestPayload)

        /** Invoke remote implementation using GRPC */
        val executionServiceOutputProto =
            streamingRemoteExecutionService.sendNonInteractive(selector, txId, executionServiceInputProto, timeout)

        /** Set the response data */
        if (executionServiceOutputProto.payload != null) {
            val outputData = PayloadUtils.getResponseDataFromPayload(
                blueprintAction,
                executionServiceOutputProto.payload.asJsonType()
            )
            setAttribute(ATTRIBUTE_RESPONSE_DATA, outputData)
        }

        /** set node template attribute */
        val statusMessage = executionServiceOutputProto.status.message
        if (statusMessage == BluePrintConstants.STATUS_SUCCESS) {
            setAttribute(ATTRIBUTE_STATUS, BluePrintConstants.STATUS_SUCCESS.asJsonPrimitive())
        } else {
            val errorMessage = executionServiceOutputProto.status.errorMessage ?: "failed in remote execution"
            throw BluePrintProcessorException(errorMessage)
        }
    }

    override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
        addError("Failed in ComponentRemoteScriptExecutor : ${runtimeException.message}")
    }
}