262c33f9f7228f8a86bc6e3848aaa47adbdec2d5
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
18
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
42
43 @Service
44 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
45                               private val bluePrintCatalogService: BluePrintCatalogService,
46                               private val blueprintDGExecutionService: BlueprintDGExecutionService) {
47
48     private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
49
50     fun upload(filePart: FilePart): Mono<String> {
51         try {
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)\"}")
56             }
57         } catch (e: Exception) {
58             return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
59         }
60     }
61
62     fun process(executionServiceInput: ExecutionServiceInput,
63                 responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>,
64                 inputPayload: Struct) {
65         when {
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()
71                 }
72                 responseObserver.onNext(response(executionServiceInput).toProto(inputPayload))
73             }
74             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
75                 val executionServiceOutput = doProcess(executionServiceInput)
76                 responseObserver.onNext(executionServiceOutput.toProto(inputPayload))
77                 responseObserver.onCompleted()
78             }
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));
82         }
83     }
84
85     fun processSync(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
86         return doProcess(executionServiceInput)
87     }
88
89     private fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
90         val requestId = executionServiceInput.commonHeader.requestId
91         log.info("processing request id $requestId")
92
93         val actionIdentifiers = executionServiceInput.actionIdentifiers
94
95         val blueprintName = actionIdentifiers.blueprintName
96         val blueprintVersion = actionIdentifiers.blueprintVersion
97
98         val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
99         log.info("blueprint base path $basePath")
100
101         val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
102
103         return blueprintDGExecutionService.executeDirectedGraph(blueprintRuntimeService, executionServiceInput)
104     }
105
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
112
113         val status = Status()
114         status.errorMessage = errorMessage
115         if (failure) {
116             status.eventType = "EVENT-COMPONENT-FAILURE"
117             status.code = 500
118             status.message = BluePrintConstants.STATUS_FAILURE
119         } else {
120             status.eventType = "EVENT-COMPONENT-PROCESSING"
121             status.code = 200
122             status.message = BluePrintConstants.STATUS_PROCESSING
123         }
124
125         executionServiceOutput.status = status
126
127         return executionServiceOutput
128     }
129 }