Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / BluePrintProcessingGRPCHandler.kt
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.cds.blueprintsprocessor.selfservice.api
19
20 import io.grpc.Status
21 import io.grpc.stub.StreamObserver
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.toJava
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
27 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
28 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
29 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
30 import org.onap.ccsdk.cds.error.catalog.core.utils.errorCauseOrDefault
31 import org.onap.ccsdk.cds.error.catalog.core.utils.errorMessageOrDefault
32 import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogService
33 import org.slf4j.LoggerFactory
34 import org.springframework.security.access.prepost.PreAuthorize
35 import org.springframework.stereotype.Service
36 import java.util.concurrent.Phaser
37 import javax.annotation.PreDestroy
38
39 @Service
40 open class BluePrintProcessingGRPCHandler(
41     private val bluePrintCoreConfiguration: BluePrintCoreConfiguration,
42     private val executionServiceHandler: ExecutionServiceHandler,
43     private val errorCatalogService: ErrorCatalogService
44 ) :
45     BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase() {
46
47     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandler::class.java)
48
49     private val ph = Phaser(1)
50
51     @PreAuthorize("hasRole('USER')")
52     override fun process(
53         responseObserver: StreamObserver<ExecutionServiceOutput>
54     ): StreamObserver<ExecutionServiceInput> {
55
56         return object : StreamObserver<ExecutionServiceInput> {
57             override fun onNext(executionServiceInput: ExecutionServiceInput) {
58                 try {
59                     ph.register()
60                     runBlocking {
61                         executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
62                     }
63                 } catch (e: Exception) {
64                     onError(e)
65                 } finally {
66                     ph.arriveAndDeregister()
67                 }
68             }
69
70             override fun onError(error: Throwable) {
71                 log.debug("Fail to process message", error)
72                 if (error is BluePrintProcessorException) onErrorCatalog(error) else onError(error)
73             }
74
75             fun onError(error: Exception) {
76                 responseObserver.onError(
77                     Status.INTERNAL
78                         .withDescription(error.errorMessageOrDefault())
79                         .withCause(error.errorCauseOrDefault())
80                         .asException()
81                 )
82             }
83
84             fun onErrorCatalog(error: BluePrintProcessorException) {
85                 if (error.protocol == "") {
86                     error.grpc(ErrorCatalogCodes.GENERIC_FAILURE)
87                 }
88                 val errorPayload = errorCatalogService.errorPayload(error)
89                 val grpcCode = Status.fromCodeValue(errorPayload.code)
90                 responseObserver.onError(
91                     grpcCode
92                         .withDescription(errorPayload.message)
93                         .withCause(error.errorCauseOrDefault())
94                         .asException()
95                 )
96             }
97
98             override fun onCompleted() {
99                 log.info("Completed")
100             }
101         }
102     }
103
104     @PreDestroy
105     fun preDestroy() {
106         val name = "BluePrintProcessingGRPCHandler"
107         log.info("Starting to shutdown $name waiting for in-flight requests to finish ...")
108         ph.arriveAndAwaitAdvance()
109         log.info("Done waiting in $name")
110     }
111 }