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.cds.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 kotlinx.coroutines.reactive.awaitSingle
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
27 import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.toProto
28 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
29 import org.onap.ccsdk.cds.controllerblueprints.core.*
30 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.ErrorCode
32 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService
33 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowExecutionService
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
35 import org.slf4j.LoggerFactory
36 import org.springframework.http.codec.multipart.FilePart
37 import org.springframework.stereotype.Service
39 import java.io.IOException
41 import java.util.stream.Collectors
44 class ExecutionServiceHandler(private val bluePrintPathConfiguration: BluePrintPathConfiguration,
45 private val bluePrintCatalogService: BluePrintCatalogService,
46 private val bluePrintWorkflowExecutionService
47 : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>) {
49 private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
51 suspend fun upload(filePart: FilePart): String {
52 val saveId = UUID.randomUUID().toString()
53 val blueprintArchive = normalizedPathName(bluePrintPathConfiguration.blueprintArchivePath, saveId)
54 val blueprintWorking = normalizedPathName(bluePrintPathConfiguration.blueprintWorkingPath, saveId)
57 val compressedFile = normalizedFile(blueprintArchive, "cba.zip")
58 compressedFile.parentFile.reCreateNBDirs()
59 // Copy the File Part to Local File
60 copyFromFilePart(filePart, compressedFile)
61 // Save the Copied file to Database
62 return bluePrintCatalogService.saveToDatabase(saveId, compressedFile, true)
63 } catch (e: IOException) {
64 throw BluePrintException(ErrorCode.IO_FILE_INTERRUPT.value,
65 "Error in Upload CBA: ${e.message}", e)
67 deleteNBDir(blueprintArchive)
68 deleteNBDir(blueprintWorking)
72 suspend fun process(executionServiceInput: ExecutionServiceInput,
73 responseObserver: StreamObserver<org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput>) {
75 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
76 GlobalScope.launch(Dispatchers.Default) {
77 val executionServiceOutput = doProcess(executionServiceInput)
78 responseObserver.onNext(executionServiceOutput.toProto())
79 responseObserver.onCompleted()
81 responseObserver.onNext(response(executionServiceInput).toProto())
83 executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
84 val executionServiceOutput = doProcess(executionServiceInput)
85 responseObserver.onNext(executionServiceOutput.toProto())
86 responseObserver.onCompleted()
88 else -> responseObserver.onNext(response(executionServiceInput,
89 "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
94 suspend fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
95 val requestId = executionServiceInput.commonHeader.requestId
96 log.info("processing request id $requestId")
98 val actionIdentifiers = executionServiceInput.actionIdentifiers
100 val blueprintName = actionIdentifiers.blueprintName
101 val blueprintVersion = actionIdentifiers.blueprintVersion
103 val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
104 log.info("blueprint base path $basePath")
106 val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
108 val output = bluePrintWorkflowExecutionService.executeBluePrintWorkflow(blueprintRuntimeService,
109 executionServiceInput, hashMapOf())
111 val errors = blueprintRuntimeService.getBluePrintError().errors
112 if (errors.isNotEmpty()) {
113 val errorMessage = errors.stream().map { it.toString() }.collect(Collectors.joining(", "))
114 setErrorStatus(errorMessage, output.status)
120 private suspend fun copyFromFilePart(filePart: FilePart, targetFile: File): File {
121 return filePart.transferTo(targetFile)
122 .thenReturn(targetFile)
126 private fun setErrorStatus(errorMessage: String, status: Status) {
127 status.errorMessage = errorMessage
128 status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
130 status.message = BluePrintConstants.STATUS_FAILURE
133 private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
134 failure: Boolean = false): ExecutionServiceOutput {
135 val executionServiceOutput = ExecutionServiceOutput()
136 executionServiceOutput.commonHeader = executionServiceInput.commonHeader
137 executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
138 executionServiceOutput.payload = JsonNodeFactory.instance.objectNode()
140 val status = Status()
142 setErrorStatus(errorMessage, status)
144 status.eventType = EventType.EVENT_COMPONENT_PROCESSING.name
146 status.message = BluePrintConstants.STATUS_PROCESSING
149 executionServiceOutput.status = status
151 return executionServiceOutput