c53f039358be7ea323e7d21fa5edef7632b35859
[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.fasterxml.jackson.databind.node.JsonNodeFactory
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         when {
65             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
66                 GlobalScope.launch(Dispatchers.Default) {
67                     val executionServiceOutput = doProcess(executionServiceInput)
68                     responseObserver.onNext(executionServiceOutput.toProto())
69                     responseObserver.onCompleted()
70                 }
71                 responseObserver.onNext(response(executionServiceInput).toProto())
72             }
73             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
74                 val executionServiceOutput = doProcess(executionServiceInput)
75                 responseObserver.onNext(executionServiceOutput.toProto())
76                 responseObserver.onCompleted()
77             }
78             else -> responseObserver.onNext(response(executionServiceInput,
79                 "Failed to process request, 'actionIdentifiers.mode' not specified. Valid value are: 'sync' or 'async'.",
80                 true).toProto());
81         }
82     }
83
84     fun processSync(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
85         return doProcess(executionServiceInput)
86     }
87
88     private fun doProcess(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
89         val requestId = executionServiceInput.commonHeader.requestId
90         log.info("processing request id $requestId")
91
92         val actionIdentifiers = executionServiceInput.actionIdentifiers
93
94         val blueprintName = actionIdentifiers.blueprintName
95         val blueprintVersion = actionIdentifiers.blueprintVersion
96
97         val basePath = bluePrintCatalogService.getFromDatabase(blueprintName, blueprintVersion)
98         log.info("blueprint base path $basePath")
99
100         val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(requestId, basePath.toString())
101
102         return blueprintDGExecutionService.executeDirectedGraph(blueprintRuntimeService, executionServiceInput)
103     }
104
105     private fun response(executionServiceInput: ExecutionServiceInput, errorMessage: String = "",
106                          failure: Boolean = false): ExecutionServiceOutput {
107         val executionServiceOutput = ExecutionServiceOutput()
108         executionServiceOutput.commonHeader = executionServiceInput.commonHeader
109         executionServiceOutput.actionIdentifiers = executionServiceInput.actionIdentifiers
110         executionServiceOutput.payload = JsonNodeFactory.instance.objectNode()
111
112         val status = Status()
113         status.errorMessage = errorMessage
114         if (failure) {
115             status.eventType = "EVENT-COMPONENT-FAILURE"
116             status.code = 500
117             status.message = BluePrintConstants.STATUS_FAILURE
118         } else {
119             status.eventType = "EVENT-COMPONENT-PROCESSING"
120             status.code = 200
121             status.message = BluePrintConstants.STATUS_PROCESSING
122         }
123
124         executionServiceOutput.status = status
125
126         return executionServiceOutput
127     }
128 }