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.fasterxml.jackson.databind.node.JsonNodeFactory
20 import com.google.protobuf.Struct
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.blueprintsprocessor.services.workflow.BlueprintDGExecutionService
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.utils.BluePrintFileUtils
38 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
39 import org.slf4j.LoggerFactory
40 import org.springframework.http.codec.multipart.FilePart
41 import org.springframework.stereotype.Service
42 import reactor.core.publisher.Mono
45 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
46 private val bluePrintCatalogService: BluePrintCatalogService,
47 private val blueprintDGExecutionService: BlueprintDGExecutionService) {
49 private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
51 fun upload(filePart: FilePart): Mono<String> {
53 val archivedPath = BluePrintFileUtils.getCbaStorageDirectory(bluePrintCoreConfiguration.archivePath)
54 val cbaPath = saveCBAFile(filePart, archivedPath)
55 bluePrintCatalogService.saveToDatabase(cbaPath.toFile()).let {
56 return Mono.just("{\"status\": \"Successfully uploaded blueprint with id($it)\"}")
58 } catch (e: Exception) {
59 return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
63 fun process(executionServiceInput: ExecutionServiceInput,
64 responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>,
65 inputPayload: Struct) {
67 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
68 GlobalScope.launch(Dispatchers.Default) {
69 val executionServiceOutput = doProcess(executionServiceInput)
70 responseObserver.onNext(executionServiceOutput.toProto())
71 responseObserver.onCompleted()
73 responseObserver.onNext(response(executionServiceInput).toProto())
75 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
76 val executionServiceOutput = doProcess(executionServiceInput)
77 responseObserver.onNext(executionServiceOutput.toProto())
78 responseObserver.onCompleted()
80 else -> responseObserver.onNext(response(executionServiceInput,
81 "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
86 fun processSync(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
87 return doProcess(executionServiceInput)
90 private fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
91 val requestId = executionServiceInput.commonHeader.requestId
92 log.info("processing request id $requestId")
94 val actionIdentifiers = executionServiceInput.actionIdentifiers
96 val blueprintName = actionIdentifiers.blueprintName
97 val blueprintVersion = actionIdentifiers.blueprintVersion
99 val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
100 log.info("blueprint base path $basePath")
102 val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
104 return blueprintDGExecutionService.executeDirectedGraph(blueprintRuntimeService, executionServiceInput)
107 private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
108 failure: Boolean = false): ExecutionServiceOutput {
109 val executionServiceOutput = ExecutionServiceOutput()
110 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
111 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
112 executionServiceOutput.payload = JsonNodeFactory.instance.objectNode()
114 val status = Status()
115 status.errorMessage = errorMessage
117 status.eventType = "EVENT-COMPONENT-FAILURE"
119 status.message = BluePrintConstants.STATUS_FAILURE
121 status.eventType = "EVENT-COMPONENT-PROCESSING"
123 status.message = BluePrintConstants.STATUS_PROCESSING
126 executionServiceOutput.status = status
128 return executionServiceOutput