2 * Copyright © 2017-2018 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.apps.blueprintsprocessor.selfservice.api
19 import com.google.protobuf.Struct
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.apps.blueprintsprocessor.core.BluePrintCoreConfiguration
25 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_ASYNC
26 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ACTION_MODE_SYNC
27 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
28 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput
29 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status
30 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.saveCBAFile
31 import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.toProto
32 import org.onap.ccsdk.apps.blueprintsprocessor.services.workflow.BlueprintDGExecutionService
33 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
34 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
35 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService
36 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils
37 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
38 import org.slf4j.LoggerFactory
39 import org.springframework.http.codec.multipart.FilePart
40 import org.springframework.stereotype.Service
41 import reactor.core.publisher.Mono
44 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
45 private val bluePrintCatalogService: BluePrintCatalogService,
46 private val blueprintDGExecutionService: BlueprintDGExecutionService) {
48 private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
50 fun upload(filePart: FilePart): Mono<String> {
52 val archivedPath = BluePrintFileUtils.getCbaStorageDirectory(bluePrintCoreConfiguration.archivePath)
53 val cbaPath = saveCBAFile(filePart, archivedPath)
54 bluePrintCatalogService.saveToDatabase(cbaPath.toFile()).let {
55 return Mono.just("{\"status\": \"Successfully uploaded blueprint with id($it)\"}")
57 } catch (e: Exception) {
58 return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
62 fun process(executionServiceInput: ExecutionServiceInput,
63 responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>,
64 inputPayload: Struct) {
66 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
67 GlobalScope.launch(Dispatchers.Default) {
68 val executionServiceOutput = doProcess(executionServiceInput)
69 responseObserver.onNext(executionServiceOutput.toProto(inputPayload))
70 responseObserver.onCompleted()
72 responseObserver.onNext(response(executionServiceInput).toProto(inputPayload))
74 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
75 val executionServiceOutput = doProcess(executionServiceInput)
76 responseObserver.onNext(executionServiceOutput.toProto(inputPayload))
77 responseObserver.onCompleted()
79 else -> responseObserver.onNext(response(executionServiceInput,
80 "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
81 true).toProto(inputPayload));
85 fun processSync(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
86 return doProcess(executionServiceInput)
89 private fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
90 val requestId = executionServiceInput.commonHeader.requestId
91 log.info("processing request id $requestId")
93 val actionIdentifiers = executionServiceInput.actionIdentifiers
95 val blueprintName = actionIdentifiers.blueprintName
96 val blueprintVersion = actionIdentifiers.blueprintVersion
98 val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
99 log.info("blueprint base path $basePath")
101 val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
103 return blueprintDGExecutionService.executeDirectedGraph(blueprintRuntimeService, executionServiceInput)
106 private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
107 failure: Boolean = false): ExecutionServiceOutput {
108 val executionServiceOutput = ExecutionServiceOutput()
109 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
110 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
111 executionServiceOutput.payload = executionServiceInput.payload
113 val status = Status()
114 status.errorMessage = errorMessage
116 status.eventType = "EVENT-COMPONENT-FAILURE"
118 status.message = BluePrintConstants.STATUS_FAILURE
120 status.eventType = "EVENT-COMPONENT-PROCESSING"
122 status.message = BluePrintConstants.STATUS_PROCESSING
125 executionServiceOutput.status = status
127 return executionServiceOutput