dbc7340195e56c15b134d01c81d2ee3907a82785
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / ComponentRemoteScriptExecutor.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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.asJsonType
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
22 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createActionIdentifiersProto
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createCommonHeaderProto
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createExecutionServiceInputProto
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
27 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory
29 import org.springframework.context.annotation.Scope
30 import org.springframework.stereotype.Component
31 import java.util.UUID
32
33 /**
34  * This is generic Remote Script Component Executor function
35  * @author Brinda Santh
36  */
37 @Component("component-remote-script-executor")
38 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
39 open class ComponentRemoteScriptExecutor(
40     private var streamingRemoteExecutionService: StreamingRemoteExecutionService<
41         org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput,
42         org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput>
43 ) : AbstractComponentFunction() {
44
45     companion object {
46         const val INPUT_SELECTOR = "selector"
47         const val INPUT_BLUEPRINT_NAME = "blueprint-name"
48         const val INPUT_BLUEPRINT_VERSION = "blueprint-version"
49         const val INPUT_BLUEPRINT_ACTION = "blueprint-action"
50         const val INPUT_TIMEOUT = "timeout"
51         const val INPUT_REQUEST_DATA = "request-data"
52
53         const val ATTRIBUTE_RESPONSE_DATA = "response-data"
54         const val ATTRIBUTE_STATUS = "status"
55
56         const val OUTPUT_STATUS = "status"
57     }
58
59     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
60         val selector = getOperationInput(INPUT_SELECTOR)
61         val blueprintName = getOperationInput(INPUT_BLUEPRINT_NAME).asText()
62         val blueprintVersion = getOperationInput(INPUT_BLUEPRINT_VERSION).asText()
63         val blueprintAction = getOperationInput(INPUT_BLUEPRINT_ACTION).asText()
64         val requestData = getOperationInput(INPUT_REQUEST_DATA)
65         val timeout = getOperationInput(INPUT_TIMEOUT).asLong()
66
67         val requestPayload = PayloadUtils.prepareRequestPayloadStr(blueprintAction, requestData)
68
69         val txId = UUID.randomUUID().toString()
70         val commonHeader = createCommonHeaderProto(
71             executionRequest.commonHeader.subRequestId,
72             txId, BluePrintConstants.APP_NAME
73         )
74         val actionIdentifier = createActionIdentifiersProto(blueprintName, blueprintVersion, blueprintAction)
75
76         val executionServiceInputProto =
77             createExecutionServiceInputProto(commonHeader, actionIdentifier, requestPayload)
78
79         /** Invoke remote implementation using GRPC */
80         val executionServiceOutputProto =
81             streamingRemoteExecutionService.sendNonInteractive(selector, txId, executionServiceInputProto, timeout)
82
83         /** Set the response data */
84         if (executionServiceOutputProto.payload != null) {
85             val outputData = PayloadUtils.getResponseDataFromPayload(
86                 blueprintAction,
87                 executionServiceOutputProto.payload.asJsonType()
88             )
89             setAttribute(ATTRIBUTE_RESPONSE_DATA, outputData)
90         }
91
92         /** set node template attribute */
93         val statusMessage = executionServiceOutputProto.status.message
94         if (statusMessage == BluePrintConstants.STATUS_SUCCESS) {
95             setAttribute(ATTRIBUTE_STATUS, BluePrintConstants.STATUS_SUCCESS.asJsonPrimitive())
96         } else {
97             val errorMessage = executionServiceOutputProto.status.errorMessage ?: "failed in remote execution"
98             throw BluePrintProcessorException(errorMessage)
99         }
100     }
101
102     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
103         bluePrintRuntimeService.getBluePrintError()
104             .addError("Failed in ComponentRemoteScriptExecutor : ${runtimeException.message}")
105     }
106 }