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.*
27 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.saveCBAFile
28 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.toProto
29 import org.onap.ccsdk.apps.controllerblueprints.common.api.EventType
30 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
31 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
32 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
33 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
34 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
35 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
36 import org.slf4j.LoggerFactory
37 import org.springframework.http.codec.multipart.FilePart
38 import org.springframework.stereotype.Service
39 import reactor.core.publisher.Mono
42 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
43 private val bluePrintCatalogService: BluePrintCatalogService,
44 private val bluePrintWorkflowExecutionService
45 : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>) {
47 private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
49 fun upload(filePart: FilePart): Mono<String> {
51 val archivedPath = BluePrintFileUtils.getCbaStorageDirectory(bluePrintCoreConfiguration.archivePath)
52 val cbaPath = saveCBAFile(filePart, archivedPath)
53 bluePrintCatalogService.saveToDatabase(cbaPath.toFile()).let {
54 return Mono.just("{\"status\": \"Successfully uploaded blueprint with id($it)\"}")
56 } catch (e: Exception) {
57 return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
61 suspend fun process(executionServiceInput: ExecutionServiceInput,
62 responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>) {
64 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
65 GlobalScope.launch(Dispatchers.Default) {
66 val executionServiceOutput = doProcess(executionServiceInput)
67 responseObserver.onNext(executionServiceOutput.toProto())
68 responseObserver.onCompleted()
70 responseObserver.onNext(response(executionServiceInput).toProto())
72 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
73 val executionServiceOutput = doProcess(executionServiceInput)
74 responseObserver.onNext(executionServiceOutput.toProto())
75 responseObserver.onCompleted()
77 else -> responseObserver.onNext(response(executionServiceInput,
78 "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
83 suspend fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
84 val requestId = executionServiceInput.commonHeader.requestId
85 log.info("processing request id $requestId")
87 val actionIdentifiers = executionServiceInput.actionIdentifiers
89 val blueprintName = actionIdentifiers.blueprintName
90 val blueprintVersion = actionIdentifiers.blueprintVersion
92 val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
93 log.info("blueprint base path $basePath")
95 val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
97 return bluePrintWorkflowExecutionService.executeBluePrintWorkflow(blueprintRuntimeService,
98 executionServiceInput, hashMapOf())
101 private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
102 failure: Boolean = false): ExecutionServiceOutput {
103 val executionServiceOutput = ExecutionServiceOutput()
104 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
105 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
106 executionServiceOutput.payload = JsonNodeFactory.instance.objectNode()
108 val status = Status()
109 status.errorMessage = errorMessage
111 status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
113 status.message = BluePrintConstants.STATUS_FAILURE
115 status.eventType = EventType.EVENT_COMPONENT_PROCESSING.name
117 status.message = BluePrintConstants.STATUS_PROCESSING
120 executionServiceOutput.status = status
122 return executionServiceOutput