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 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
34 * This is generic Remote Script Component Executor function
35 * @author Brinda Santh
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() {
47 const val INPUT_SELECTOR = "selector"
48 const val INPUT_BLUEPRINT_NAME = "blueprint-name"
49 const val INPUT_BLUEPRINT_VERSION = "blueprint-version"
50 const val INPUT_BLUEPRINT_ACTION = "blueprint-action"
51 const val INPUT_TIMEOUT = "timeout"
52 const val INPUT_REQUEST_DATA = "request-data"
54 const val ATTRIBUTE_RESPONSE_DATA = "response-data"
55 const val ATTRIBUTE_STATUS = "status"
57 const val OUTPUT_STATUS = "status"
60 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
61 val selector = getOperationInput(INPUT_SELECTOR)
62 val blueprintName = getOperationInput(INPUT_BLUEPRINT_NAME).asText()
63 val blueprintVersion = getOperationInput(INPUT_BLUEPRINT_VERSION).asText()
64 val blueprintAction = getOperationInput(INPUT_BLUEPRINT_ACTION).asText()
65 val requestData = getOperationInput(INPUT_REQUEST_DATA)
66 val timeout = getOperationInput(INPUT_TIMEOUT).asLong()
68 val requestPayload = PayloadUtils.prepareRequestPayloadStr(blueprintAction, requestData)
70 val txId = UUID.randomUUID().toString()
71 val commonHeader = createCommonHeaderProto(
72 executionRequest.commonHeader.subRequestId,
73 txId, BlueprintConstants.APP_NAME
75 val actionIdentifier = createActionIdentifiersProto(blueprintName, blueprintVersion, blueprintAction)
77 val executionServiceInputProto =
78 createExecutionServiceInputProto(commonHeader, actionIdentifier, requestPayload)
80 /** Invoke remote implementation using GRPC */
81 val executionServiceOutputProto =
82 streamingRemoteExecutionService.sendNonInteractive(selector, txId, executionServiceInputProto, timeout)
84 /** Set the response data */
85 if (executionServiceOutputProto.payload != null) {
86 val outputData = PayloadUtils.getResponseDataFromPayload(
88 executionServiceOutputProto.payload.asJsonType()
90 setAttribute(ATTRIBUTE_RESPONSE_DATA, outputData)
93 /** set node template attribute */
94 val statusMessage = executionServiceOutputProto.status.message
95 if (statusMessage == BlueprintConstants.STATUS_SUCCESS) {
96 setAttribute(ATTRIBUTE_STATUS, BlueprintConstants.STATUS_SUCCESS.asJsonPrimitive())
98 val errorMessage = executionServiceOutputProto.status.errorMessage ?: "failed in remote execution"
99 throw BlueprintProcessorException(errorMessage)
103 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
104 addError("Failed in ComponentRemoteScriptExecutor : ${runtimeException.message}")