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