2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
20 import com.fasterxml.jackson.databind.node.JsonNodeFactory
21 import io.grpc.stub.StreamObserver
22 import kotlinx.coroutines.Dispatchers
23 import kotlinx.coroutines.GlobalScope
24 import kotlinx.coroutines.launch
25 import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
26 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
27 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_SYNC
28 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput
30 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status
31 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.saveCBAFile
32 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.toProto
33 import org.onap.ccsdk.apps.controllerblueprints.common.api.EventType
34 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
35 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
36 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
37 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
38 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
39 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
40 import org.slf4j.LoggerFactory
41 import org.springframework.http.codec.multipart.FilePart
42 import org.springframework.stereotype.Service
43 import reactor.core.publisher.Mono
44 import java.util.stream.Collectors
47 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
48 private val bluePrintCatalogService: BluePrintCatalogService,
49 private val bluePrintWorkflowExecutionService
50 : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>) {
52 private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
54 fun upload(filePart: FilePart): Mono<String> {
56 val archivedPath = BluePrintFileUtils.getCbaStorageDirectory(bluePrintCoreConfiguration.archivePath)
57 val cbaPath = saveCBAFile(filePart, archivedPath)
58 bluePrintCatalogService.saveToDatabase(cbaPath.toFile()).let {
59 return Mono.just("{\"status\": \"Successfully uploaded blueprint with id($it)\"}")
61 } catch (e: Exception) {
62 return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
66 suspend fun process(executionServiceInput: ExecutionServiceInput,
67 responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>) {
69 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
70 GlobalScope.launch(Dispatchers.Default) {
71 val executionServiceOutput = doProcess(executionServiceInput)
72 responseObserver.onNext(executionServiceOutput.toProto())
73 responseObserver.onCompleted()
75 responseObserver.onNext(response(executionServiceInput).toProto())
77 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
78 val executionServiceOutput = doProcess(executionServiceInput)
79 responseObserver.onNext(executionServiceOutput.toProto())
80 responseObserver.onCompleted()
82 else -> responseObserver.onNext(response(executionServiceInput,
83 "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
88 suspend fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
89 val requestId = executionServiceInput.commonHeader.requestId
90 log.info("processing request id $requestId")
92 val actionIdentifiers = executionServiceInput.actionIdentifiers
94 val blueprintName = actionIdentifiers.blueprintName
95 val blueprintVersion = actionIdentifiers.blueprintVersion
97 val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
98 log.info("blueprint base path $basePath")
100 val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
102 val output = bluePrintWorkflowExecutionService.executeBluePrintWorkflow(blueprintRuntimeService,
103 executionServiceInput, hashMapOf())
105 val errors = blueprintRuntimeService.getBluePrintError().errors
106 if (errors.isNotEmpty()) {
107 val errorMessage = errors.stream().map { it.toString() }.collect(Collectors.joining(", "))
108 setErrorStatus(errorMessage, output.status)
114 private fun setErrorStatus(errorMessage: String, status: Status) {
115 status.errorMessage = errorMessage
116 status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
118 status.message = BluePrintConstants.STATUS_FAILURE
121 private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
122 failure: Boolean = false): ExecutionServiceOutput {
123 val executionServiceOutput = ExecutionServiceOutput()
124 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
125 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
126 executionServiceOutput.payload = JsonNodeFactory.instance.objectNode()
128 val status = Status()
130 setErrorStatus(errorMessage, status)
132 status.eventType = EventType.EVENT_COMPONENT_PROCESSING.name
134 status.message = BluePrintConstants.STATUS_PROCESSING
137 executionServiceOutput.status = status
139 return executionServiceOutput