5278c17ed9e159a8366f96a8e430613487710a9f
[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.common.api.EventType
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
43
44 @Service
45 class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
46                               private val bluePrintCatalogService: BluePrintCatalogService,
47                               private val blueprintDGExecutionService: BlueprintDGExecutionService) {
48
49     private val log = LoggerFactory.getLogger(ExecutionServiceHandler::class.toString())
50
51     fun upload(filePart: FilePart): Mono<String> {
52         try {
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)\"}")
57             }
58         } catch (e: Exception) {
59             return Mono.error<String>(BluePrintException("Error uploading the CBA file.", e))
60         }
61     }
62
63     fun process(executionServiceInput: ExecutionServiceInput,
64                 responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>) {
65         when {
66             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
67                 GlobalScope.launch(Dispatchers.Default) {
68                     val executionServiceOutput = doProcess(executionServiceInput)
69                     responseObserver.onNext(executionServiceOutput.toProto())
70                     responseObserver.onCompleted()
71                 }
72                 responseObserver.onNext(response(executionServiceInput).toProto())
73             }
74             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_SYNC -> {
75                 val executionServiceOutput = doProcess(executionServiceInput)
76                 responseObserver.onNext(executionServiceOutput.toProto())
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());
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 = JsonNodeFactory.instance.objectNode()
112
113         val status = Status()
114         status.errorMessage = errorMessage
115         if (failure) {
116             status.eventType = EventType.EVENT_COMPONENT_FAILURE.name
117             status.code = 500
118             status.message = BluePrintConstants.STATUS_FAILURE
119         } else {
120             status.eventType = EventType.EVENT_COMPONENT_PROCESSING.name
121             status.code = 200
122             status.message = BluePrintConstants.STATUS_PROCESSING
123         }
124
125         executionServiceOutput.status = status
126
127         return executionServiceOutput
128     }
129 }