20af589a1af2315f7b2c7a763e6928f30ed089f9
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / ExecutionServiceHandler.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
19
20 import io.grpc.stub.StreamObserver
21 import kotlinx.coroutines.Dispatchers
22 import kotlinx.coroutines.GlobalScope
23 import kotlinx.coroutines.launch
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
25 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.toProto
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
28 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
32 import org.slf4j.LoggerFactory
33 import org.springframework.stereotype.Service
34 import java.util.stream.Collectors
35
36 @Service
37 class ExecutionServiceHandler(private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
38                               private val blueprintsProcessorCatalogService: BluePrintCatalogService,
39                               private val bluePrintWorkflowExecutionService
40                               : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>) {
41
42     private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
43
44     suspend fun process(executionServiceInput: ExecutionServiceInput,
45                         responseObserver: StreamObserver<org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput>) {
46         when {
47             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
48                 GlobalScope.launch(Dispatchers.Default) {
49                     val executionServiceOutput = doProcess(executionServiceInput)
50                     responseObserver.onNext(executionServiceOutput.toProto())
51                     responseObserver.onCompleted()
52                 }
53                 responseObserver.onNext(response(executionServiceInput).toProto())
54             }
55             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
56                 val executionServiceOutput = doProcess(executionServiceInput)
57                 responseObserver.onNext(executionServiceOutput.toProto())
58                 responseObserver.onCompleted()
59             }
60             else -> responseObserver.onNext(response(executionServiceInput,
61                     "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
62                     true).toProto());
63         }
64     }
65
66     suspend fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
67         val requestId = executionServiceInput.commonHeader.requestId
68         log.info("processing request id $requestId")
69         val actionIdentifiers = executionServiceInput.actionIdentifiers
70         val blueprintName = actionIdentifiers.blueprintName
71         val blueprintVersion = actionIdentifiers.blueprintVersion
72         try {
73             val basePath = blueprintsProcessorCatalogService.getFromDatabase(blueprintName, blueprintVersion)
74             log.info("blueprint base path $basePath")
75
76             val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
77
78             val output = bluePrintWorkflowExecutionService.executeBluePrintWorkflow(blueprintRuntimeService,
79                     executionServiceInput, hashMapOf())
80
81             val errors = blueprintRuntimeService.getBluePrintError().errors
82             if (errors.isNotEmpty()) {
83                 val errorMessage = errors.stream().map { it.toString() }.collect(Collectors.joining(", "))
84                 setErrorStatus(errorMessage, output.status)
85             }
86             return output
87         } catch (e: Exception) {
88             log.error("fail processing request id $requestId", e)
89             return response(executionServiceInput, e.localizedMessage ?: e.message ?: e.toString(), true)
90         }
91     }
92
93     private fun setErrorStatus(errorMessage: String, status: Status) {
94         status.errorMessage = errorMessage
95         status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
96         status.code = 500
97         status.message = BluePrintConstants.STATUS_FAILURE
98     }
99
100     private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
101                          failure: Boolean = false): ExecutionServiceOutput {
102         val executionServiceOutput = ExecutionServiceOutput()
103         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
104         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
105         executionServiceOutput.payload = executionServiceInput.payload
106
107         val status = Status()
108         if (failure) {
109             setErrorStatus(errorMessage, status)
110         } else {
111             status.eventType = EventType.EVENT_COMPONENT_PROCESSING.name
112             status.code = 200
113             status.message = BluePrintConstants.STATUS_PROCESSING
114         }
115
116         executionServiceOutput.status = status
117
118         return executionServiceOutput
119     }
120
121 }