d8afe1688ffe954810024d8d6b38eb10933b850d
[ccsdk/apps.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
19
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
40
41 @Service
42 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
43                               private val bluePrintCatalogService: BluePrintCatalogService,
44                               private val bluePrintWorkflowExecutionService
45                               : BluePrintWorkflowExecutionService<ExecutionServiceInput, ExecutionServiceOutput>) {
46
47     private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
48
49     fun upload(filePart: FilePart): Mono<String> {
50         try {
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)\"}")
55             }
56         } catch (e: Exception) {
57             return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
58         }
59     }
60
61     suspend fun process(executionServiceInput: ExecutionServiceInput,
62                         responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>) {
63         when {
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()
69                 }
70                 responseObserver.onNext(response(executionServiceInput).toProto())
71             }
72             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
73                 val executionServiceOutput = doProcess(executionServiceInput)
74                 responseObserver.onNext(executionServiceOutput.toProto())
75                 responseObserver.onCompleted()
76             }
77             else -> responseObserver.onNext(response(executionServiceInput,
78                     "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
79                     true).toProto());
80         }
81     }
82
83     suspend fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
84         val requestId = executionServiceInput.commonHeader.requestId
85         log.info("processing request id $requestId")
86
87         val actionIdentifiers = executionServiceInput.actionIdentifiers
88
89         val blueprintName = actionIdentifiers.blueprintName
90         val blueprintVersion = actionIdentifiers.blueprintVersion
91
92         val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
93         log.info("blueprint base path $basePath")
94
95         val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
96
97         return bluePrintWorkflowExecutionService.executeBluePrintWorkflow(blueprintRuntimeService,
98                 executionServiceInput, hashMapOf())
99     }
100
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()
107
108         val status = Status()
109         status.errorMessage = errorMessage
110         if (failure) {
111             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
112             status.code = 500
113             status.message = BluePrintConstants.STATUS_FAILURE
114         } else {
115             status.eventType = EventType.EVENT_COMPONENT_PROCESSING.name
116             status.code = 200
117             status.message = BluePrintConstants.STATUS_PROCESSING
118         }
119
120         executionServiceOutput.status = status
121
122         return executionServiceOutput
123     }
124 }